问:
Hash function with uniqueness guarantees and controllable entropy
Is there a class of hash functions that meets the following specs:

Upper and lower bound can be specified

Uniqueness is guaranteed as long as the input is between the upper and lower bounds

Amount of entropy is controllable, or at least high and evenly distributed

An example of a low entropy hash function that produces unique results, and allows the upper bound to be specified is

```<code>int hash(int x,int upperBound) {
    return x - (upperBound * (x \ upperBound));
}
</code>```

This would produce a number between [0, upperBound), resetting back to 0 when the number can be divided by upperBound.

So lets say our upper bound is 20^3, that gives us 46656 numbers I believe. Feeding a number between 0 and 46655 should produce a unique result. Any number over will produce a collision. Providing the same number should always give the same result. Being able to control entropy would be a plus, but if it's evenly distributed and high then that will work fine too.

The end goal is to turn the number into an alpha numeric representation which can quickly be looked at to determine if it has been changed since the last time a number was requested. I should not receive the same number until all numbers have been used.

答:
OK, so I feel I've done enough research to at least begin to move things in a helpful direction.

The term for what you are looking for generally is a "perfect hash function", with the addition that you want some potentially manageable degree of randomness.

In general the state of the art is that you use an algorithmic method that generates a mapping, and then save that final mapping. There are lots of interesting ways to go about this (and more), but the problem comes down to the fact that the probability of a collision with any random method is likely going to guarantee a collision before you exhaust your input space. I'll provide an example of this now, in C# code (copy/paste-able code, I believe):

```<code>  System.Text.StringBuilder Sb = new System.Text.StringBuilder();

  System.Collections.Generic.HashSet<string> results = new System.Collections.Generic.HashSet<string>();

  using (System.Security.Cryptography.SHA512 hash = System.Security.Cryptography.SHA512Managed.Create() ) {
    System.Text.Encoding enc = System.Text.Encoding.UTF8;

    for (int input = 0; input < 10000; input++) {
      Byte[] result = hash.ComputeHash(enc.GetBytes(input.ToString()));

      foreach (Byte b in result) {
        Sb.Append(b.ToString("x2"));
      }

      results.Add(Sb.ToString().Substring(Sb.Length - 3));
    }

  }
</code>```

What I'm doing is giving an input value of 0-9999, converting it to a SHA512 hash, and then only taking the last 3 alphanumeric digits. You can then compare the size of the HashSet to the inputs to determine just how many duplicates you have.

The result: with 10000 inputs you only get 3735 unique results. Ouch - that's a lot of collisions! If you change your request to mapping to 4 digits (change the above final line of code to Sb.Length - 4), you get 9303 - not bad! If you allow 5 digits of output you get 9955 - still with the collisions, and we've massively extended our allowable output and only allow 10000 inputs!

So if you used such a method you need to restrict your maximum inputs considerably and likely increase the size of the output too.

If you didn't care so much about randomness, you could use (x + 18) % 46656, as in replacing the last line with this:

```<code>results.Add(((input + 18) % 46656).ToString());
</code>```

This results in zero collisions and runs a lot faster, too. The output is not at all random, of course, especially if you move up in sequence.

Now, with some hand-tuning I did manage to come up with this little doozy:

```<code>results.Add(((input * (40001) + 11) % 46656).ToString());
</code>```

So take the input, multiply it by 40001, add 11, then "wrap-around" it to your maximum input. On 0-46656 as input it generates no collisions, and yet it jumps all over the place - f(1)->400012, while f(2)->33357. If you skip using the first few inputs (0-2) the function that generates these numbers is even more opaque, and as this is non-linear it is not easy to find the function that generates this set. Multiply by an odd number and you can use this as a kind of "seed" to your function - prime numbers might be an even better choice! Small numbers result in less jumpiness, while multiplying by the same number you are taking the mod of is...well, always 0. I suspect somewhere in the middle/high side is ideal.

Then parse the integer output into your desired string format, and tadah, Bob's your uncle!

Now if you want something more random with measurable entropy and that takes a very long time to reverse-engineer...well,  that's going to be harder to arrange by far.

I hope this is at least helpful on getting you closer to where you want to go.

Body:
OK, so I feel I've done enough research to at least begin to move things in a helpful direction.

The term for what you are looking for generally is a "perfect hash function", with the addition that you want some potentially manageable degree of randomness.

In general the state of the art is that you use an algorithmic method that generates a mapping, and then save that final mapping. There are lots of interesting ways to go about this (and more), but the problem comes down to the fact that the probability of a collision with any random method is likely going to guarantee a collision before you exhaust your input space. I'll provide an example of this now, in C# code (copy/paste-able code, I believe):

  System.Text.StringBuilder Sb = new System.Text.StringBuilder();

  System.Collections.Generic.HashSet<string> results = new System.Collections.Generic.HashSet<string>();

  using (System.Security.Cryptography.SHA512 hash = System.Security.Cryptography.SHA512Managed.Create() ) {
    System.Text.Encoding enc = System.Text.Encoding.UTF8;

    for (int input = 0; input < 10000; input++) {
      Byte[] result = hash.ComputeHash(enc.GetBytes(input.ToString()));

      foreach (Byte b in result) {
        Sb.Append(b.ToString("x2"));
      }

      results.Add(Sb.ToString().Substring(Sb.Length - 3));
    }

  }

What I'm doing is giving an input value of 0-9999, converting it to a SHA512 hash, and then only taking the last 3 alphanumeric digits. You can then compare the size of the HashSet to the inputs to determine just how many duplicates you have.

The result: with 10000 inputs you only get 3735 unique results. Ouch - that's a lot of collisions! If you change your request to mapping to 4 digits (change the above final line of code to Sb.Length - 4), you get 9303 - not bad! If you allow 5 digits of output you get 9955 - still with the collisions, and we've massively extended our allowable output and only allow 10000 inputs!

So if you used such a method you need to restrict your maximum inputs considerably and likely increase the size of the output too.

If you didn't care so much about randomness, you could use (x + 18) % 46656, as in replacing the last line with this:

results.Add(((input + 18) % 46656).ToString());

This results in zero collisions and runs a lot faster, too. The output is not at all random, of course, especially if you move up in sequence.

Now, with some hand-tuning I did manage to come up with this little doozy:

results.Add(((input * (40001) + 11) % 46656).ToString());

So take the input, multiply it by 40001, add 11, then "wrap-around" it to your maximum input. On 0-46656 as input it generates no collisions, and yet it jumps all over the place - f(1)->400012, while f(2)->33357. If you skip using the first few inputs (0-2) the function that generates these numbers is even more opaque, and as this is non-linear it is not easy to find the function that generates this set. Multiply by an odd number and you can use this as a kind of "seed" to your function - prime numbers might be an even better choice! Small numbers result in less jumpiness, while multiplying by the same number you are taking the mod of is...well, always 0. I suspect somewhere in the middle/high side is ideal.

Then parse the integer output into your desired string format, and tadah, Bob's your uncle!

Now if you want something more random with measurable entropy and that takes a very long time to reverse-engineer...well,  that's going to be harder to arrange by far.

I hope this is at least helpful on getting you closer to where you want to go.


问:
Best (worst) logically impossible task assigned to you?
What was a project or spec that got put on your desk that could not possibly be done? How did you explain the dilemma to the "requester"? More importantly, did they understand after you explained the fundamental issue? </p>

答:
Once I was asked by an old, nearly senile man to make a program to win a lottery.

I was like...

"Oh... (hahhahahahahahahahaha) sorry, it's just allergies, but go on..."

He showed me his "theories" he came up with after analyzing 20 years worth of records and that if I just made a program that followed his rules, he would pay me.

I made the program, gave it to him, showed him it worked according to his rules, got paid, wished him good luck and told him if he ever bet too big and lost too much money, it's not my fault in any way.

:X

ContentLicense: CC BY-SA 2.5


问:
How do you unit test a function that clears properties?
I have a very common function that I have always unit tested in the same way, but I'm wondering if there is a better solution or if it's even possible a code smell is involved. It seems like a very simple case but I have a function that clears the properties of the object. Working in JavaScript, here is a simple example:

```<code>function Dog(name, owner) {
  this.name = name;
  this.owner = owner;

  this.reset = function() {
    this.name = '';
    this.owner = '';
  };
}

var puppy = new Dog('Max', 'Timmy');
console.log(puppy.name)  // logs "Max"
puppy.reset();
console.log(puppy.name)  // logs ""
</code>```

I would normally unit test by setting the properties, calling the clear function, and then asserting that the properties were indeed set back to the defaults or "cleared out". The reason I'm asking about such a simple solution is because of the dogma that unit tests should only have 1 assertion. I also think that a "reset" type function could get way out of hand when it is dealing with a large number of properties (i.e. an object that is meant to store a SPA's state).

I'm sure that I am over-thinking this but wanted to get some outside opinion/criticism for something I have been doing the same for many years. I just cannot possibly think of a better way to do this.

Another question could be, are unit tests surrounding a reset function necessary? To me they seem to almost just test the language implementation -- similar to a getter/setter property.

答:
Probably instead of deciding based on some authorities view of best practices, it's better to remember why you are writing unit tests - such as you want to save time later by catching bugs early in development, prevent bugs from reaching users, and document the code for developers (and possibly for others).

The deciding principle, then, is whether the time spent writing the tests is justified in achieving those goals.  Of course, this is still a guess based on limited information, but that's the best that can be done.

How long will it take you to figure out the actual bug if you write one test versus N tests?  Which way will be more stable in the face of changing requirements?  In this case I personally would estimate that writing a large number of tests probably won't be worth the time, plus the semantics of "clearing out the properties" apparently implies that it should clear out future properties as well.  Note that this could technically be written in a single assertion anyway, for example:

```<code>assert.isTrue(myLibrary.propertyCollection(objectUnderTest).All(function(x) {x==='';}));
</code>```

Whether this makes sense in your situation is something you can answer better than I can.

ContentLicense: CC BY-SA 3.0

Body:
Probably instead of deciding based on some authorities view of best practices, it's better to remember why you are writing unit tests - such as you want to save time later by catching bugs early in development, prevent bugs from reaching users, and document the code for developers (and possibly for others).

The deciding principle, then, is whether the time spent writing the tests is justified in achieving those goals.  Of course, this is still a guess based on limited information, but that's the best that can be done.

How long will it take you to figure out the actual bug if you write one test versus N tests?  Which way will be more stable in the face of changing requirements?  In this case I personally would estimate that writing a large number of tests probably won't be worth the time, plus the semantics of "clearing out the properties" apparently implies that it should clear out future properties as well.  Note that this could technically be written in a single assertion anyway, for example:

assert.isTrue(myLibrary.propertyCollection(objectUnderTest).All(function(x) {x==='';}));

Whether this makes sense in your situation is something you can answer better than I can.


问:
How does word size affect the amount of virtual address space available?
So, I should really know this stuff already, but I am starting to learn more about the lower levels of software development. I am currently reading Computer Systems: A Programmer's Perspective. by Bryant O'Hallaron.

I am on chapter 2 and he is talking about the way data is represented internally.

I am having trouble understanding something conceptually and I am sure that I'm about to make my ignorance lucid here.

I understand that a "word" is just a set of bytes and that the word size is just how many bits wide the system bus is. But, he also says: "the most important system parameter determined by the word size is the maximum size of the virtual address space. That is, for a machine with a w-bit word size, the virtual addresses can range from 0 to (2^w)-1, giving the program access to at most 2^w bytes"

I am both confused on the general relationship between the word size and the amount of addresses in the system and how the specific formula is w-bit word size=2^w bytes of memory available.

I am really scratching my head here, can some one help me out?

EDIT: I actually misinterpreted his definition of a word and consequently the definition of word size. What he really said was:

Busses are typically designed to transfer fixed-sized chunks of bytes
  known as words. The number of bytes in a word (the word size) is a
  fundamental system parameter that varies across systems. most machines
  today have word sizes of either 4 bytes(32 bits) or 8 bytes(64 bits).
  For the sake of our discussion here, we will assume a word size of 4
  bytes, and we will assume that buses transfer only one word at a
  time.

which is pretty much a catch-all for the cases discussed in the answers without having to go into detail. He also said he would be oversimplifying some things, perhaps in later sections he will go into more detail.

答:
A w-bit word can represent 2^w distinct values. If you had more than 2^w addresses, you'd have addresses that don't correspond to any combination of w bits.

If each address corresponds to a byte in memory, then 2^w addresses gives you access to 2^w bytes.


答:
The idea is that one word of memory can be used as an address in the address space (i.e., a word is wide enough to hold a pointer). If an address were larger than a word, addressing would require multiple sequential words. That's not impossible, but is unreasonably complicated (and likely slow).

So, given an w-bit word, how many values can that word represent? How may addresses can it denote? Since any bit can take on two values, we end up with 2·2·2·…·2 = 2w addresses. Addresses are counted starting by zero, so the highest address is 2w - 1.

Example using a three-bit word: There are 23=8 possible addresses:

```<code>bin: 000 001 010 011 100 101 110 111
dec:   0   1   2   3   4   5   6   7
</code>```

The highest address is 23-1 = 8 - 1 = 7. If the memory is an array of bytes and the address is an index to this array, then using a three-bit word we can only address eight bytes. Even if the array physically holds more bytes, we cannot reach them with a restricted index. Therefore, the amount of virtual memory is restricted by the word size.

Title: How does word size affect the amount of virtual address space available?

Body:
So, I should really know this stuff already, but I am starting to learn more about the lower levels of software development. I am currently reading Computer Systems: A Programmer's Perspective. by Bryant O'Hallaron.

I am on chapter 2 and he is talking about the way data is represented internally.

I am having trouble understanding something conceptually and I am sure that I'm about to make my ignorance lucid here.

I understand that a "word" is just a set of bytes and that the word size is just how many bits wide the system bus is. But, he also says: "the most important system parameter determined by the word size is the maximum size of the virtual address space. That is, for a machine with a w-bit word size, the virtual addresses can range from 0 to (2^w)-1, giving the program access to at most 2^w bytes"

I am both confused on the general relationship between the word size and the amount of addresses in the system and how the specific formula is w-bit word size=2^w bytes of memory available.

I am really scratching my head here, can some one help me out?

EDIT: I actually misinterpreted his definition of a word and consequently the definition of word size. What he really said was:

Busses are typically designed to transfer fixed-sized chunks of bytes
  known as words. The number of bytes in a word (the word size) is a
  fundamental system parameter that varies across systems. most machines
  today have word sizes of either 4 bytes(32 bits) or 8 bytes(64 bits).
  For the sake of our discussion here, we will assume a word size of 4
  bytes, and we will assume that buses transfer only one word at a
  time.

which is pretty much a catch-all for the cases discussed in the answers without having to go into detail. He also said he would be oversimplifying some things, perhaps in later sections he will go into more detail.

Body:
The idea is that one word of memory can be used as an address in the address space (i.e., a word is wide enough to hold a pointer). If an address were larger than a word, addressing would require multiple sequential words. That's not impossible, but is unreasonably complicated (and likely slow).

So, given an w-bit word, how many values can that word represent? How may addresses can it denote? Since any bit can take on two values, we end up with 2·2·2·…·2 = 2w addresses. Addresses are counted starting by zero, so the highest address is 2w - 1.

Example using a three-bit word: There are 23=8 possible addresses:

bin: 000 001 010 011 100 101 110 111
dec:   0   1   2   3   4   5   6   7

The highest address is 23-1 = 8 - 1 = 7. If the memory is an array of bytes and the address is an index to this array, then using a three-bit word we can only address eight bytes. Even if the array physically holds more bytes, we cannot reach them with a restricted index. Therefore, the amount of virtual memory is restricted by the word size.


问:
Database vs. server-sided scripting language
To keep my question short and to the point, consider this:

```<code>$relation = $mysql->query("SELECT relation($loggeduser, $requesteduser) AS a")->fetch_assoc()['a'];
if ($relation != BLOCKED)
{
    return $mysql->query("SELECT stuff FROM sometable WHERE powerlevel>9000 LIMIT 39");
} else {
    return '{"error":"not enough jquery"}';
}
</code>```

Where relation is some MySQL function. When would this^ approach be better than something like this:

```<code>$stuff = $mysql->query("SELECT getstuff($loggeduser, $requesteduser) AS a")->fetch_assoc()['a'];
return ($stuff != null) ? $stuff : '{"error":"stop it"}';
</code>```

Where getstuff is some MySQL function that just does everything that the first code would do.

In this scenario, the second seems to make a lot more sense. In reality, the second makes sense for so many other scenarios I can think of to the point where you can have a web app essentially composed of just MySQL functions that deal with almost everything you need (i.e. getUsers, getFollowers, pageInfo, etc.).

However, I'm surely missing something, and my question is this - When should I not use MySQL functions over server-sided scripting operations? You can omit the obvious (i.e. saving/processing files, etc.).

答:
The <s>second</s> first approach makes another round-trip to the database. I suppose it might let you run some complex processing on the web-server side.

If that additional processing is indeed complex, it's often preferable to keep it outside the database. Database stored procedure languages are often not the best at performance, and stored function code is usually harder to properly modularize, test, source-control and deploy.

If you already have all the (reasonably simple) processing in your relation function, you're better off using it right in the query and saving a database round-trip.


答:
I understand that a "word" is just a set of bytes and that the word size is just how many bits wide the system bus is. But, he also says: "the most important system parameter determined by the word size is the maximum size of the virtual address space. That is, for a machine with a w-bit word size, the virtual addresses can range from 0 to (2^w)-1, giving the program access to at most 2^w bytes"

That's assuming a lot.  The assumptions are not unreasonable, but holding them in a book pretending to introduce computer architecture to programmers seems a weakness of the book.

A word is a sequence of bits with the width used for normal argument of integer computation. (And that's still assuming that the architecture doesn't define a word with another, smaller, size which was the word used in an ancestor of the architecture.)  That is often the size of integer registers.  Bus have often that size, but they may be smaller or larger and are not really used to define what is the word size.

There is an important class of processors for which data addresses have the same size as word. That's not always true.  There are other processors may have a smaller or bigger address space (this is somewhat out of fashion, the 8086 had 16-bit words but the addresses were 20-bit wide, the PDP-10 had 36-bit words but the addresses were 18-bit wide).

There is an important class of processors for which each byte of memory has its own address.  That's not always true.  There are other processors which give addresses to words, not to bytes.  That's also out of fashion for general purpose processors, but more specialized one like DSP are still doing that.

So if you have a processor which is in those two classes, a word of width w bits may holds 2^w different value, each referencing one byte.  This is a maximum for the virtual memory size.  The processor may have architectural restrictions which prevent the use of all that space (reserving part for the OS, for IO), or it may have other restrictions (the data structures used to map the virtual memory to the physical one in 64-bit processors often are not able to map the whole 64-bit space). There are at least two instances of machines which ignored the high order byte of 32-bit addresses and programmers have taken advantage of that (using those bits for flags and other stuff) leading to a painful evolution when later one wanted to use the bits for addresses (IBM 360 and MC68000 as used on Mac).


问:
How to model the dependencies between fields in very complex forms
We have to create a web application that will be used as an application form for multiple insurance products (15 in total). This application form will be similar to a form wizard, it will span across multiple pages, depending on what product between 4 and 10.

The grand total of all different elements (inputs, select boxes) that the form will be rendering is around 250, but even the most complex product will not use more than 170 of them. The least complex one still requires around 80 elements.

We do not want to create 15 different application forms, one per product, we want to have a single application form which will be used by all products.

Now as you can imagine, the elements have a lot of dependencies between them. A value entered in a field can make another field or set of field appear or disappear (on the current page or a following page(s)). Some other dependencies based on entered values:

value of an element is required or not

possible values for select boxes will be changed

the validation constraints will be changed

As you can imagine, modeling this is very complex. The question is, what tool would you recommend for modeling (and documenting) all these elements, the dependencies between them and the validation constraints? How would you do the modeling? Not talking about the data model at all in this case. This model will be part of the specifications of what needs to be done and as a reference after the completion of the project. By changing the model the application forms will not be automatically changed.

Some of the things we would like to be able to do easily:

see what elements a certain element depends on

see all elements included in the form for certain product

see required elements for a certain product

define validation rules for each element

define various attributes for each element

Limitation: our product managers and product owners are the ones who will do the modelling.

答:
The question is, what tool would you recommend for modeling (and documenting) all these elements, the dependencies between them and the validation constraints? How would you do the modeling?

We had a similar project. Very complex forms, and a lot of them.

The original paper forms

Our goal was to make the web pages look exactly like them

Excel

Screen-form-element data specification

data element name (very important during coding) type, length, formatting, general rules

Enterprise Architect

Basic class design via UML.

Enterprise Architect (EA)

Here is a link.

Qualifier: it's been a few years since I last used it. A complex tool. Big learning curve. Very good UML knowledge required for precise results; there is metadata behind everything. Thus EA functionality is wide, deep, enigmatic, and sometimes quirky.

EA integrates its artifacts very well. But our teams' collective UML and EA tool knowledge was insufficient to make it a satisfactory end-to-end tool. Note that our goal was not to use it that way. Even so, better to not use (some aspects) of the tool than to use it badly.

Each developer used it to design classes for his/her assigned form (or section thereof). The business analyst used it to create use-case diagrams. Did not use it for requirements analysis because the paper forms, form data, and process were well established - and done before we adopted EA.

We never got to leverage this comprehensive tool that had the promise of integration from requirements analysis to actual code. Developers learned only enough to create the rudimentary class and sequence diagrams required for management go-code-approval. And it was apparent to me that even when I generated code-shell from the class diagrams it was too hard (that is, lack of detailed UML knowledge), too inconvenient, too tedious to keep EA in synch with code development. So as soon as we started coding the UML diagrams very quickly became obsolete and were ignored.

Good sequence diagrams are invaluable for understanding class interaction and object instantiation. A nice high-level map when coding.

Warning

Competent and (appropriately) complete business domain design is FAR more important than any tool. I get PTS just thinking about how universally &*$&# our code was except for the very rare time we did a good business model. I could write pages and pages.


问:
Bottleneck performance in ECS
I've been looking into building an entity-component-system. Basically, an entity is just an id wrapped around a struct, components are data belonging to that entity(and reference said id), and systems are the code. Entities and components are all stored inside arrays, to allow fast iteration over each.

For example, an entity could have a Mass, Position and Velocity component. A GravitySystem would take in these three components, calculate some velocity (based off of the Mass) and add it to the Position component.

My problem is, what happens when an entity is removed from the middle of an array? One option is to have the last element of the array swap positions with the entity that was just removed, so that the array stays neatly packed. The downside is that I lose the ability to reference each element by same index number, ie Entity ID 5 is at index 5, and each component belonging to that entity is also located at index 5 within their own arrays.

A solution would be to just ask if entity[i] is "active" before each iteration. Something like,

```<code>void gravitySystem(entityList[], massList[], velocityList[], positionList[])
{
    for(int i = 0; i < 100; ++i) {
       if(entityList[i].isAlive == 1) {
         velocityList[i] += 9.81 * massList[i];
         positionList[i] += velocityList[i];
       }
       else {
         printf("The entity is dead, Jim.\n");
       }
     }
}
</code>```

My problem with this solution is that if I were to have a huge list of entities, say 4M, going through this loop, then the if(entityList[i].isAlive==1) statement would have an impact on performance. Is there another solution that would remove this bottleneck? Perhaps one that would keep the array nice and packed without "holes" in it?

答:
One possible solution would be to replace the entity with a new entity with no components. With the setup you have, this sounds like it would just be a matter of leaving the entity there and removing all the components referencing the entity's ID. Then, your systems should naturally skip that entity since it doesn't have the components they require.

At the same time, you can add the index to a list of "recyclable" entity indices. When creating new entities, if there's anything in that list, shift an index off of the list and use the entity at that index instead of placing it at the end. It will have no components at this point, so you can give it the new components, effectively making it a new entity.

This is assuming your systems will skip entities that don't have the required components. Ideally you could write something like this (c++):

```<code>class GravitySystem : public System<MassComponent, PositionComponent, VelocityComponent> {
    public:
    GravitySystem() {}

    void logic(Entity& e) {
        auto mass = e.get<MassComponent>();
        auto pos = e.get<PositionComponent>();
        auto vel = e.get<VelocityComponent>();
        vel->y += 9.81 * mass->value;
        pos->x += vel->x;
        pos->y += vel->y;
    }
};
</code>```

And then in your game loop you write something like gravitySystem.process(entities), and it iterates over each entity and applies the logic on each entity that has the required components. Then you don't need to worry about IDs unless you need them for something else. Take a look at darkf/microecs for an example.

ContentLicense: CC BY-SA 4.0

Title: Bottleneck performance in ECS

Body:
One possible solution would be to replace the entity with a new entity with no components. With the setup you have, this sounds like it would just be a matter of leaving the entity there and removing all the components referencing the entity's ID. Then, your systems should naturally skip that entity since it doesn't have the components they require.

At the same time, you can add the index to a list of "recyclable" entity indices. When creating new entities, if there's anything in that list, shift an index off of the list and use the entity at that index instead of placing it at the end. It will have no components at this point, so you can give it the new components, effectively making it a new entity.

This is assuming your systems will skip entities that don't have the required components. Ideally you could write something like this (c++):

class GravitySystem : public System<MassComponent, PositionComponent, VelocityComponent> {
    public:
    GravitySystem() {}

    void logic(Entity& e) {
        auto mass = e.get<MassComponent>();
        auto pos = e.get<PositionComponent>();
        auto vel = e.get<VelocityComponent>();
        vel->y += 9.81 * mass->value;
        pos->x += vel->x;
        pos->y += vel->y;
    }
};

And then in your game loop you write something like gravitySystem.process(entities), and it iterates over each entity and applies the logic on each entity that has the required components. Then you don't need to worry about IDs unless you need them for something else. Take a look at darkf/microecs for an example.


答: 8-10 years ago, we had to write "a web app" that did what our desktop product did in about 2 weeks. The website turned out to be almost all client-side Javascript scripting ActiveX to replicated the desktop product. The only server-side processing involved generating options for client consumption.</p>

Title: Best (worst) logically impossible task assigned to you?

Body: What was a project or spec that got put on your desk that could not possibly be done? How did you explain the dilemma to the "requester"? More importantly, did they understand after you explained the fundamental issue? </p>


问:
Mobile app for a website vs browser view
I have noticed that many websites have their own mobile apps.

For developers, What is the benefit of creating mobile app for a website? In which cases it is better than viewing it using browser?

From users point of view it is not always good. Why should I install separate app for every website I often visit? Mobile app seems not always to be faster than browser, and they can take a hundred of MBs in some cases. UI of mobile apps is not always better than that of their websites, and functionality is usually less.

Which points can be crucial when determining if there is a need of mobile app for a website?

答:
Most of the websites now are programmed to work on the mobile as well with HTML5, what is known as the responsive web design. But the mobile apps are specifically required when you want to reverage native mobile functionalities as well.

For example: Facebook. It has a website which can run on mobile as well but there is an app as well which can leverage mobile specific technologies to enhance user experience like GPS to just find location and check-in or directly take pictures and upload simultaneously. Also the mobile UI is adapted according to different phone sizes and platforms giving it more intuitive feel to make it better usable.

I hope that clarifies your question.


问:
How to use multiple programming languages together in the same program?
Such a simple question, but I have not found a reasonable answer to this.

I currently program in Python, an interpreted language. I always hear of people using multiple languages in the same program? Then I hear them using scripting languages; Can someone on here please in plain english explain to me the difference between what I know as programming languages and scripts, and how on Earth do people use multiple languages together. That just does not make any sense.

How can someone use Javascript, PHP, and ASP together. Or program a game in C/C++ and use python as scripts? It just doesn't make sense to me, they are different languages for a reason, i assume, so how do they play a role with one another?

答: Bill Door gave some good examples where the "main program" is written in C or C++, and a scripting language is included for customization, But there is also a common, but different scenario, where the "main program", written in some "scripting language" (whatever people have in mind when they use that word) is extended by modules written in C or C++. For example, in Python, a typical use case would be to implement an extension or module in C, because of the better performance one can typically achieve this way. That's why the core parts of Python modules like NumPy or SciPy are mostly written in C, or PyGame, or the Python Imaging Library (PIL).</p>


答:
Interoperability is usually achieved by transferring data

Generally, you can think each language being it's own separate program. This is not always the case though...

Some examples:

Sending sql queries to a database

using json_encode() in php to encode a data structure into it's string representation, which then can be decoded by java script.

Exposing an api which can be called by an embedded interpreter. This api can be exposed by memory addresses (think lua tied in with c++). Another example is Jython, python that can be ran on the JVM.

Some reasons you want to do this:

Some problems are easier to solve in other languages

You can extend a binary program without having to re-compile it

Tying in existing solutions so you don't have to write stuff from scratch.

Title: How to use multiple programming languages together in the same program?

Body:
Such a simple question, but I have not found a reasonable answer to this.

I currently program in Python, an interpreted language. I always hear of people using multiple languages in the same program? Then I hear them using scripting languages; Can someone on here please in plain english explain to me the difference between what I know as programming languages and scripts, and how on Earth do people use multiple languages together. That just does not make any sense.

How can someone use Javascript, PHP, and ASP together. Or program a game in C/C++ and use python as scripts? It just doesn't make sense to me, they are different languages for a reason, i assume, so how do they play a role with one another?


问:
Why is *declaration* of data and functions necessary in C language, when the definition is written at the end of the source code?
Consider the following "C" code:

```<code>#include<stdio.h>
main()
{
  printf("func:%d",Func_i());
}

Func_i()
{
  int i=3;
  return i;
}
</code>```

Func_i() is defined at the end of the source code and no declaration is provide before its use in main(). At the very time when the compiler sees Func_i() in main(), it comes out of the main() and finds out Func_i(). The compiler somehow finds the value returned by Func_i()and gives it to printf(). I also know that the compiler cannot find the return type of Func_i(). It, by default takes(guesses?) the return type of Func_i() to be int. That is if the code had float Func_i() then the compiler would give the error: Conflicting types for Func_i().

From the above discussion we see that:

The compiler can find the value returned by Func_i().

If the compiler can find the value returned by Func_i() by coming out of the main() and searching down the source code, then why can't it find the type of Func_i(), which is explicitly mentioned.

The compiler must know that Func_i() is of type float--that's why it gives the error of conflicting types.

If the compiler knows that Func_i is of type float, then why does it still assume Func_i() to be of type int, and gives the error of conflicting types? Why don't it forcefully make Func_i() to be of type float.

I've the same doubt with the variable declaration. Consider the following "C" code:

```<code>#include<stdio.h>
main()
{
  /* [extern int Data_i;]--omitted the declaration */
  printf("func:%d and Var:%d",Func_i(),Data_i);
}

 Func_i()
{
  int i=3;
  return i;
}
int Data_i=4;
</code>```

The compiler gives the error: 'Data_i' undeclared(first use in this function).

When the compiler sees Func_i(), it goes down to the source code to find the value returned by Func_(). Why can't the compiler do the same for the variable Data_i?

Edit:

I don't know the details of the inner working of compiler, assembler, processor etc. The basic idea of my question is that if I tell(write) the return-value of the function in the source code at last, after the use of that function then the "C" language allows the computer to find that value without giving any error. Now why can't the computer find the type similarly. Why can't the type of Data_i be found as Func_i()'s return value was found. Even if I use the extern data-type identifier; statement, I am not telling the value to be returned by that identifier(function/variable). If the computer can find that value then why can't it find the type. Why do we need the forward declaration at all?

Thank you.

答:
Because C is a single-pass, statically-typed, weakly-typed, compiled language.

Single-pass means the compiler does not look ahead to see the definition of a function or variable. Since the compiler does not look ahead, the declaration of a function must come before the use of the function, otherwise the compiler does not know what its type signature is. However, the definition of the function can be later on in the same file, or even in a different file altogether. See point #4.

The only exception is the historical artifact that undeclared functions and variables are presumed to be of type "int". Modern practice is to avoid implicit typing by always declaring functions and variables explicitly.

Statically-typed means that all type information is computed at compile time. That information is then used to generate machine code that executes at run time. There is no concept in C of run-time typing. Once an int, always an int, once a float, always a float. However, that fact is somewhat obscured by the next point.

Weakly-typed means that the C compiler automatically generates code to convert between numeric types without requiring the programmer to explicitly specify the conversion operations. Because of static typing, the same conversion will always be carried out in the same way each time through the program. If a float value is converted to an int value at a given spot in the code, a float value will always be converted to an int value at that spot in the code. This cannot be changed at run-time. The value itself may change from one execution of the program to the next, of course, and conditional statements may change which sections of code are run in what order, but a given single section of code without function calls or conditionals will always perform the exact same operations whenever it is run.

Compiled means that the process of analyzing the human-readable source code and transforming it into machine-readable instructions is fully carried out before the program runs. When the compiler is compiling a function, it has no knowledge of what it will encounter further down in a given source file. However, once compilation (and assembly, linking, etc) have completed, each function in the finished executable contains numeric pointers to the functions that it will call when it is run. That is why main() can call a function further down in the source file. By the time main() is actually run, it will contain a pointer to the address of Func_i().

Machine code is very, very specific. The code for adding two integers (3 + 2) is different from the one for adding two floats (3.0 + 2.0). Those are both different from adding an int to a float (3 + 2.0), and so on. The compiler determines for every point in a function what exact operation needs to be carried out at that point, and generates code that carries out that exact operation. Once that has been done, it cannot be changed without recompiling the function.

Putting all these concepts together, the reason that main() cannot "see" further down to determine the type of Func_i() is that type analysis occurs at the very beginning of the compilation process. At that point, only the part of the source file up to the definition of main() has been read and analyzed, and the definition of Func_i() is not yet known to the compiler.

The reason that main() can "see" where Func_i() is to call it is that calling happens at run time, after compilation has already resolved all of the names and types of all of the identifiers, assembly has already converted all of the functions to machine code, and linking has already inserted the correct address of each function in each place it is called.

I have, of course, left out most of the gory details. The actual process is much, much more complicated. I hope that I have provided enough of a high-level overview to answer your questions.

Additionally, please remember, what I have written above specifically applies to C.

In other languages, the compiler may make multiple passes through the source code, and so the compiler could pick up the definition of Func_i() without it being predeclared.

In other languages, functions and / or variables may be dynamically typed, so a single variable could hold, or a single function could be passed or return, an integer, a float, a string, an array, or an object at different times.

In other languages, typing may be stronger, requiring conversion from floating-point to integer to be explicitly specified. In yet other languages, typing may be weaker, allowing conversion from the string "3.0" to the float 3.0 to the integer 3 to be carried out automatically.

And in other languages, code may be interpreted one line at a time, or compiled to byte-code and then interpreted, or just-in-time compiled, or put through a wide variety of other execution schemes.


问:
How did OOP evolve to include the notion of Properties
I've come from a C++ background and am going all out C# in my current job and I've just been reading a lot of Q&A about what's the difference between public fields and properties and all the back and forth in variations and incarnations of this basic question (e.g. this SO post and all the related linked questions). All of those questions are addressed in terms of practical differences that take for granted the existence of a property system but I think it would be good to approach this subject in terms of what the designers of all the languages which decided to support properties in the first place were thinking (check out the list in the Wikipedia article here). How did OOP evolve from C++/Java to extend into what the Wikipedia article interestingly identifies as a middleground between methods and member data:

"That is, properties are intermediate between member code (methods)
  and member data (instance variables) of the class, and properties
  provide a higher level of encapsulation than public fields."

MSDN adds further background:

"Although properties are technically very similar to methods, they are
  quite different in terms of their usage scenarios. They should be seen
  as smart fields. They have the calling syntax of fields, and the
  flexibility of methods."

I'd like to know how it was arrived at that this intermediate level of encapsulation proved useful for programming in general. I'm assuming this concept was not present at the first incarnation of programming languages that expressed the OOP paradigm.

答:
Properties have nothing to do with object-oriented programming, because properties are only syntactic sugar. Properties look like fields superficially, and in the .net world, it is recommended that they behave like fields in some ways, but they are not fields in any sense. Properties are syntactic sugar for one or two methods: one to get a value, and one to set a value. Either the set method or the get method may be omitted, but not both. There may be no field storing the value returned by the get method. Because they share a syntax with fields, and because they often use fields, people associate properties with fields.

Properties have advantages over fields:

In a property set method, before a property value is stored, the new value, or the state of the object, may be checked against a set of preconditions or invariants.

A property get method may exist for convenience, if retrieval of the property value can be logically considered equivalent to retrieval of a field value.

A property set method may perform other operations, such as notifying a parent object or listening objects of a change to a property value.

Because properties are the abstraction of fields, and for syntactic convenience, languages such as C# adopted the field syntax for properties.


问:
What is the "default" software license?
If I release some code and binaries, but I don't include any license at all with it, what are the legal terms that apply by default (in the US, where I am). I know that I automatically have copyright without doing anything, but what restrictions are there on it?

If I upload my code to github and announce it as a free download / contribute at will, then are people allowed to modify and close source my work? I haven't said that they cannot, as a GPL would, but I don't feel that it would by default be acceptable to steal my work either.

So what can and cannot people do with code that is freely available, but has absolutely no licensing terms attached?

答:
I think that the legal situation is sufficiently uncertain that even a practicing lawyer couldn't give you a clear cut answer.

On the one hand, you as copyright holder have not explicitly granted permission to anyone to make copies.  On the other hand, it could be construed that people have an implicit right to copy because you uploaded to github.  And there are various legal defenses that might apply to someone that you (hypothetically) sued; e.g. laches or estoppel.

Either way, you are not helping anyone by not putting a license on your code.  My advice is to do the right thing and create a new release with a reputable open source license.  And if you care about protecting your IP rights on the software going forward, talk to a copyright lawyer.

Title: What is the "default" software license?


答:
Absolutely nothing whatsoever. Properties and OOP have nothing to do with each other. Properties are nothing more than syntactic sugar for function calls, and therefore have exactly the same OOP-attachment that function calls do- which is to say, none.

Wikipedia is completely incorrect, by the way. The getMember/setMember pattern you see in Java offers exactly the same advantages (and disadvantages) as properties in C#. And you can replicate this pattern even in C if you want to.

Properties in C# are nothing more than language-supported syntactic-sugar.

Title: How did OOP evolve to include the notion of Properties

Body:
I've come from a C++ background and am going all out C# in my current job and I've just been reading a lot of Q&A about what's the difference between public fields and properties and all the back and forth in variations and incarnations of this basic question (e.g. this SO post and all the related linked questions). All of those questions are addressed in terms of practical differences that take for granted the existence of a property system but I think it would be good to approach this subject in terms of what the designers of all the languages which decided to support properties in the first place were thinking (check out the list in the Wikipedia article here). How did OOP evolve from C++/Java to extend into what the Wikipedia article interestingly identifies as a middleground between methods and member data:

"That is, properties are intermediate between member code (methods)
  and member data (instance variables) of the class, and properties
  provide a higher level of encapsulation than public fields."

MSDN adds further background:

"Although properties are technically very similar to methods, they are
  quite different in terms of their usage scenarios. They should be seen
  as smart fields. They have the calling syntax of fields, and the
  flexibility of methods."

I'd like to know how it was arrived at that this intermediate level of encapsulation proved useful for programming in general. I'm assuming this concept was not present at the first incarnation of programming languages that expressed the OOP paradigm.


答:
Well, I am not 100% sure, but I guess things are probably simpler than you expect. From the OO modeling schools of the 90's, there was a demand for modeling classes with encapsulated member attributes, and when implemented in languages like C++ or Java, this typically lead to code with lots of getters and setters, so a lot of "noise" code for a relatively simple requirement. Note that most of (probably all, did not check this) the languages listed in your linked Wikipedia article started to introduce "properties" of objects at the end of the 90's or later.

I guess that was the main reason language designers decided to add some syntactic sugar to reduce that noise. And though properties are surely no "core OO concept", they at least have something to do with object orientation. They do not show "an evolution of OOP" (as your question title assumes), but they support OO modeling at the programming language level to make the implementation easier.


问:
Why lambda/closures expressions came so late to C++?
I was reading a really great Bjarne Stroustrup's article where he exposes some C++ myths.
I look at code that focus my attention, because I wouldn't know that C++ supports this kind of expressions. I'm talking about lambda expressions, or closures.

```<code>void do_my_sort(vector<double>& v)
{
  sort(v,[](double x, double y) { return x>y; });  // sort v in decreasing order
}
</code>```

I was working with C# and JavaScript for several years, more recently with Java and Objective C and I find this language feature very powerful and useful. I currently doing some works in Smalltalk and, no surprise... the closures are there too...

So I was wondering, why this language feature came so late to C++ (2011)?

答:
Weak compilers, large scope, existing library weaknesses, and potentially negative library interactions.

First, it was very hard for implementations to support all of C++98, and only occurred many years after the Standard shipped. Adding even more features in would only have made this worse. C# doesn't have this problem because C# 1.0/2.0 are very easy to implement in comparison. The fact is that even now C++ tooling is far behind tooling for these other languages because C++ is such a bitch to handle.

Secondly, C++ lambdas have to deal with a lot of additional details. For example, the lifetime of captured objects is something that C#/JS do not have to consider.

Thirdly, I will say that there were few examples of APIs where lambdas would have been genuinely useful. For example, the original STL did a lot with function objects. However, because you couldn't compose or lazy the algorithms, and iterators seriously suck, adding lambdas would not exactly have made them into LINQ. Really addressing these issues and making the C++ algorithms on par would have required a lot more work than just lambdas, a lot of which still hasn't been done. Compared to writing custom iterators and allocators of the time, and chaining together algorithms in-place on containers by hand, using a non-local struct functor was really not that bad.

Finally, it was unclear as to whether library techniques like expression templates could completely obviate the need for a language feature at all. It turns out that they can't, but there are other language features (non-placement new, delete and their array versions, native arrays) that are now not only completely redundant in C++11, but actively seriously harmful. Repeating these mistakes is undesirable. It's better to only resort to language features where you know for sure that a library can't handle it.


答:
You have it backwards (kind of). Lambda Calculus exists as pretty much the core formal basis for programming languages, and has been for decades. It has no fields.

To model mutable state, you need to make two abstractions. One represents setting some state, and another that retrieves that state (Chapter 13 in my version of TaPL for reference). Sound familiar? From a theoretical background, OO didn't evolve to have this stuff. OO read Programming Languages 101 and made a baby step forward.

From a practical perspective, there are two fairly clear motivations. You come from a C++ background, so what would need to happen if you had a public field - say... the text in a textbox. What happens when you want to change your design so that "whenever this textbox changes, do blah"? You get to kill that field, make a function or two and wire up that logic, since you can not trust developers to call "UpdateTextbox" themselves. This is a very breaking change to your api (and unfortunately still a breaking change in .NET's implementation of properties). This sort of behavior is all over the place in the Windows API. Since that's a big deal at Microsoft, C# likely wanted to make that less painful.

The other big motivation is Java Beans and their kin. A number of frameworks were built to use Java reflection to look for GetX and SetX pairs and effectively treat them like modern properties. But since they weren't actual language constructs, these frameworks were fragile and unweildy. If you typo'd a name, things would just break silently. If one got refactored, nothing moved the other side of the property. And doing all of the field/get/set boilerplate was verbose and tedious (even for Java!). Since C# was developed largely as "Java with lessons learned" that sort of pain was one of those lessons.

But the biggest thing is that the property concept has been successful. It's easy to understand, it's easy to use. These help adoption greatly, and as a tool, programmers have found that properties solve a subset of problems more cleanly than either functions or fields.


答:
C++ did not have an ISO standard until well after it was first created. Much of its early life was fragmented, with compilers all working differently to the point of being source-incompatible at times.

The first C++ standard was C++98, with relatively minor updates in the C++03 standard. The primary goal of these standards was to take the "wild west" of C++ and bring some semblance of order to it. Once the dust settled and developers got over the changes, the C++ committee would focus on new features.

From Bjarne Stroustrup himself (some parenthesis removed for brevity), The C++ Programming Language (4th Edition), section 1.4.4 "The 2011 Standard":

The current C++, C++11, known for years as C++0x, is the work of the
  members of WG21. The committee worked under increasingly onerous
  self-imposed processes and procedures. These processes probably led to
  a better (and more rigorous) specification, but they also limited
  innovation. An initial draft standard for public review was produced
  in 2009. The second ISO C++ standard was ratified by a 21-0 national
  vote in August 2011.

One reason for the long gap between the two standards is that most
  members of the committee (including me) were under the mistaken
  impression that the ISO rules required a "waiting period" after a
  standard was issued before working on new features. Consequently,
  serious work on new language features did not start until 2002.

C++ was not even standardized until 1998, and the next few years were spent fixing errors in the existing (new) standard. Then the committee sat on its hands for a couple of years due to procedural issues that were likely unnecessary. It then took seven years from initial inception to a public document, with much arguing and editing along the way. Even then, it took another two years until the document was in a position to be voted on and ratified.

What this means is the addition of lambdas and other language features had to wait until the status quo was stabilized (1998/2003), then politics and procedures of the process dragged out the addition of new features far longer than it probably needed to (2011).

Another reason for the lack of urgency is that functors can do almost anything a lambda can do, they are just more verbose. The language as a whole was not missing much other than the brevity of lambdas and their ability to bind to local variables. However, even that can mostly be worked around by passing references into the functor.

Title: Why lambda/closures expressions came so late to C++?

Body:
I was reading a really great Bjarne Stroustrup's article where he exposes some C++ myths.
I look at code that focus my attention, because I wouldn't know that C++ supports this kind of expressions. I'm talking about lambda expressions, or closures.

```<code>void do_my_sort(vector<double>& v)
{
  sort(v,[](double x, double y) { return x>y; });  // sort v in decreasing order
}
</code>```

I was working with C# and JavaScript for several years, more recently with Java and Objective C and I find this language feature very powerful and useful. I currently doing some works in Smalltalk and, no surprise... the closures are there too...

So I was wondering, why this language feature came so late to C++ (2011)?


答:
What a closure does, in essence, is abstract over the lifetime of variables.

Manual memory management OTOH, requires the programmer to be aware of the lifetime of variables. But, how can you be aware of something that is abstracted away from you?

Closures are fundamentally incompatible with manual memory management. Reconciling the two is very hard, and the C++ community has done the best job they can, but actually, you can still get errors (Or maybe even UB? I admit I don't actually know C++.) using closures that capture variables which are then freed.

Or, put another way: closures extend the dynamic scope of variables beyond their lexical scope, but the fact that dynamic scope and lexical scope are identical is the fundamental basis of C++ idioms such as RAII.

C++ was AFAIK the first language with manual memory management to do closures, so they were doing original research and original research takes time. That's the main reason why it took so long. They were doing this research in a committee, which is about the worst place to do research, which probably prolonged the process even more, but the main reason is that they were doing hard original research, not that they were doing so in a committee.


答:
Using Mobile app from websites instead of browser view is useful in the following conditions:

The mobile application of the website has additional functions that can't be done on the website    alone, for example (stack-exchange) which you can use easily from the mobile application and you    can jump to several sites on stack-exchange network in seconds without having to browse for each    site on the network, also   you can edit your questions and view your answers and status in     seconds.

Some websites doesn't have mobile compatibility, which means it is designed to be accessed from     desktop or laptop rather than from mobile, because is not designed for mobile browser (unscaled     for mobile devices), and you use that website frequently, it will be better to use the  website     application on your mobile.

Some websites are huge for example news websites like (BBC.com, AlJazeera.net, CNN.com),    when you are looking for news of certain region or category you need to deal with the website   interface directly and may be end up using the website search for results, while all that and more  can be accomplished using the website's application on your mobile in less time and effort.

Some mobiles doesn't support high speed Internet or subscribed to limited bandwidth packages,   when you access a website with relatively large pages (most modern websites are large) it will take     time to load in your phone Web browser and may consume large portion of your limited    bandwidth,  by using    the website's application you can access the data required and save some    precious time and money without having to load the whole Website or web pages.

Title: Mobile app for a website vs browser view

Body:
I have noticed that many websites have their own mobile apps.

For developers, What is the benefit of creating mobile app for a website? In which cases it is better than viewing it using browser?

From users point of view it is not always good. Why should I install separate app for every website I often visit? Mobile app seems not always to be faster than browser, and they can take a hundred of MBs in some cases. UI of mobile apps is not always better than that of their websites, and functionality is usually less.

Which points can be crucial when determining if there is a need of mobile app for a website?


答:
It's all about Encapsulation and the Uniform Access Principle.

An object should be able to respond to a message by either returning existing data or running a method, but the sender shouldn't be able to tell which is which. Or if you view this from the sender's side: the sender should be able to access existing data or run a method through a uniform interface.

There are several ways to achieve this:

get rid of data altogether, just have methods (Newspeak does this)

a less radical form of the above: get rid of data in the public interface, i.e. make data always private, in the public interface, only expose methods (Smalltalk, Ruby do this)

get rid of methods altogether, just have data (Self does this, "methods" are just Method objects assigned to instance variables, instance variables are looked up with virtual dispatch)

make no syntactic or semantic distinction between methods and data (Scala does this, accessing a field is syntactically indistinguishable from calling a method without an argument list (foo.bar), assigning to a field is syntactically indistinguishable from calling a specially named method (foo.bar = baz) is the same as foo.bar_=(baz) i.e. calling a method named foo_=, and read-only values can be overridden or implemented by a method without parameter list (i.e. val foo) in a superclass can be overridden (or an abstract val be implemented) in a subclass with a method def foo)

Java, however, doesn't follow the Uniform Access Principle. In Java, it is possible to distinguish between accessing data and running a method. foo.bar is different from foo.bar(). The reason for this is that fields and methods are semantically and syntactically distinct.

C# tries to fix this by adding properties to the language, basically methods that look like fields. However, method calls still look and feel different from field and property accesses. Fields and properties now have Uniform Access, but methods still don't.

So, this doesn't actually fix the problem: you cannot fix having two different ways to access things by adding a third way to access things! Even if that third way looks like one of the other two ways, you will still have (at least) two different ways. You can only fix it by either getting rid of all the different ways except one or get rid of the differences.

It's perfectly fine to have a language with methods, properties and fields, but all three should have uniform access.


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

1) Client's connect directly to the SQL database

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

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

Entity Framework (connect to sql database)

WCF (web service)

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

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

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

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

public WcfUserRepository : IUserRepository
{
   IUserRepositoy _userRepository;

   public WcfUserRepository(IUserRepository userRepository)
   {
   }

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

So either way the clients will only rely on IUserRepository.

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

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

Any opinions on the best practise here?

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

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

答:
I'd say it depends on the application and its users.  If its an internal company app localized to an office location talking to the db directly is better, as building a service between the app and db makes the building of your software more expensive for no real gain.  If there are physically disconnected branch locations or your app is available outside of the company's internal network a service will bring benefits such as security (opening your SQL db to the world shouldn't be considered) as well as keeping the relatively chatty db traffic on the company network which is faster while having few round trips between the client and service.

Entity framework in either case is how you abstract specific db servers, although in practice changing is rarely done.

Also in either event you should limit how much data comes back.  Its unlikely users can work with 10,000 rows on and individual basis.  Paging will help there.

Title: Clients connect to WCF or SQL Server


答:
It's a matter of implementation versus implication. Properties were in OOP before C++ or Java hit the scene (they were there, with some roughness around the edges, in Simula, and they are fundamental to Smalltalk). Entities with properties are conceptually different from values with code attached. The get & set prefixes in some language conventions only serve to muddy the waters; they make you aware of the difference between fields and properties, assuming that fields may be accessed directly without get/set in a manner that is idiomatic to the language, and that's leaky.

The whole point of OOP is to treat things as if they were entities in the "real" world, not just as structs with some code mixed in. Another programmer should need to know very, very little about the way I have implemented things, and should not be at all concerned with which of the various values they are allowed to get and/or set are real and which are virtual. If you run across a vector of mine, you shouldn't need to know whether I'm storing angle and magnitude or real and imaginary components internal to the vector object. If I change the representation in V2.0 of my library, it shouldn't affect your code at all (though you may wish to take advantage of the cool new features). Similarly, there are properties an entity might have that depend on data external to the entity, but which are undoubtedly properties from a lexical standpoint. You ask people "how old are you", not "please perform the calculation that will reveal your age to me", even though you know that the data available to that "object" are date of birth (a private immutable member) and today's date (a public, auto-incrementing environmental property, dependent on time zone, daylight savings time, and the International Date Line). Age is a property, not a method, even though it takes some calculation to get there and cannot (except in toy computer representations of things with artificially-limited lifetimes) be stored as a field.

Rather than thinking of properties as the bastard child of fields and methods, it's far more satisfying to thing of methods as a specialized sort of property - things that your entities can do rather than things that they are. Otherwise you're not conceptually dealing with object/entities, you're dealing with data collections that happen to have code attached to them. The implementaions may be identical, but the implications are different.

It should be needless to say, however, that this abstraction comes at a cost. If a programmer using a class can't tell whether he or she is accessing data as it is stored or getting/setting values that need to be computed, then there will be a level at which the language is also necessarily uncertain (and therefore may require that everything requires code to intermediate between accessors/selectors and values). There is nothing conceptually wrong with "structs with code" - they can certainly be much more efficient - but they leak implementaion all over the place, and that's one of the things that OOP is supposed to eliminate.


答: In .net specifically, properties originate from the old Visual Basic days which as it happens was not object oriented in the way we think about it today. It was largely build around the then-new COM system that superficially treated everything not necessarily as classes but in terms of components which would expose properties that could be accessed both in code but also in graphical editors. When VB and the newly created C# were merged into .net, VB gained a whole lot of OOP-features and they kept properties since removing them would be like a step backwards - imagine if the automatic code update tool they had in Visual Studio had been replacing all your properties with getters and setters and was breaking compatibility with all COM-libraries. It would only be logical to support them in all .net-languages so components can be used across the whole framework without interop issues or exposing the internal implementation.</p>


答:
A combination of tools might help manage the complexity.  I like to start with a stuctured yet descriptive approach (as distinct from a highly formalised approach) which is easy for humans to interact with.  PM's should be comfortable with spreadsheets and it can be helpful to layout dependencies in tabular format.

Eg a table for product x field dependencies.

A second table might encapsulate the interactions between fields (field x field).  The intersecting cells might initially contain descriptive text.

As a first pass this may expose issues with the logic and/or identify opportunities for simplifying the logic.

And while PMs might shy away from web programming directly, use a modern, expressive client side technology to build up the "language" of your application.  Tools such as angular.js help encourage focus on what the components do and minimising noise code.  The right web technology should also provide good test support.

Title: How to model the dependencies between fields in very complex forms

Body:
We have to create a web application that will be used as an application form for multiple insurance products (15 in total). This application form will be similar to a form wizard, it will span across multiple pages, depending on what product between 4 and 10.

The grand total of all different elements (inputs, select boxes) that the form will be rendering is around 250, but even the most complex product will not use more than 170 of them. The least complex one still requires around 80 elements.

We do not want to create 15 different application forms, one per product, we want to have a single application form which will be used by all products.

Now as you can imagine, the elements have a lot of dependencies between them. A value entered in a field can make another field or set of field appear or disappear (on the current page or a following page(s)). Some other dependencies based on entered values:

value of an element is required or not

possible values for select boxes will be changed

the validation constraints will be changed

As you can imagine, modeling this is very complex. The question is, what tool would you recommend for modeling (and documenting) all these elements, the dependencies between them and the validation constraints? How would you do the modeling? Not talking about the data model at all in this case. This model will be part of the specifications of what needs to be done and as a reference after the completion of the project. By changing the model the application forms will not be automatically changed.

Some of the things we would like to be able to do easily:

see what elements a certain element depends on

see all elements included in the form for certain product

see required elements for a certain product

define validation rules for each element

define various attributes for each element

Limitation: our product managers and product owners are the ones who will do the modelling.


问:
Development on Android without using Java
As the title of this question suggests, I am looking forward to develop app(s) on Android platform without using Java at all. It doesn't mean I have some kind of vendetta against this beautiful language or I don't know how to develop apps in Java. But I am considering that my application(s) can be developed by only using XML, JavaScript and Ajax.

So do I still have to rely on Java?

Will there be any disadvantage(s) of not using Java?

Will I still be able to extend my app(s) in the future?

Am I asking a really stupid question here?

Kindly help me out.

Edit:

The app I am working on (still on whiteboard, need expert opinion on this approach) will simply fetch news from different news sources and will display to its user.

答:
Checkout Ionic and Telerik appBuilder.

If you are more getting to the bits and pieces and doing all on your own check Cordova which is the base of the two mentioned above.

So do I still have to rely on Java?

No for most of the cases.

Will there be any disadvantage(s) of not using Java?

you are not doing native app

you are limited by plugins available by the framework you are using

and a small performance issue

Will I still be able to extend my app(s) in the future?

Yes but again depend on which framework you are doing it and what it allows.

Am I asking a really stupid question here?

Not at all.


问:
Safety-critical software and optimising compilers
I'm reading some internal documentation for code developed to the MISRA C guidelines, and I see that in compiler flags section of the documentation there is a note to compile with optimisation off (-O0). I'm new to safety-critical development, so in my mind turning off optimisation is overly-pessimistic and modern compilers will produce valid code with moderate optimisation, and anyway a decent test suite will catch compiler flaws.

Are there good arguments for disabling optimisation? Perhaps because of this disabling, the code is riddled with left- and right-shifts replacing divisions and other micro-optimisations that I believe we should leave to the compiler to deal with.

答:
I agree with you that it's overly pessimistic, but some (potentially historical) reasons:

DO-178C Level A code requires Source Code to Object Code Traceability, which is much harder to prove manually with optimized code (and qualified compilers are very expensive)

optimised code is harder to debug, which might prohibit scripted debugging opportunities

there could be more compiler bugs during optimization


答: Once I was tasked with creating a 1:1 translation of Access VBA code to PL/SQL. Things went relatively well, until I found out that deep within several nested IFs and LOOPs of a procedure, it would open a yes-no-dialog and ask for another decision by the user. The best I could offer, and eventually implemented, was to add another parameter to that procedure; if it ran into that branch, it would rollback, return a special status that instructs the GUI program to ask the user, and then restart the procedure with an extra parameter.</p>


答:
If the optimiser for your chipset/platform is not known to be reliable, then you need to ask yourself what would be the side effect of incorrect code being deployed to your embedded device:

A Set Top Box may crash and need the user to restart.

A hospital life-support system, might cause loss of life.

While comprehensive system tests will help to catch known scenarios, it's the unknown scenarios that will likely catch you out.

Given this, it seems like there may be some premature optimisation going on in the code base - have you performance-profiled the code to determine that code with all the shift operators is the primary cause of performance problems? Are you using the best algorithm for the job?

Title: Safety-critical software and optimising compilers

Body:
I'm reading some internal documentation for code developed to the MISRA C guidelines, and I see that in compiler flags section of the documentation there is a note to compile with optimisation off (-O0). I'm new to safety-critical development, so in my mind turning off optimisation is overly-pessimistic and modern compilers will produce valid code with moderate optimisation, and anyway a decent test suite will catch compiler flaws.

Are there good arguments for disabling optimisation? Perhaps because of this disabling, the code is riddled with left- and right-shifts replacing divisions and other micro-optimisations that I believe we should leave to the compiler to deal with.


答:
I gonna go through your question paragraph by paragraph:

For developers, What is the benefit of creating mobile app for a website? In which cases it is better than viewing it using browser?

If your app needs to use stuff like SMS, addressbook access, or so then you have to use an app. But, many things can be done by now in HTML5 and JavaScript, just as an example, Geolocation.

From users point of view it is not always good.
  Why should I install separate app for every website I often visit?

Push notifications. For news sites for example for headline etc. Another example is for facebook the incoming messages.

You actually can implement push notifications with websites as well, but it isn't very pretty, because you would send an SMS or an email with a link to the new message or headline.

Mobile app seems not always to be faster than browser, and they can take a hundred of MBs in some cases.

The first part is sad but true, the second part is, to say it nicely, wrong. When a mobile app, that replaces a website takes 100MB place, it is just very crappy programmed. The usual app I install is under 5 MB and even that is fairly much.

UI of mobile apps is not always better than that of their websites[...]

That is not nessacerly true. For WP, Sailfish OS or Firefox OS users the UI of websites is often everything but native. The other question is, what is a good UI? But that topic is discussed on https://ux.stackexchange.com/

[...]functionality is usually less.

You can decide that when you develop the app. I personally would develop apps with the same functionality like the mobile website.

Which points can be crucial when determining if there is a need of mobile app for a website?

Like I said earlier, if you need special technologies or if you want to provide all users a native UI. But that is a hard topic, cause there are too many systems out there.

Conclusion

With HTML5, CSS3 and JavaScript webapps kinda grew up. But there is still a range of things that you cannot build with a mobile websites.

Here is a quick list:

Push notifications

Native UI for everybody (could get tricky)

SMS

Addressbook

Camera

Compass

Some more system components

Cryptographics on clientside

I hope it helps :)


答:
If you think about avoiding Java, but not Dalvik altogether, you could use Kotlin.

http://kotlinlang.org

Very neat language by JetBrains; thus it integrates well with Android Studio (or IntelliJ Idea). Much less verbose than Java, and introduces some functional programming concepts. Kind of like Scala, only simplier. It can coexist with Java in an Android project, or you can move everything to Kotlin. The IDE plugin allows for converting Java code into Kotlin automatically, so it's easy and facilitates learning it.

See: http://blog.gouline.net/2014/08/31/kotlin-the-swift-of-android/

It's a niche language and not 100% mature yet. I wouldn't advocate using it in large scale commercial projects, but for something light-weight or a hobby app; go for it. As a learning experience, and for the taste of a slightly different paradigm.

What would you be missing on?

Kotlin skills are not sought after by employers (as of now at least), so learning it doesn't add significant experience to your CV. Although a project done in Kotlin could still make for an interesting entry on a resume, indicating an enthusiastic programmer;

Superb refactoring functionality provided by Android Studio for Java is very limited when coding in Kotlin.

It has its counterpart born in the Eclipse world: http://eclipse.org/xtend/ - which seems to be somewhat similar. I've never tried that one though.

Title: Development on Android without using Java

Body:
As the title of this question suggests, I am looking forward to develop app(s) on Android platform without using Java at all. It doesn't mean I have some kind of vendetta against this beautiful language or I don't know how to develop apps in Java. But I am considering that my application(s) can be developed by only using XML, JavaScript and Ajax.

So do I still have to rely on Java?

Will there be any disadvantage(s) of not using Java?

Will I still be able to extend my app(s) in the future?

Am I asking a really stupid question here?

Kindly help me out.

Edit:

The app I am working on (still on whiteboard, need expert opinion on this approach) will simply fetch news from different news sources and will display to its user.


问:
What'd be better? To add one more local variable, or to call a method twice in general case?
I have the method which returns java.util.Date inside the hibernate-entity class:

```<code>package ua.com.winforce.loto_partner.commons.db.entity;

@Entity
@Table(schema = "pr", name = "publice")
public class Pr {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

    @Column(name = "reg_date")
    private Date regDate;

    //GET, SET
}
</code>```

And in a method I need to create the local variable which will be hold getRegDate(); value, or to invoke that method twice. What would be more appropriate in that case? I mean, in the first case we're potentially closing the moment when GC will be triggered, but in the first we waste our time to the second method invocation.

答:
There are three reasons* to cache a function return in a variable:

You know or suspect that the function has side effects.

The function can return different values on every call, and you need a consistent result. Normally this is due to the function having side-effects, but it can also be due to reading volatile data.

The repeated function call would make the code less clear. This is the "extract explanatory variable" refactoring.

Your example doesn't appear to fit any of these cases.

* there is a fourth, but it's reserved for Doug Lea and Martin Thompson, and is arguably a combination of #1 and #2.


问:
Functional programming for loop side effect
I am trying to get my head around as to why having a local variable or a for loop inside a function is not considered to be pure functional programming.

Given this function:

```<code>int as_int(char *str)
{
    int acc; /* accumulate the partial result */

    for (acc = 0; isdigit(*str); str++) {
        acc = acc * 10 + (*str - '0');
    }

    return acc;
}
</code>```

Under what circumstances would the variable acc be a side-effect ? Even in a concurrent environment each invocation of the function would have its own copy of acc. So I don't quite get why it isn't allowed in functional programming.

答:
Looping in functional programming isn't done with control statements like for and while, it's done with explicit calls to functions like map, fold, or recursion - all of which involve placing the inner loop call inside another function. If the loop code mutates variables outside the loop, this inner loop function would be manipulating variables outside its scope and would thus be impure. So the whole outer function is pure, but the looping is not. Looping constructs in functional programming require you to make the state explicit. Translating your code to something using functional programming looping tools reveals the impurity:

```<code>int as_int(char *str)
{
    int acc = 0; /* accumulate the partial result */

    map(takeWhile(isdigit, str), void function(char *chr) {
      acc = acc * 10 + (chr - '0');
    });

    return acc;
}
</code>```

(Note - this syntax is approximate to get the general idea across)

This code uses an inner function for the loop body which must mutate the variable acc, which is outside it's scope. This is impure - the inner loop function depends on the outer loop context, calling it multiple times with the same character will have side-effects, and the order you call it on the sequence of characters matters. In functional programming, in order to make this a pure function you would have to make this dependency on state passed between loop iterations explicit with fold:

```<code>int as_int(char *str)
{
    return fold(takeWhile(isdigit, str), 0, int function(char *chr, int acc) {
      return acc * 10 + (chr - '0');
    });
}
</code>```

fold uses a function of two arguments for the inner loop body: the first argument is an item in the sequence that fold is looping over, while the second is some value that the inner loop body uses to build up partial results. For the first loop iteration, acc is 0, for the second, acc is whatever the first inner loop function call returned, for the third, it's whatever the second inner loop returned, and the final loop returns the result of the whole fold expression.

Note that this isn't really a problem with your code from the perspective of the rest of your program - both definitions of as_int are pure. The difference is by making the inner loop code a pure function, you can take advantage of the massive array of tools functional programming offers to decompose the loop into something more declarative (e.g. using takeWhile, fold, filter, map, etc. etc.)

Body:
Looping in functional programming isn't done with control statements like for and while, it's done with explicit calls to functions like map, fold, or recursion - all of which involve placing the inner loop call inside another function. If the loop code mutates variables outside the loop, this inner loop function would be manipulating variables outside its scope and would thus be impure. So the whole outer function is pure, but the looping is not. Looping constructs in functional programming require you to make the state explicit. Translating your code to something using functional programming looping tools reveals the impurity:

int as_int(char *str)
{
    int acc = 0; /* accumulate the partial result */

    map(takeWhile(isdigit, str), void function(char *chr) {
      acc = acc * 10 + (chr - '0');
    });

    return acc;
}

(Note - this syntax is approximate to get the general idea across)

This code uses an inner function for the loop body which must mutate the variable acc, which is outside it's scope. This is impure - the inner loop function depends on the outer loop context, calling it multiple times with the same character will have side-effects, and the order you call it on the sequence of characters matters. In functional programming, in order to make this a pure function you would have to make this dependency on state passed between loop iterations explicit with fold:

int as_int(char *str)
{
    return fold(takeWhile(isdigit, str), 0, int function(char *chr, int acc) {
      return acc * 10 + (chr - '0');
    });
}

fold uses a function of two arguments for the inner loop body: the first argument is an item in the sequence that fold is looping over, while the second is some value that the inner loop body uses to build up partial results. For the first loop iteration, acc is 0, for the second, acc is whatever the first inner loop function call returned, for the third, it's whatever the second inner loop returned, and the final loop returns the result of the whole fold expression.

Note that this isn't really a problem with your code from the perspective of the rest of your program - both definitions of as_int are pure. The difference is by making the inner loop code a pure function, you can take advantage of the massive array of tools functional programming offers to decompose the loop into something more declarative (e.g. using takeWhile, fold, filter, map, etc. etc.)


答:
It's not just compiler bugs you have to worry about. Some "perfectly valid" (ie. conforming to the letter of the language spec) compiler optimizations produce quite unexpected behavior. See Finding Undefined Behavior Bugs by Finding Dead Code.

The point is that these problems are real, and they’re nasty because the problem can only be seen by looking at the compiler’s output. Compilers are getting smarter all the time, causing code that previously worked to break. A sufficiently advanced compiler is indistinguishable from an adversary.


答: C++ had function objects from 1983 onwards; they took/takes care of many examples where people now use lambdas (and use lambdas in other languages). In fact, a C++ lambda is probably best understood as a simplified notation for defining and immediately using a function object.</p>


问:
As a software engineer, who should I be following on Twitter?
Who in the software engineering and software development fields uses Twitter to tweet about relevant happenings in the field?</p>

答: If you're a .Net/Mono developer: Miguel de Icaza</p>


问:
Need an example of a real backlog task or how elaborate must it be?
I'd like to know whether a task must be self contained.

Our tasks mostly contain a couple of sentences.

To understand what you are to do you should:

Read the requirements document for several related back log items.

Know how system currently works and understand how it differs
from what is required.

In other words: newcomer or the person who was absent at the planning meeting will require a sufficient amount of time just to comprehend the task.

In addition such way of keeping backlog does not tell much about system evolution and looks more like a primitive contextually conditioned to-do list.

答:
In most agile framework implementations, the tasks and stories are meant to be a reminder to have a conversation, not a fully detailed spec.

We have had similar problems where new team members who are not familiar with agile or who have missed the planning sessions struggle to work on their own without guidance. The simple solution here is to guide them.

Your Scrum Master, or whomever is playing a similar agile coach role, should be recognizing this dynamic in the team and should be making sure your team member knows that they can reach out to your Product Owner.

The Product Owner, or whomever is playing the role of owning the business requirements, should be available constantly to answer questions, refine acceptance criteria, and generally help the team understand what it is that needs to be built.

That being said, it is generally helpful to capture the output of discussions or decisions made during the conversation on your backlog tasks so that everybody remembers why a decision was made or why there was a deviation from the original criteria.


答:
What's the difference between a functor and a lambda? The way the code you write looks!

A lambda is just a functor written in-line so to avoid a bit of boring boilerplate (replacing it with less boilerplate). So asking why C++ didn't have this feature is missing the point of what the feature really is. What extra functionality do you gain from a lamdba that you didn't get from a functor?

So apart from a short-hand syntax, there's nothing new to see here. (and remember some shorthand syntax is bad, take a look at some perl sometime and tell me shortening language features so they're easy and quick to type is a good thing :-) )


问:
Java Doc - Do fields also get documented?
This is a really simple question but oddly, I'm finding it difficult to get a definite answer....

What do you do with fields? Is this valid?

```<code>/**
 * Keeps track of all usernames in the system.
 */
private List<String> usernames = new ArrayList<>();
</code>```

Or is above ingnored by documentation tools, so would it be best to do something like:

```<code> //Keeps track of all usernames in the system.
private List<String> usernames = new ArrayList<>();
</code>```

Since (from what I see), documentation only shows methods, constructors and classes, what do programmers usually do with fields?

答:
Private fields are usually ignored by documentation tools, so when you ship your API to a third party, they will not be able to see any documentation regarding private fields (unless you specify that you also want to include private fields in the documentation during the documentation generation phase of your project).

That being said, I think it is a good idea to use JavaDoc to also document private fields because:

It will conform to the rest of the other documentation.

Most IDEs will extract JavaDoc and show it to you when you try and access different object properties. This can save time going around the code to see what is the purpose of a particular variable.

In the event that you will want to include private field documentation into your documentation, all the work will have already been done.


问:
Pattern for caching DAOs: strategy or decorator?
I'm building a php system with the Services/DAOs/Domain Models pattern, and now is the time to implement a caching system for the DAOs.

Would you use a decorator pattern, or maybe the strategy pattern?

What are the ups and downs of each one?

added requirement: Like I told in a comment answering to edalorzo I need to be able to use the DAOs without any caching at some moments. For the same method sometimes is acceptable to have cache, but some other times is not.

答: I've implemented it both ways in the past, and my experience has been that using a decorator results in a clearer separation of responsibilities between the two classes, as using a strategy requires support in the data access class, whereas with a decorator you write the data access class the same as you would if you weren't implementing cacheing.</p>