问:
Are there languages that allow goto statement to only go down?
goto statements can sometimes be useful to go down (to lower lines of code) in code, but can create a mess if used to go up (to higher lines of code). Therefore, I am wondering if there is any language that only allows goto statement to go down, e.g, godown.

Edit:

My main motivation for asking this question is this question Why does Go have a “goto” statement

I feel like if designers of a modern language like go decided to use goto statement there is a place for it. Also, as pointed out in one of the answers to the question, goto are used in go source code.

When I said "create a mess", I was referring to something like this, in a more complicated scenario:

```<code>package main

import "fmt"

func main(){
    i := 0
back:
    i++
    fmt.Println(i)
    if i < 10 {
        goto back
    }
    fmt.Println("we are finished")
}
</code>```

答:
I don't know of any [programming] languages that do this; if it's "bad" enough to allow goto at all then it will allow a goto to go anywhere.

I seem to recall a scripting language that only searched forward for the target of a goto statement but, sadly, I can't remember which one; it may have been a [very] early version of DOS.


问:
Continuous Integration for API wrappers
I'm setting up the Continuous Integration process for our API and I've split my brain trying to figure out how to connect that process with the API wrapper libs too.

Here's what I want to happen:

I push a change to the API

The tests are run to make sure that the API is green.

Temporarily deploy the API and test the following wrapper libs:

a. myapi-phpwrapper lib -- it has a test written in PHPUnit.

b. myapi-ruby wrapper -- with a test written in RSpec.

 4. If all of the above pass, then deploy the API. Otherwise, rollback.

If the above pass, then deploy to production. Otherwise, keep the previous version of the API.

That way, I can be sure that changes we push to the API don't break our wrapper libs. I can't be the first person to be doing this .. Ideas?

答:
I've done similar things in the past. In general that plan works though you certainly want to fix the end to be "if tests work deploy to production and re run external tests" rather than rolling back if you have the infrastructure to support it.

You also might want to think about versioning the api up front as a prophylactic measure.

In terms of tools, there are a number of CI platforms out there of various sorts. The best of breed are TeamCity (free for small scale / relatively cheap for more ) and Jenkins (FOSS). There are some cloud deployment services but I have not used any of them so I can't say what would work better. They tend to be tied to specific platforms and/or services so that will likely help you narrow the field.

The real work is in building your codebase to be 100% deployable by automated processes -- to a large extent the CI servers are just a fancy reporting layer for that action. Exactly how to get there really depends on specifics about the API and codebase but the place to start is "Can I build and deploy this from the command line without human intervention?"

ContentLicense: CC BY-SA 3.0

Title: Continuous Integration for API wrappers


问:
Programmatically extending Hibernate table/entity definitions in Spring, how?
I would like to know if there is a way —maybe with AOP— to extend table/entity definitions so that I can add functionality across the board that requires persistence.

Let me explain what I want to do with a concrete example. Let's suppose we have a simple ecommerce system:

User

id

name

password

Item

id

name

price

stock

ShoppingCart

id

user_id

ShoppingCartItems

id

shopping_cart_id

item_id

Order

id

shopping_cart_id

Purchase

id

order_id

invoice_number

Now, let's suppose that we think:

hey, wouldn't it be great if we could add a field to timestamp all entities?

You would now know things like:

When a User registered

When an item was created

When a shopping cart was created

When an item was added to a shopping cart

When an order was placed

When was the order was processed and paid

Now you have another great idea:

What if there was a status field for all entities so I could soft-delete or archive items, etc.

And yet another idea:

What if there was a text field called meta to serialize unimportant data, but that could help adding functionality without painful database modifications.

There are solutions I can think of (meeting relational integrity requirement):

Of course, you would need to go back, change your tables, modify your implementations so that when creating/updated you save the timestamp of then operation, same for status, same for meta.

And a somewhat convoluted but very modular way. Create some sort of plugin, that creates a table that handles the field, and 1 pivot for each entity. And through events, associate the extended data to the existing entities accordingly.

But what if... you could just tell Hibernate to extend the table definitions programmatically at run time, and add the persistence behaviors through something like AOP to inject functionality.

I actually did the first thing (programmatically extending table definitions outside existing implementation) in Doctrine successfully once, but I'm just starting to learn Hibernate. OTOH, I'm just starting to learn Spring and know the basics on AOP, so at least in my mind it is feasible to intercept Hibernate's configuration process where it understands table definitions.

So, is this possible with Spring and Hibernate?, how?, can you provide an example?

Addendum: The problem is that I need to do this more than once, and it could come from anywhere. AOP was my answer to the multiple inheritance problem since extending classes would be too constrained, however, in the past I did it with Events and worked just as fine... IIRC the problem is that I had to hack Doctrine in order to do this to fire an event at the time between when the model description was being converted from configuration files into actual models.

AOP is not a must, just an idea for an approach if such an event or dependency injection mechanism is not already in place. Another approach could be mixins, but I come back to AOP as probably the most elegant solution.

The main requirement here is to not modify the original model's code.

Disclaimer: I'm not an expert on AOP but I'm fairly familiar to the concepts.

答:
As far as I can tell, what you are describing can be trivially implemented with an Embeddable class and not need of AOP.

```<code>@Embeddable
class Audit {
  Timestamp created;
  String createdBy;
  String updatedBy;
}
</code>```

Then you can simply add your embeddable object within your entities.

```<code>@Entity
class Order {
   @Embedded
   Audit audit;
}
</code>```

Using inheritance, you might just as well make this available for a set of entities:

```<code>@MappedSuperClass
class MotherOfAllEntities {
   @Embedded
   Audit audit;
}

@Entity
class ChildClass extends MotherOfAllEntities {
}
</code>```

If the information being added is just audit information, as you seem to suggest in your post you may consider using something like Hibernate Envers which seems to be an implementation of what you're apparently suggesting with your question

```<code>@Entity
@Audited
class Order {
}
</code>```

And Envers will make sure to store audit information about the entity in some other data structure.

Since Envers seem to be an implementation of something like what you want to do from scratch, you might just as well give it a look to their code and see how they did it, but yet again, if it is to implement what you described, to me it sounds like killing a fly with a bazooka.

Body:
As far as I can tell, what you are describing can be trivially implemented with an Embeddable class and not need of AOP.

@Embeddable
class Audit {
  Timestamp created;
  String createdBy;
  String updatedBy;
}

Then you can simply add your embeddable object within your entities.

@Entity
class Order {
   @Embedded
   Audit audit;
}

Using inheritance, you might just as well make this available for a set of entities:

@MappedSuperClass
class MotherOfAllEntities {
   @Embedded
   Audit audit;
}

@Entity
class ChildClass extends MotherOfAllEntities {
}

If the information being added is just audit information, as you seem to suggest in your post you may consider using something like Hibernate Envers which seems to be an implementation of what you're apparently suggesting with your question

@Entity
@Audited
class Order {
}

And Envers will make sure to store audit information about the entity in some other data structure.

Since Envers seem to be an implementation of something like what you want to do from scratch, you might just as well give it a look to their code and see how they did it, but yet again, if it is to implement what you described, to me it sounds like killing a fly with a bazooka.


问:
Software Design: Generating Documents Of Different Formats and Different Layouts
I am currently prototyping a piece software which must be able to generate different types of documents in different file formats. The document could be a letter, or a receipt, for example, requested as a Word document, PDF, or both.

I am using a Word and PDF library which each have their own separate APIs. Each different type of document can have many different layouts and formats, including custom fonts, images, headers and footers.

I want the design to be as abstract as possible, and avoid exponential growth of concrete implementations for every new type of document, layout, and file format.

First, I think I should abstract the PDF and Word document APIs into some more generic API. I feel like the adapter pattern is most suitable. For example, I might have an interface called DocumentAdapter, with two concrete implementations called PdfDocmentAdapter and WordDocumentAdapter.

I then want to be able to build any type of document, in any format, with any layout. I've therefore started to design a DocumentBuilder class with two dependencies: a document adapter, and a document specification. The specification should define the layout of the document - and be file format agnostic.

The DocumentBuilder will have a concrete method called BuildDocument, which builds the document for the appropriate adapter and specification - then writes it to the filesystem.

The problem is that the APIs for building Word and PDF documents are so different I don't really know how to best solve this design problem.

I could have something like LetterDocumentInterface -> PdfLetterDocument, WordLetterDocument, StandardPdfLetterDocument, ShortPdfLetterDocument, StandardWordLetterDocument, ShortWordLetterDocument, but this would lead to a ridiculous number of concrete classes.

Any hints or experience from building similar designs would be immensely appreciated

答:
When faced with a similar challenge several years ago, I went with DocBook. There were translators available for generating PDF, Word, HTML and other output formats.

I had a number of difficulties with this approach.

DocBook itself is very much oriented to producing books. Other document types are a specialization, but you need all the cruft. No such thing as lightweight DocBook.

Producing other document formats from a docbook source used an XSLT transformation step. At the time, XSLT 2.0 processors were not wide spread and were tricky to work with.

The pre-packaged DocBook XSLT packages had been developed for the particular needs of the those developers. Hence, there were obscure and tricky things that had to be circumvented.

I eventually developed transformers to produce Word (OOXML) documents, PDF (generating Apache FOP) documents, and HTML. The results were quite good looking. However, by this time the train had left the station and the project could not afford to wait on me to develop these things to the point of robustness.

Looking back, I would suggest using a simplified document format, perhaps resembling DocBook, and use XSLT to generate the contents in the different formats. Then, injecting of style sheets would take care of the rest of your needs.


答:
There are only few cases that I think an only-go-down goto statement might be useful, but all can be done more elegantly via break and switch statements.

There is no need for Goto-down statement, in my opinion, and I'm not aware of any mature or experimental programming language with such an ability.

Title: Are there languages that allow goto statement to only go down?

Body:
goto statements can sometimes be useful to go down (to lower lines of code) in code, but can create a mess if used to go up (to higher lines of code). Therefore, I am wondering if there is any language that only allows goto statement to go down, e.g, godown.

Edit:

My main motivation for asking this question is this question Why does Go have a “goto” statement

I feel like if designers of a modern language like go decided to use goto statement there is a place for it. Also, as pointed out in one of the answers to the question, goto are used in go source code.

When I said "create a mess", I was referring to something like this, in a more complicated scenario:

```<code>package main

import "fmt"

func main(){
    i := 0
back:
    i++
    fmt.Println(i)
    if i < 10 {
        goto back
    }
    fmt.Println("we are finished")
}
</code>```


问:
How can I unit test audio?
I have inherited a small project and want to extend it and stabilize it at the same time by writing Unit Tests for all the new code I am adding. The first class, TypedAudioCreator, creates audio files and this turned out to be very easy to test first and write code for second.

However, when it came time to write TypedAudioPlayer, I had no idea how I could test it. It's a very small class focusing on the very basics of playing sound:

```<code>public class TypedAudioFilePlayer
{
    public event StartedPlayingHandler StartedPlaying;
    public event StoppedPlayingHandler StoppedPlaying;

    public readonly int TimeBetweenPlays;

    private Queue<TypedAudioFile> _playlist = new Queue<TypedAudioFile>();

    public TypedAudioFilePlayer(int timeBetweenPlays)
    {
        TimeBetweenPlays = timeBetweenPlays;
    }

    public void AddFile(TypedAudioFile file)
    {
        _playlist.Enqueue(file);
    }

    public void StartPlaying()
    {
        ThreadPool.QueueUserWorkItem(ignoredState =>
        {
            while (_playlist.Count > 0)
            {
                var audioFile = _playlist.Dequeue();

                if (StartedPlaying != null)
                    StartedPlaying(audioFile);

                audioFile.SoundPlayer.PlaySync();
                audioFile.SoundPlayer.Dispose();

                if (StoppedPlaying != null)
                    StoppedPlaying(audioFile);
            }
        });
    }

    public void StopPlaying()
    {
        if (StoppedPlaying != null)
            StoppedPlaying(null);
    }
}
</code>```

I'm still very new at TDD, but I realize the benefits of the practice and would like to try and get better at it. I have written Code first, no tests here, but that was just me being too lazy to properly think of the TDD way of solving it.  The question I have is, how should/could I test this class?

答:
Keep in mind that there is a difference between Unit Testing, which is the act of writing small tests that test individual units of your code, and Automated Test Runners which run your unit tests, usually as part of the build process or some kind of continuous integration system.

Unit testing is commonly automated, but may still be performed manually. The IEEE does not favor one over the other. The objective in unit testing is to isolate a unit and validate its correctness. A manual approach to unit testing may employ a step-by-step instructional document.

(http://en.wikipedia.org/wiki/Unit_testing#Techniques)

You can easily write a unit test to test that an audio player component plays audio correctly:

Make sure your speakers are working and the volume is turned up.

Go to /my/test/folder.

Execute myTestRunner audioPlayerTest.script.thingee.

You should hear Beethoven's 5th Symphony play for 15 seconds.

If you heard nothing, the audio played more or less than 15 seconds, or was distorted in any way, the test failed. Otherwise, the test passed.

What you can't easily do is include that test in an automated testing system. Automated testing is a particular implementation of unit testing, but it isn't the only implementation.

See also: https://stackoverflow.com/questions/1877118/is-unit-testing-always-automated

Title: How can I unit test audio?


问:
Explain MVC to non-programmers
I have a need to explain MVC to non-programmers.  Namely, to managers of other departments, in the context of progress report.  One of the things I do is refactor our codebase towards MVC separation.

What is MVC separation they might ask?
Why is it needed they might ask?

After reading a fairly technical answer like this:  What is MVC, really?, I am not entirely satisfied, since I will be talking to non-programmers.  They may nod their heads but they probably will not understand what it is and why it is needed.

In reality, I don't fully grasp what MVC is other than "separation of concerns, duties, functions, classes, blocks, tasks, things, in order to improve flexibility of making changes to the software".  Separating database from view and view from business logic using techniques like DI and OO tools and techniques is something I consider to be MVC separation.

So next time you are explaining MVC to a non-programmer who has background in sales and accounting for example, what would you tell them?

答:
You don't explain MVC.

What you do is explain that this is a restructuring of the codebase.

A restructuring that simplifies the codebase and therefore enables the developers to make faster and better changes to bug reports and feature requests, with less bugs.

They don't need to know the technical details, just why it was done, what was achieved by doing it and how the business benefits.

In other words - speak their language to them.

Title: Explain MVC to non-programmers


答:
in order to improve flexibility of making changes to the software

Managers are interested in the end result. The less technical they are the less likely they are concerned with how you get there. MVC is very common, popular and proven.

When change requests are made in the future, you can let management know they can be made easier and faster. Everybody likes to hear this.

It is a common strategy, so hiring new developers shouldn't present a problem. In fact, you may attract better developers who are strong proponents.

This is all going to be very difficult if there are many pressing issues in this particular project. They may not be wiling to take a step back so you can take two steps forward. On solution may be to wait or implement in smaller pieces.

Title: Explain MVC to non-programmers

Body:
I have a need to explain MVC to non-programmers.  Namely, to managers of other departments, in the context of progress report.  One of the things I do is refactor our codebase towards MVC separation.

What is MVC separation they might ask?
Why is it needed they might ask?

After reading a fairly technical answer like this:  What is MVC, really?, I am not entirely satisfied, since I will be talking to non-programmers.  They may nod their heads but they probably will not understand what it is and why it is needed.

In reality, I don't fully grasp what MVC is other than "separation of concerns, duties, functions, classes, blocks, tasks, things, in order to improve flexibility of making changes to the software".  Separating database from view and view from business logic using techniques like DI and OO tools and techniques is something I consider to be MVC separation.

So next time you are explaining MVC to a non-programmer who has background in sales and accounting for example, what would you tell them?


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

答:
Fluency should be something to strive for in the language you're using. I'm not sure if you differentiate knowing things exist and memorizing them. When someone says memorize I usually think of exact naming, syntax, etc. To me there is a greater difference between a developer who doesn't even consider to Google a capability of a language/framework to one who needs a "cheat sheet" or reference to actually write code than there is between the ones who need some reminders and those that have it all memorized.

Some people have better memories than others. Some put more effort into memorizing things, but I think a developer who has developed fluency with a language is demonstrating a lot more experience. If you claim to be a senior developer who is current working with a particular language, but you're not really sure how to write and IF statement, you'd be suspect.


答:
While I endorse the gist of Oded's answer, that you should explain technical projects to business managers in "their language," I have a qualm about "they don't need to know technical details, just why it was done."

If you are in a project or investment review meeting with department heads, and you declare "this is what we're doing," they may want to ask "Umm...why? That seems like a big investment of time and energy. Could you give us a little more understanding what you're doing and why?" That seems an eminently reasonable question. You don't want to be caught out in a position of "Well...it's complicated." or "No, I can't really explain it." or "Don't worry yourselves about it." The more senior the staff you're doing the project review for, the less likely they are to let "it's just stuff I can't explain well" fly. Better if you can go at least one level deeper to make others feel comfortable that 1) you know what you're doing and 2) this really is a reasonably-considered effort/investment.

So, have a sketch of MVC in your hip pocket, ready to go. Something like:

"It's a bit technical, but... MVC divides code into Models (responsible for the core data and business logic), Views (which displays the data), and Controllers (which handle user interactions and updates). It's a proven architecture--possibly software engineering's most successful "design pattern." I know it might like seem like "just some plumbing," but to handle all the requests coming in, we need a stronger foundation. This will put us on the right footing to build new features and resolve bugs faster."

Even if they don't fully understand MVC at the end of it, and even if you had to over-simplify to make it comprehensible ("break some eggs to make an omelet"), I wager you will find it much more comfortable to have an explanation ready than to have to say "I can't explain it" or "you aren't qualified to understand it" to senior managers.

Title: Explain MVC to non-programmers


问:
Repository pattern without entity framework?
Is it possible to implement the Repository pattern without using entity framework?

I am working on a small dev team of 3 on a small scale (for now) ASP.NET MVC solution.  I told my manager that I think that we need a layer of abstraction between our business logic and our controllers.  Right now our controllers contain a lot of the business logic that goes on, and other functionality, which I know is bad practice.

I have used the repository pattern in the past at my last position, but I did not implement it there. And I cannot say that I have 100% 'wrapped my head around' the pattern and would know how to implement it on my own.  There are so many tutorials out there for this pattern and they all seem to differ usually, even if it is a slight difference.

Another concern of mine is that the other programmer on my team (senior dev) said that we don't need to switch to EF right now. I think because our project is still small, and we don't have the time to make the switch.  I respect and understand this so I am wondering if it is possible to still use this pattern without EF.

If so, does anyone have useful tutorials, examples or references?

If not, what should I look to do next?  I don't want to show up empty handed.  I may just represent our (real world) objects as classes that store and provide functionality.  That way there will hopefully be less logic and functionality being performed in the controllers.

Thanks for reading.

答:
Design patterns like the Repository pattern aren't dependent on particular technology, so the obvious answer to your question is "yes, of course you can". You can write repositories which are ultimately backed by any storage technology - the point is that the Repository pattern is a way of structuring your data access code and separating it from your other code. It's described in such a way as to be independent of programming language - provided the language in question has objects and interface inheritance you can make it work.

What it isn't is a way of structuring your business logic and separating it from your view controllers, which is what your explanation actually says you need. For that you've got a number of other options available. Where I work we write a set of Service objects which provide business logic, and the controllers call those (they in turn have Repository objects injected by the DI framework to allow them to access data storage without having to know all the details). Controllers then only contain code which deals with processing incoming data from the UI into a form the services can handle, and turning service responses into something suitable for display to the user.


问:
Will a profound knowledge of C++ help you in learning other languages faster/easier?
Is there a possibility that someone who has learned all the key aspects of C++ and knows the fundamentals of the language very well, will learn other languages (such as, Python, Perl, Java) faster and easier?</p>

答: I would say that knowing any programming language deeply will help with learning others, at least other languages in the same family. For example, C++ knowledge won't help much with LISP or Haskell, but for object oriented procedural languages, it will. My approach has been to try and learn a language from each of the different families (procedural, functional, object oriented, etc.) of languages well, then that knowledge can carry over into other languages in that family. On the other hand, a deep knowledge of APL won't help with anything else...


问:
Would a statically typed alternative to JavaScript on webpages be practical?
Preference for dynamic and static typing is largely a matter of taste, and different people find them more or less suitable in different situations.

My question is, would it be technically possible to have a statically-typed alternative to JavaScript for client-side web page augmentation etc.?

答: Most of the benefits of statically-typed languages are realized at compile time.  If the language is going to be interpreted on the client, then a good deal of those advantages are lost.  If you compile them on the server, then you need to figure out how to get them loaded and run on the client (think ActiveX controls).  You could go with a hybrid approach (compile to some intermediate tokenized form), but then you're basically back to Java applets.</p>

ContentLicense: CC BY-SA 2.5


答:
No. Knowing C++ well will make it easier to learn other languages which are like C++. But that would be boring. Why would you want to know two languages which are the same? That doesn't buy you anything. (Note that this isn't specific to C++. It applies to any language. Compare with natural languages: learning Italian will not make it easier to learn other languages. It will make it easier to learn Spanish, French or Portuguese, and learning three of those will make it easier to learn the fourth, but even learning all Romance languages will not help you one bit learning Finnish, Chinese, Hindi, Hebrew, Arabic, Pashtu, Greek, etc.)

Programming languages implement paradigms. Peter van Roy has collected a poster of the 34 principal programming paradigms.

Paradigms, in turn, are composed of concepts. That poster lists about 18 concepts.

All paradigms (or at least the ones in the poster) are composed from those concepts. All languages implement one or more paradigms composed of those concepts. So, if you learn those concepts, you will (in some sense) learn every programming language at once.

There are of course still lots of language-specific quirks (syntax among them). Also, that poster completely ignores typing, and there is of course a significant difference between a System F<:ω-style type system, a Scala-style type system, or a dynamic duck-typed type system, let alone a dependent type system à la Idris, Agda, Coq, Guru, or ATS.

Title: Will a profound knowledge of C++ help you in learning other languages faster/easier?

Body: Is there a possibility that someone who has learned all the key aspects of C++ and knows the fundamentals of the language very well, will learn other languages (such as, Python, Perl, Java) faster and easier?</p>


答:
Unfortunately, no.  This is actually a big problem when teaching C++ in programming classes, especially beginner-level classes: you can learn programming principles, or you can learn the C++ language, but the C++ language has too many pitfalls and stupid little gotchas to learn both effectively within the scope of a one-semester class!

There are many things that were tried in C++ and eventually became idiomatic in the language through sheer inertia, that pretty much every other language since has looked at and rejected because it turned out to be a huge mistake.  The major ones are C++'s Templates and C++'s object model (objects as value types is never a good thing, because it breaks Liskov substitution and thous OOP itself.)

So if you get a deep knowledge of C++ first, and then you want to learn other programming languages, there's going to be a whole lot of harmful and counterproductive crud you'll have to unlearn in order to be effective at other programming languages.


答:
Every language you learn makes the next one easier to learn, especially within the same paradigm.  They borrow from each other heavily, and after a while you get to the point where a new language looks like merely a collection of features from other languages, with a few unique twists.

I don't think C++ is better or worse than any other language in that regard.  It is one of the more verbose languages I know, and makes some things hard that are easy in most other languages, but that doesn't necessarily translate into making the next language easier to learn.  It does generally make the next language feel easier to use, though.  There's a difference.


答:
In General Terms

Profound knowledge of any programming language is likely to help you with picking up other languages quicker. That is so partially because programming is a way of thinking more than it is learning syntax. Most programming practices would be true of most other programming languages. That is why people say that you always learn the second programming language quicker than the first.

In Specific Terms

C++ is a language which allows for a much greater 'control' than other languages. This can be both good and bad. For example a real gun is better than a air gun when you go in the forest (because you can actually shoot something down with it), but it is also more dangerous for you and those around you. C++ has concepts such as memory management, which you don't need to worry about in most other languages. Having a firm grasp of these concepts however is far from pointless even when working with auto memory management languages. You can also perform bitwise operations and go much lower down to the 'metal' with a language like C++. Even a basic understanding in those areas can help you a lot as a programmer in any language.

Personal Opinion

I personally learned Java first for nearly 3 years before learning C++, and I should say that I sort of regret it (uni course...). This made learning C++ (i'm still learning - far from 'profound' knowledge :D) not as straightforward as i'd like it. If I had done it the other way around I would say that it would have been much easier for me. Especially when you know WHY something is done in a specific way, and not just rest assured on the auto-magicness of the language. If I am allowed to give an opinion I would suggest: C (for basic and universal programming paradigms) -> C++ (for basic understanding of OOP along with memory management) -> Then you can go into any 'real' OOP language with a good foundation or you can continue with more advanced topics in C++, all depending on what you're planning on doing.


问:
Finding a solution for interleaving multiple data messages into one network stream (in QT framework)
I need to find a solution for high throughput network data interleaving in my QT framework based windows application.

My application is based on server/client architecture. The client connects to a server and both can exchange messages of arbitrary sizes. A message is just a piece of data, with a header describing the message.

The following limitations must be considered:

Message size is known in advance on sender site (on receiver site after receiving first message header)

There is only one network stream (one opened port)

Connection oriented protocol (TCP/IP) if possible

Wireless network connection (WLAN) (typically 5-10 MB/s net throughput)

Messages should be sent as fast as possible

Large messages may not block smaller messages

Because there can only be one data stream (per connection), I decided to use data interleaving so that one large message can not block several smaller messages. For this purpose I allow message fragmentation and introduced a 3 class message priority. The message header contains the total size, payload offset and payload size so that assembling of an interleaved message is possible on receiver site.

Example of large messages:

Video stream

Firmware update

Examples of small messages:

Status messages (workload, battery status, ...)

Command messages

A naive approach would be to send away all the messages in a row. However this would block smaller high priority messages for certain amount of time because video stream data messages for example utilizes the network connection to the full. This would not be acceptable. Instead of sending a message in one part, I decided to split all messages and interleave them in 8 KB large chunks. Although I have no good idea how I can optimally interleave the individual message fragments into one another, I think there are several sheduling strategies that can be used here. However, this is not my problem.

```<code>+----------------------------------+
| Message                          |
+--+-------------------------------+
|  | Header 1                      |
+--+-------------------------------+
|  | Payload (max. 8 KB)           |
+--+-------------------------------+
| ...                              |
+--+-------------------------------+
|  | Header N                      |
+--+-------------------------------+
|  | Payload (max. 8 KB)           |
+--+-------------------------------+
</code>```

Guess, at a certain point in time, there is a single very large message that has to be sent. There is no other message in output queue! So there is nothing to interleave here. A naive approach would be to send that message away. However guess, shortly after beginning to send this large message, several high priority small messages gets queued. However since the large message was already sent to the socket, these new small messages has to wait until the large message has been sent. This case should be prevented!

I had many different ideas, but none of them was satisfactory.

One idea was to provide only just so much data on the socket that it does not run out of data, but not so much that too many data is in the socket buffer. This would ensure that at any time smaller data can be inserted into the data stream without major delays. The problem is, how can I find out how much data is optimal so that the throughput is still  at maximum? How can I get informed that data has been sent and how much data is left in socket buffer?

Did I miss something? Is there even a better solution? I'm open for new/better ideas.

Edit: Some more clarification

The idea to fill priority queues and insert higher priority chunks in front of the low priority chunks was also considered by me. The problem which occurs is as follows:

I have to hold back data chunks in a queue and may not send it to the socket too fast. Then, once in the socket buffer I've no more control over it. This can lead to the situation where there is enough data to send, but the socket runs out of data and is idling. This will decrease the data throughput. How can I determine the optimal fill state of the socket buffer so that the socket doesn't run out of data? The problem may even worser, as far as I know, there is no possibillity to get informed about the state of the buffer, neither if all data has been sent out, nor how much data is left in the socket buffer for sending.

答:
You are splitting the large messages up in chunks if you have multiple messages to send, so that that one large video doesn't interfere with other high-priority messages.

This means that the receiver must always be able to receive large messages in multiple chunks.

As the receiver will be expecting large messages to be split into chunks anyway, why not split all messages into chunks, even if there is nothing else to send? Then the sender can have one (or more) processes filling a (priority-)queue of chunks to be sent and one process that checks if there are chunks in the queue and sends the first one.

If you start a large transfer, the queue will be filled with a large number of low-priority chunks. If halfway through there is a high-priority message (or rather chunk) to send, then that would be placed in the queue in front of the low-priority chunks and get sent out at the next opportunity.

The maximum delay that the high-priority message gets is the time it takes to transfer one full-size chunk.


答:
I would tell them that MVC separates my concerns.

My first concern is the code logic - what I do behind the scenes to make the website perform the actions they want it to.

My second concern is the business logic - what they (the user) want the application to do.

My third concern is how the site looks - The page they visit to do what they want.

(I woudln't tell them this next part) So, in order, my explanations were for the Model, Controller, and View.

Title: Explain MVC to non-programmers


答:
I'm going to take a different interpretation here.

C++ is a good language for learning about memory and data structures, since it forces you to think carefully about concepts like object ownership and lifespan.  And learning about memory and data structures is one of the essential parts of any software engineering curriculum.

But you have to be learning about data structures.  If you just "learn C++," you won't really gain very much, and it may slow down the process of learning other languages in the short run.

When I was in college, Data Structures was a required freshman-level course, taken after CS1 (which at least half the class had placed out of).  It was hard.  The format of the course was basically "Here's a data structure you've never seen before [e.g. a leftist heap].  Implement it in C++ in a week and make your output match our expected output exactly, byte-for-byte, so we can grade it automatically."  Then, next week you'd get a new data structure and a new assignment.  I probably learned more in that one course than I did in all of my other courses combined.

That's the kind of learning you need to be doing for C++ to be valuable.


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

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

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

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

Just to clear things out:

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

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

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

Absolutely. The use of real-time threads would be a good example of a situation in which this is, not only possible, but the desired way (and often, the only correct way) to get the job done. However, real-time threads are usually limited to the OS kernel, usually for drivers which need to be able to guarantee that a hardware event of some sort is handled within some defined period of time. You should not have real-time threads in normal user applications and I'm not sure that it's even possible to have one in a Windows user-mode application. Generally, operating systems make it intentionally impossible to do this from user land precisely because it does allow a given application to take over control of the system.

Regarding user-land applications: Your assumption that checking for a given number of threads in order to run is necessarily malicious in intent is not correct. For instance, you could have 2 long-running, performance-intensive tasks that need a core to themselves. Regardless of CPU core speed, sharing a core with other threads could be a serious and unacceptable performance degradation due to cache thrashing along with the normal penalties incurred by thread switching (which are pretty substantial.) In this case, it would be perfectly reasonable, especially for a game, to set each of these threads to have an affinity only on one particular core for each of them and then set all of your other threads to not have affinity on those 2 cores. In order to do this, though, you'd have to add a check that the system has more than 2 cores and fail if it doesn't.


答:
Model - Wires/electricity

View - Light Fixture

Controller - Light Switch

Relatively easy to switch out components (light fixture, light switch/dimmer).  Maybe not so much the wiring, but still can be done without effecting other components.  Everyone should be able to visualize that in thier head, even marketing/sales types.  (Clear separation, etc.)

Now tell your boss/co-workers that in our application/system the switch is embedded inside the light fixture and the light fixture is wrapped around in copper wire.  Now imagine trying update the light fixture, switch, or wire.  Very costly, impact to other components very high and perhaps dangerous to do without breaking something else.

MVC is applying a pattern to the code base which turns the jumbled (but working) mess into something where changes can happen faster and easier with less errors.

Title: Explain MVC to non-programmers


答:
An easy way to understand MVC: the model is the data, the view is the window on the screen, and the controller is the glue between the two.

The best rubric ever: "We need SMART Models, THIN Controllers, and
  DUMB Views"

As with other software design patterns, MVC expresses the "core of the solution" to a problem while allowing it to be adapted for each system. It is better seen as a concept instead of a specific implementation. There are many implementations of the concept.

All MVC variants use the same division of components, but usually they differ in how those components interact.

For those of you who are not aware, MVC was originally described in
  terms of a design pattern for use with Smalltalk by Trygve
  Reenskaug in 1979. His paper was published under the title
  "Applications Programming in Smalltalk-80: How to use
  Model-View-Controller", and paved the groundwork for most future MVC
  implementations.

Title: Explain MVC to non-programmers


问:
Release cycle as a game
I thinking about how increase developer's commitment to release and create release cycle as a game for more fun in office.

Current suggestions

Building tower from bricks

Climbing on some hypothetical everest, where each camp is release stage

Some badges system, like StackExchange system

答:
Hudson supports something similar

Take a look at the Hudson continuous integration server. It has plugins that support games to that developers get points for successful builds, but lose badly if they commit a broken build.

Might be better than building towers...

Title: Release cycle as a game


答:
Any code using spinlocks with any noticeable amount of lock contention will perform terribly (to an extent where -- for an application like a game -- you can say "doesn't work") if the number of threads exceeds the number of cores.

Imagine for example a producer thread submitting tasks to a queue which serves 4 consumer threads. There are only two cores:

The producer tries to obtain the spinlock, but it is held by a consumer running on the other core. The two cores are running lockstep while the producer is spinning, waiting on the lock to be released. This is already bad, but not as bad as it will get.

Unluckily, the consumer thread is at the end of its time quantum, so it is preempted, and another consumer thread is scheduled. It tries to get hold of the lock, but of course the lock is taken, so now two cores are spinning and waiting for something that cannot possibly happen.

The producer thread reaches the end of its time slice and is preempted, another consumer wakes up. Again, two consumers are waiting for a lock to be released, and it just won't happen before two more time quantums have passed.

[...]
Finally the consumer that was holding the spinlock has released the lock. It is immediately taken by whoever is spinning on the other core. There is a 75% chance (3 to 1) that it's another consumer thread. In other words, it's 75% likely that the producer is still being stalled. Of course this means that the consumers stall, too. Without the producer sumbitting tasks, they have nothing to do.

Note that this works in principle with any kind of lock, not just spinlocks -- but the devastating effect is much more prominent with spinlocks because the CPU keeps burning cycles while it achieves nothing.

Now imagine that in addition to the above some programmer had the brilliant idea to use a dedicated thread with affinity set to the first core, so RDTSC will give reliable results on all processors (it won't anyway, but some people think so).

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

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

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

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

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

Just to clear things out:

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

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


答: No.  A profound knowledge of C, or of the subset of C++ that is "basically C", will help when learning other languages, at least when picturing what their implementations have to be doing.  A profound knowledge of C++, on the other hand, will mostly be information that is confusing or irrelevant when learning other languages; if you learn C++'s object orientation at more than a surface level, for example, when you move on to Perl/Python/Ruby you'll spend more time un-learning what you know than using it, since objects in those languages are implemented in a completely different way than in C++.  The same thing is true to a greater or lesser extent for most other languages, and for other language features (for example, C++ templates and Haskell polymorphism are basically the same feature, with, again, completely different implementations).  So deep knowledge of C++ is mostly going to be C++-specific.


问:
How to support different API Versions
I am writing a Rest API and am wondering how best to handle supporting the different versions. By this I don't mean how to define a URI as V2 or V3, but rather how to structure the code given that it'd need to:

Support multiple versions at the same time, eg. V1 & V2 & V3 URIs must be live at the same time. I would retire V1 when say V4 comes in in order to constrain the amount supported at any one time.

Avoid as much code duplication as possible

Make it easy to add non-breaking changes to a version, without it impacting on other versions

It would seem that there are few approaches that could be taken:

Use Git to control the versions, with a branch for the different versions (and old versions  essentially having no new development work done on it). This would mean no code duplication as only the latest version is in the code, but previous versions would need to work with the new version of the DB until they are retired.

Duplicate code so that each version is handled in the same application and has a totally separate code path, but this would mean a lot of duplication

Re-use a lot of code across the versions, but this would make it harder to maintain as changing one version is more likely to impact a previous version

Is there any best practice to deal with this problem as all options seem to have their own problems?

答:
A combination of using GIT release branches (or fork each version into a separate repository) to support and maintain old API versions and possibly have some re-usable code that can be shared as a dependency, like a commons library, is most likely the way to go. So each API version would be a separately deployable artifact. This allows flexibility so that, for example, API V1 can depend on commons V1, while APIs V2,V3,V4 can depend on commons V2. This would be the easiest and cleanest from a development perspective as your code base doesn't multiply with each new version, instead every version is isolated into it's own project/code base and is only concerned with itself.

Another reason to deploy separate artifacts is that there may be cross cutting concerns like a security mechanism, or frameworks/libraries like your dependency injection framework that could change in newer versions of the API and would create a lot of difficulty in supporting older APIs if they all live in the same code base (and Classloader at runtime, if it's Java).

Every approach, whether its a branch per version or monolithic duplicated code base, is always going to have the issue of a common integration point (like the DB, or a distributed cache schema) needing to change. Older version APIs may require some kind of maintenance to work with those changes, or the introduction of some other tools (like database views) to help compatibility. This may be an unavoidable difficulty depending on the nature of your changes.

Title: How to support different API Versions


问:
Limiting complexity in JPA programs Java/Hibernate
I've been working on a new application for some months now. It's my first big JPA program, and is still in the early stages. However, the complexity of my JPA object tree is becoming a growing problem.

For instance I have an object tree of Users, who have Companies, Divisions, Related Divisions, Currencies, Local Currencies, Currency Exchange Rates. At lower levels, things are not so bad. For instance if I pull out Currencies, matters are easier to cope with. But for high level, tree topping elements like a list of Users, my object tree has become horrific.

Now, a small bug has slipped into the JPA object tree meaning an important join table is being overwritten by one of my entities. I hope I can find the bug, but what I'd like advice on, is how to avoid this rapidly burgeoning complexity.

Thanks!

答:
This isn't really a direct answer, but you might consider skipping complex object hierarchies altogether and just do straight SQL in the more complex cases.

I wrote a simple ORM called Norm because I didn't want to incur all of the overhead of JPA. The theory is that you do the dead minimum necessary to get data in and out of the database. If your ORM can't handle what you need to do, do it in SQL.


问:
Would Python be too slow for client-side use in Browsers?
I've heard the statement that Python would be too slow to be of any use in browsers.

I reckon Javascript is only superior in this aspect because of companies like Google who need it fast (and made it fast) because they need it to survive, but I could be wrong.

Are there any differences in how Python and Javascript are designed that have an impact on how they (would) perform in browsers?

Since as of now there isn't a client side Python implementation, my question comes from the statement someone made, so maybe it has something to do with the languages itself (although I don’t believe that).

答:
It depends on the implementation of the language and not necessarily the language itself.  Most JavaScript interpreters are much faster than nearly all implementations of Python.

This doesn't mean that the Python language can't be used at nearly the same speeds as JavaScript.  Opal implements almost the full Ruby language and standard library in the browser by compiling Ruby code into JavaScript code wrapped in closures.  Putting aside the overhead of including the Opal library, its speed is far closer to that of straight JavaScript than any other Ruby interpreter that I know of.

I don't know if there is a Python equivalent of Opal, but such a project would probably mean that the answer to your question is "no".  With the increasing use of JavaScript as an "assembly language for the web", it wouldn't surprise me if it will be used more and more as a platform for other languages, especially as mobile computing power increases and the overhead of having a language implemented in JavaScript becomes increasingly negligent.

EDIT: Here is a list of Python implementations for the browser that compile/run on JavaScript.

https://github.com/jashkenas/coffeescript/wiki/list-of-languages-that-compile-to-js#python

And in case you are interested, you can check out Opal, which I really like.

http://opalrb.org/

Since I doubt that browsers are ever going to come with support for separate interpreters, such compilers are probably the way of the future in terms of using languages other than JavaScript.  Even now, you will get comparable performance in most areas.  This is my opinion, however, so keep that in mind.


问:
Storing Dates & Times in SQL
I have a tool that I am working on which allows people to create meetings. They can select the Date, Time & Timezone that this meeting is occurring in.

I need to determine the best way to store that data in SQL.

The end goal is that a user can click on "View Meetings" and it will show them all of the meetings. However, they also need to be able to select the timezone that they want to view the meetings in and that needs to changes the times to reflect what they have picked.

What is the best approach to accomplish this?

答:
As a person who has had to fix timezone issues, ALWAYS store the data in UTC.  Translate it to the timezone they selected for display only.

If SQL Server allows you to store the UTC offset as part of the datetime, then it's not as big of a deal. However, in MySQL, the date is just stored absent of timezone context. Here's a case in point, which I hope I can make to make sense :)

The user saves a time 2:00 EST for the day DST is removed. Then, let's say you store it in Central time, so it gets stored as 1:00 CST (but the CST part is not stored in MySQL). When the code pulls it out as 1:00am, it just assumes DST is still active because it hasn't been forced back. Then, for display, you change it back to the user's timezone, which bumps it ahead one hour to 2am EDT. However, at 2am EDT, the timezone gets bumped back to 1am EST. So, the user entered 2am EST but resulted in 1am EST on the way out.

I am trying to fix some legacy code with exactly this problem. In our logs, because of this issue, when we try to graph out that timeframe, we end up with 3 hours of 1am-2am instead of 2 for the transition and the 2am - 3am time is completely lost for eastern timezone. Problems only occur twice a year, and the transition away from DST is the worst...but it never would have been a problem if the times had been stored in UTC since UTC does not have any time changes, ever.

This may not be a problem you will encounter in your use-case, but, IMHO, it is best to just always store in UTC and translate on the view, as this has no drawbacks that I am aware of for any kind of application that has to deal with multiple timezones already.

One small exception, however...let's say that you are storing a recurring event. It happens at 18:00 Eastern on Tuesdays all year. For such an example, if you stored it as 23:00 UTC...part of the year, the event would run at 19:00. So, for such an example, you just want to deal with the timepiece separate from any date context.

[UPDATE] PHP timezone parsing

```<code>$dataFromDb = $row;
// get it from DB and assume DB is in UTC
$myDateTime = new DateTime($row['dateTime'], new DateTimeZone('UTC'));
// convert it to desired timezone
$myDateTime->setTimezone( new DateTimeZone('America/Chicago') );
echo $myDateTime->format('c'); //format in ISO-8601 format (very transportable)
</code>```

You can then send the ISO format to JavaScript. However, JavaScript will take whatever time you send it and display it in the user's LOCAL timezone. So, if you want to display Eastern time even to someone in Central time, you'll need to force it into the Eastern timezone in JS. This is a PAIN without momentjs. So, if you don't want it to display the time in the user's local time, and you don't want to add momentjs to your project, then it is MUCH easier to just handle all the conversions in PHP and send the string as you want it formatted from PHP and don't let JS mess with it.

To Save to DB, same process:

```<code>//get selected timezone and date/time from user:
$dateTime = new DateTime( $_POST['dateTime'], new DateTimeZone($_POST['timezone']));
$dateTime->setTimezone( new DateTimeZone('UTC') );
$dbColumn = $dateTime->format('Y-m-d H:i:s');
</code>```

NOTE: I'm not doing validation in the above for simplicity and assuming the DateTime gets created correctly...don't actually trust that data from user will actually become a valid date.

Title: Storing Dates & Times in SQL

Body:
As a person who has had to fix timezone issues, ALWAYS store the data in UTC.  Translate it to the timezone they selected for display only.

If SQL Server allows you to store the UTC offset as part of the datetime, then it's not as big of a deal. However, in MySQL, the date is just stored absent of timezone context. Here's a case in point, which I hope I can make to make sense :)

The user saves a time 2:00 EST for the day DST is removed. Then, let's say you store it in Central time, so it gets stored as 1:00 CST (but the CST part is not stored in MySQL). When the code pulls it out as 1:00am, it just assumes DST is still active because it hasn't been forced back. Then, for display, you change it back to the user's timezone, which bumps it ahead one hour to 2am EDT. However, at 2am EDT, the timezone gets bumped back to 1am EST. So, the user entered 2am EST but resulted in 1am EST on the way out.

I am trying to fix some legacy code with exactly this problem. In our logs, because of this issue, when we try to graph out that timeframe, we end up with 3 hours of 1am-2am instead of 2 for the transition and the 2am - 3am time is completely lost for eastern timezone. Problems only occur twice a year, and the transition away from DST is the worst...but it never would have been a problem if the times had been stored in UTC since UTC does not have any time changes, ever.

This may not be a problem you will encounter in your use-case, but, IMHO, it is best to just always store in UTC and translate on the view, as this has no drawbacks that I am aware of for any kind of application that has to deal with multiple timezones already.

One small exception, however...let's say that you are storing a recurring event. It happens at 18:00 Eastern on Tuesdays all year. For such an example, if you stored it as 23:00 UTC...part of the year, the event would run at 19:00. So, for such an example, you just want to deal with the timepiece separate from any date context.

[UPDATE] PHP timezone parsing

$dataFromDb = $row;
// get it from DB and assume DB is in UTC
$myDateTime = new DateTime($row['dateTime'], new DateTimeZone('UTC'));
// convert it to desired timezone
$myDateTime->setTimezone( new DateTimeZone('America/Chicago') );
echo $myDateTime->format('c'); //format in ISO-8601 format (very transportable)

You can then send the ISO format to JavaScript. However, JavaScript will take whatever time you send it and display it in the user's LOCAL timezone. So, if you want to display Eastern time even to someone in Central time, you'll need to force it into the Eastern timezone in JS. This is a PAIN without momentjs. So, if you don't want it to display the time in the user's local time, and you don't want to add momentjs to your project, then it is MUCH easier to just handle all the conversions in PHP and send the string as you want it formatted from PHP and don't let JS mess with it.

To Save to DB, same process:

//get selected timezone and date/time from user:
$dateTime = new DateTime( $_POST['dateTime'], new DateTimeZone($_POST['timezone']));
$dateTime->setTimezone( new DateTimeZone('UTC') );
$dbColumn = $dateTime->format('Y-m-d H:i:s');

NOTE: I'm not doing validation in the above for simplicity and assuming the DateTime gets created correctly...don't actually trust that data from user will actually become a valid date.


问:
Recommending new products using k-means clusters?
I'm trying to figure out the best way to recommend images based on past classifications using k-means clusters. What I have done is mapped the RGB values of a set of images, performed a k-means cluster analysis on those RGB values, and attached a "rating" to each image. This has created Voronoi cells similar to this graph. I've stored the cluster centers and ratings into my "training set".

The next step is to take a new image and make a recommendation based on the training set previous images. I'm not sure how to proceed. Would I want to implement a Collaborative Filtering process? Or do I need to perform more processing on the data?

Not sure if it matters but I'm using Apache Spark for the project. Thanks!

Edit: Collaborative filtering is probably not the best way to proceed, since the features being compared for products uses more than just ratings. I need to compare the similarity. I'm guessing this would involve heavy matrix operations?

Edit2: Some feedback here would be awesome. What I'm thinking is training two datasets (a rating of "yes" images vs. "no" images) and then using Spark's computeCost function to develop a value for variance/bias of an image being compared. The final step would then compare whether the image was more similar to the "yes" dataset or "no" dataset, and then make a final recommendation. I'm new to machine learning, so I could be over-thinking it.

答:
Thought I'd answer my own question here with my final solution. I realized that although I could get some partially meaningful data from k-means clustering of RGB values, the problem was that the recommendation was not based on meaningful characteristics of the image. It could potentially be useful in the future for other aspects (such as lighting or "brightness").

The final solution was using EigenFaces for image learning. The article below was the most helpful for me to understand the basics and get going:

https://chimpler.wordpress.com/2013/04/17/generating-eigenfaces-with-mahout-svd-to-recognize-person-faces/


答:
I halfway-agree with Oded - learning how to convince your non-technical peers and managers that what you are doing is important and useful, without explaining the gritty details, is a necessary skill that you would be wise to develop.

However, I believe that having the ability to explain complex concepts in simple terms actually helps with that - one problem non-technical managers often have is that since they have trouble understanding exactly what it is the tech folks are doing, they have a tendency to mistrust them. It's just human nature: it's easier to believe that something you don't understand is useless or wrong than it is to put your faith in it. Therefore, if you can make concepts easy to understand at will, you can use that whenever you need it, and over time, your non-technical managers will learn that they can understand it if they want to - you aren't pulling one over on them - you are just sparing them the details for their own sanity. They will trust you more.

That aside, let's answer the question:

I find it useful to use analogies that business people understand. For MVC, I describe it as a business.

Models are responsible for the business logic and data - they are the things that define what the program does and the details of how it does it. If the program were a business, then models would be the warehouses, factories, workers, and capital equipment. They would also be the lower-level managers that make sure rules are followed and policy is enforced.

Views are the presentation layer - they show users what is going on in the system and provide a means for interacting with the program. If our program were a company, views would be like the showroom floor, the sales booth at the trade convention, or the sales reps. They display options, provide user-friendly information and feedback, and take requests back to the company.

Controllers are the coordination layer - they make sure that information gets from the models to the views and vice versa, and that everything works together to do their job. If the program were a company, then controllers would be the mid- and high-level management. They don't get too involved in the details, but they make sure that the right people have the right tools to do their jobs, and they approve or deny high-level decisions. They provide the overall direction so that things can work together.

The benefit to explaining it with this analogy is that a good manager will see the wisdom in separating concerns this way, and might decide that they should be more controller-like, and not get too involved in the details in the future!

That hopefully will answer the "what" - the "why" is also easy with this analogy:

Imagine an ideal company, where each department is responsible for one set of tasks, and knows that it will always have the resources it needs without having to worry about what others are doing. A sales rep finds a customer, gets their order, passes it back to management who approves, and then it goes to the warehouse/production/engineering for fulfillment. Feedback is direct - no one else needs to get involved unless there is a problem. That's a good MVC design - each part has a job, and it doesn't have to worry about other parts. That way, it's easy to change if we need to. In a non-MVC design, responsibilities aren't clear. The sales rep may try to modify the product when the customer asks for something different. Or they might give different prices depending on how they felt that day. The CEO might be down on the factory floor messing with the production line, when he should be worried about how to get a more reliable supply chain in place. The warehouse workers might be out on the sales floor talking to customers when they should be fulfilling the orders that were already taken.

In other words, good MVC design lets each part focus on one thing, so that it can do it right - just like a well-organized company.

**Disclaimer - if your company is poorly organized, they might get offended by this. In that case, you will need another analogy. You might also need a different job.

Title: Explain MVC to non-programmers


问:
Identifying user of a webapp
For a small webapp I want to be able to identify a user.

The issue is, if the user cheats in some manner, I want to be able
to forbid him the usage of my application.

I already have done extensive research on that topic, but to no avail.

What is not an option:

cookies: can be disabled or deleted (to easily overridable)

ip adress: is dynamic or hidden through NAT

MAC adress: cannot be retrieved (as far as I know, or at least not without terrifying the user)

Flash: is not supported on a wide range of devices

This is a central part, so I can decide which technology to use along
this decision.

What are the options I have? (especially I do not want that the user gets a security warning as this is often the case with java applets)

Some kind of unique machine identifier would be okay.

-edit-

What I want to protect against:

It should be some kind of discussion board about recent topics.
There will be people that proof that they have not the attitude to discuss in the public, e.g. insult other people etc.

And I want to have a possibility to keep these people away.

There will be somekind of login. But what keeps people from creating simply another account and keep going?

Fingerprinting seems to be to weak, as is has some well known flaws. Using a bank account is way to hard to use, at least since there is nothing you can buy right now, it will keep users from using the site.

Isn't there something in the middle? Like a device id, which keeps users from one device away, despite it might be wrong, this would do the job in a vast majority of cases I believe.

答:
There is no 100% solution to your problem.

A user can use a wide range of strategies to bypass any possible action: delete cookies, use a VPN, even change its device (mobile phone, pc, laptop). You mentioned them.

What are the options I have?

That said, IP adress and a browser fingerprint is your best bet. There are libraries for JAVA (and probably C#) that can extract a fingerprint from the HTTP Request. But again the user could just change his browser signature or switch to another device.

Therefore is might be helpful to change the idea behind it from "completely locking a user out" to "making it hard for a user to gain access."

Edit:

"A device id" would basically be the MAC address, which can not be obtained via a HTTP request. So no, there is no device id you could use. A device fingerprint is the closest thing you can get.

An Idea:

You could use a mobile app to authenticate a user. An app could obtain a device id (MAC address or IMEI for Android). Use the device id from the phone to register a user. You'll only need the mobile app for registration. If you ban a user, he won't be able to register again using the same phone. (This is of course not 100% reliable, but more reliable than anything browser based)

Title: Identifying user of a webapp


问:
Why do browsers clamp timeouts and intervals?
I've been working on some abstractions of setTimeout and setInterval in order to process large sets of data without blocking the event loop in the browser.

Upon this, I have discovered that browsers "clamp" the number of milliseconds you specify for a timeout or an interval.

For example, if you write setTimeout('do something', 0), the browser will actually do setTimeout('do something', 4).  In other words, the browser will force a minimum of 2 to 10 milliseconds, depending on which browser and version your code is running on.

This can have a big impact on the amount of time for a callback/promise to wait.  There are ways around this (for timeouts, anyway), such as using postMessage or MessageChannel (poor solutions, in my opinion); they can provide the same effect as a timeout while effectively having 0 wait time.

Why do browsers clamp timeouts and intervals this way?  I can find no reasoning for this, official or otherwise.  My only hypothesis is that it somehow allows room other tasks to be added to the call stack, but this doesn't seem to be an issue with postMessage as far as I can tell, so beyond that I can't think of any reason why this helps anyone.

答:
I cannot tell much about the reasons why, but following some "official" documentation that can help on your investigation of the subject.

Above all the HTML 5 Spec mentioned below may contain interesting hints to continue the investigation.

The MDN Documentation on setTimeout says:

Minimum/ maximum delay and timeout nesting

Historically browsers implement setTimeout() "clamping": successive
  setTimeout() calls with delay smaller than the "minimum delay" limit
  are forced to use at least the minimum delay. The minimum delay,
  DOM_MIN_TIMEOUT_VALUE, is 4 ms (stored in a preference in Firefox:
  dom.min_timeout_value), with a DOM_CLAMP_TIMEOUT_NESTING_LEVEL of 5ms.

In fact, 4ms is specified by the HTML5 spec and is consistent across
  browsers released in 2010 and onward. Prior to (Firefox 5.0 /
  Thunderbird 5.0 / SeaMonkey 2.2), the minimum timeout value for nested
  timeouts was 10 ms.

In addition to "clamping", the timeout can also fire later when the
  page (or the OS/browser itself) is busy with other tasks.

To implement a 0 ms timeout in a modern browser, you can use
  window.postMessage() as described here.

Browsers including Internet Explorer, Chrome, Safari, and Firefox
  store the delay as a 32-bit signed Integer internally. This causes an
  Integer overflow when using delays larger than 2147483647, resulting
  in the timeout being executed immediately.


答:
Benefits of MVC is primarily in the separation of concerns:

It lets people concentrate on what they do best.

```<code>Database admins develop the model
Programmers write the controllers
and Graphic designers can design the views.
</code>```

It lowers costs because intertwined systems require staff to be either experts in all of these areas, or you are more likely to have problems when a change on one area affects the others.

Provide real-life examples of MVC architectures: Cell Phones, Desktop Phones, Skype, etc. Changing the View (type of dial pad, speakers, mics) doesn't affect the model (the audio signal), the controller is the circuitry in the phone that translates sound waves into audio signals.

Title: Explain MVC to non-programmers

Body:
Benefits of MVC is primarily in the separation of concerns:

It lets people concentrate on what they do best.

Database admins develop the model
Programmers write the controllers
and Graphic designers can design the views.

It lowers costs because intertwined systems require staff to be either experts in all of these areas, or you are more likely to have problems when a change on one area affects the others.

Provide real-life examples of MVC architectures: Cell Phones, Desktop Phones, Skype, etc. Changing the View (type of dial pad, speakers, mics) doesn't affect the model (the audio signal), the controller is the circuitry in the phone that translates sound waves into audio signals.


问:
How to preserve order of incoming requests on a multithreaded server
I want to write a multithreaded server application which is able to process requests in a multithreaded fashion while keeping up execution order of incoming requests of the same client.

For example if client A sends a message M1 and then a message M2 the muiltithreaded server must process these requests in exactly the same order, first M1 and then M2. However, there is no guarantee that threads that process these incoming requests do process these messages in the same order.

For example, thread T1 consumes message M1, thread T2 consumes message M2 and completes processing the message and is able to accept further messages before T1 has finished it's job.

After a message has been processed the result will be passed to some other process for further processing (of which only one exists in the whole computation), this process will schedule sending a response to the client.

These mutliple threads do only exist for the purpose of preprocessing data for some other process. This particular problem sounds like a problem with pipeline characteristics, first M1, then M2, then M3, etc. However, I wanted to parallalize as much as possible to achieve a higher number of communication cycles between the server and the clients.

What architecture / pattern / technique is well suited for such a task?

答:
To restore the original order after they leave your thread pool, you need some sort of data structure that holds completed messages until the next expected unit arrives and then resume releasing them to the waiting consumer thread.

What particular approach is best will depend upon how many messages are coming through, how sensitive the ultimate recipient is to skipping (nothing is happening while you wait for the out of order message to finish processing), how much work each message takes to process, memory available, etc.

If the consumer expects a certain number of messages per period of time, you might be able to accomplish this by feeding completed units of work into a sorting data structure (like a btree/binarytree/etc) and periodically peeling off the lowest ranked unit. This assumes you will be receiving messages and processing them at a rate more than fast enough to keep the buffer full. If finished messages arrive in too wrong an order, you might need to drop them rather than put them at the front of a buffer).

OwnerDisplayName: xSNRG


答:
The only fool proof way is to force users to identify themselves with an authentication mechanism.

Identification is the "Who are you?" question, and the answer can be a user name, real name, email address, browser finger print, etc.

Authentication is "Who are you and prove it!".  In computer security this is implemented by requiring the user to supply a minimum of two things, such as the user name and a MATCHING password which have been established in advance.  In this regard "things" are grouped into one of three categories:

"Something you possess" (An object such as a One-time pad)

"Something you know" (such as a piece of secret information, typically a password), and

"Something you are" (presented as a digitally encoded representation of a piece of your body, eg a fingerprint scan)

Access control is always a trade-off between convenience and security.  To make a system more secure you necessarily have to make it less convenient to use.  The most convenient system is where anybody can just do anything without having to supply any credentials.  Such systems are quite rare these days, but some public Wikis still allow anybody to edit the content.  On the end of the spectrum high security systems, such as the ones controlling nuclear reactors, require you to be physically at the system console in a room where you must book your access in advance.  Everything else falls in somewhere in between.

After Identity and Authentication comes the third concept:  Authorization, which means "Is the identified user allowed to perform the requested operation"

Authorization is meaningless without an authenticated identity, because if  you are trying to prevent misuse of a system without authenticating the users, then you simply can not be sure that users are who they claim to be.

So basically it amounts to you having to force your users to supply identity credentials, which can be set up through a registration process.

The question you must ask is how much do you care about preventing misuse of your system and how much inconvenience will your users be willing to put up with.  People are quite used to having to log in to web based services, and often quite fed up with misuse such as spamming and cheating, so most people will accept that they must register to continue using your system.

Are we talking about cheating at online voting stations?  Cheating about having received a soup kitchen meal ticket?  An online board game?  A (small) banking application?  A school subject testing application?  A Job interview screening test for a programmer position?  These all have different requirements because the users are different people from different backgrounds and with different skills and resources.

Update:

Since you are already authenticating your users, one concept (that I am currently investigating for my own application) is using a 3rd-party to do the hard work in providing user identification and authentication.  Basically for my application I will be using a form of OpenID.  I will not be supporting local registrations, users will only be able to register using their Facebook/Google+/LinkedIn accounts.  My objective is different though - I want to make it very easy for users to sign up.  Your objective is to eliminate disposable accounts by making it harder for users to register disposable accounts.

This process alone will not make it impossible to set up disposable accounts.  What I would suggest and what other web sites, including StackExchange, does to a greater-or-lesser degree:  Combine the identification with a trust-score.  Basically a new user should not immediately have the authority to post.  They need to first earn trust by some means.  SlashDot allows you to filter and by default filters out some low-score posts.  Many sites require a new user to be vetted by an existing "resident".  Forums display a user's registration date and number of posts as a measure of trust.  Sites like SE also allows existing users to "vote" or "thank" users for their valuable and helpful posts.

The concept is to authorize the action of posting and/or rate posts.

Posts from users with a low trust score may need to first be scrutinized and "accepted" by a trusted user before it becomes visible to the public.

Update 2

Higher forms of security may require more than two of the three types of things a user must provide to authenticate themselves.  A very common thing for people to "have" is a phone and you can check it by sending a text message with a unique code, which the user must enter back into the web site to prove that they have the phone with that number.  Once they have entered the phone number, and you have checked that the number is not on a list of banned numbers, you can grant them a higher trust score.

With such a system you can change your existing system without inconveniencing your existing users too much.  For example allow all users to register and log in.  In order to post they must either get a high trust score or they must supply a phone number which you can verify.  The phone number requirement does not completely make it impossible for people to misuse the system, but makes it rather difficult to create many disposable accounts.

Note that this system have some disadvantages.  Services like Amazon SNS only sends text messages to American numbers.  Messaging providers to other countries charge a fee to deliver the message, which could be a problem if your web service cannot afford the cost.

One other mechanism for identifying the user device exists but only if the user accesses the service through an application installed on a mobile device.  An app can read the device identification number.  This can also be used as a means of "trusting" a user because people only have so many physical devices, but web based apps does not support reading this type of information from the device.

Title: Identifying user of a webapp

Body:
For a small webapp I want to be able to identify a user.

The issue is, if the user cheats in some manner, I want to be able
to forbid him the usage of my application.

I already have done extensive research on that topic, but to no avail.

What is not an option:

cookies: can be disabled or deleted (to easily overridable)

ip adress: is dynamic or hidden through NAT

MAC adress: cannot be retrieved (as far as I know, or at least not without terrifying the user)

Flash: is not supported on a wide range of devices

This is a central part, so I can decide which technology to use along
this decision.

What are the options I have? (especially I do not want that the user gets a security warning as this is often the case with java applets)

Some kind of unique machine identifier would be okay.

-edit-

What I want to protect against:

It should be some kind of discussion board about recent topics.
There will be people that proof that they have not the attitude to discuss in the public, e.g. insult other people etc.

And I want to have a possibility to keep these people away.

There will be somekind of login. But what keeps people from creating simply another account and keep going?

Fingerprinting seems to be to weak, as is has some well known flaws. Using a bank account is way to hard to use, at least since there is nothing you can buy right now, it will keep users from using the site.

Isn't there something in the middle? Like a device id, which keeps users from one device away, despite it might be wrong, this would do the job in a vast majority of cases I believe.


问:
How to structure a loop that repeats until success and handles failures
I am a self-taught programmer. I started programming about 1.5 years ago. Now I have started to have programming classes in school. We have had programming classes for 1/2 year and will have another 1/2 right now.

In the classes we are learning to program in C++ (which is a language that I already knew to use quite well before we started).

I have not had any difficulties during this class but there is one recurring problem that I have not been able to find a clear solution to.

The problem is like this (in Pseudocode):

```<code> do something
 if something failed:
     handle the error
     try something (line 1) again
 else:
     we are done!
</code>```

Here is an example in C++

The code prompts the user to input a number and does so until the input is valid. It uses cin.fail() to check if the input is invalid. When cin.fail() is true I have to call cin.clear() and cin.ignore() to be able to continue to get input from the stream.

I am aware that this code does not check of EOF. The programs we have written are
  not expected to do that.

Here is how I wrote the code in one of my assignments in school:

```<code>for (;;) {
    cout << ": ";
    cin >> input;
    if (cin.fail()) {
        cin.clear();
        cin.ignore(512, '\n');
        continue;
    }
    break;
}
</code>```

My teacher said was that I should not be using break and continue like this. He suggested that I should use a regular while or do ... while loop instead.

It seems to me that using break and continue is the simplest way to represent this kind of loop. I actually thought about it for quite a while but did not really come up with a clearer solution.

I think that he wanted me to do something like this:

```<code>do {
    cout << ": ";
    cin >> input;
    bool fail = cin.fail();
    if (fail) {
        cin.clear();
        cin.ignore(512, '\n');
    }
} while (fail);
</code>```

To me this version seems alot more complex since now we also have variable called fail to keep track of and the check for input failure is done twice instead of just once.

I also figured that I can write the code like this (abusing short circuit evaluation):

```<code>do {
    cout << ": ";
    cin >> input;
    if (fail) {
        cin.clear();
        cin.ignore(512, '\n');
    }
} while (cin.fail() && (cin.clear(), cin.ignore(512, '\n', true);
</code>```

This version works exactly like the other ones. It does not use break or continue and the cin.fail() test is only done once. It does however not seem right to me to abuse the "short circuit evaluation rule" like this. I do not think my teacher would like it either.

This problem does not only apply to just cin.fail() checking. I have used break and continue like this for many other cases that involve repeating a set of code until a condition is met where something also has to be done if the condition is not met (like calling cin.clear() and cin.ignore(...) from the cin.fail() example).

I have kept using break and continue throughout the course and now my teacher has now stopped complaining about it.

What are your opinions about this?

Do you think my teacher is right?

Do you know a better way to represent this kind of problem?

答:
My logic instructor at school always said, and pounded it into my brain, that there should only be one entrance and one exit to loops.  If not you start getting spaghetti code and not knowing where the code is going during debugging.

In real life I feel that code readability is really important so that if someone else needs to fix or debug an issue with your code it's easy for them to do.

As well if it's a performance issue because you are running through this look millions of times the for loop might be faster.   Testing would answer that question.

I don't know C++ but I completely understood the first two of your code examples.  I am assuming that continue goes to the end of the for loop and then loops it again.  The while example I understood completely but for me it was easier to understand than your for(;;) example.  The third one I would have to do some thought work to figure it out.

So going by for me which was easier to read (not knowing c++) and what my logic instructor said I would use the while loop one.


问:
What is the ideal mindset for a developer participating in a requirements gathering meeting?
As a mid-level developer on my team, I participate in requirements gathering/scope planning meetings of projects that I will be part of. I have been finding it difficult to come up with the questions that add value to the discussion or to my knowledge. After some self analysis, I found that knowing that there is a senior engineer in charge of the project who would lead the discussion, has caused me to be laid back in some of these discussions. But I do want to get to a point where I am entrusted with more responsibility and gain skills to handle projects on my own. Though mindset is a bit abstract term to ask about, I would like to get some advice on few things listed below:

What kind of preparation will I need to gain most out of these meetings. From observations from team members, I noticed that understanding what all systems that the project is going to impact, What business decisions will majorly impact the project design etc. are some of them.

Do I need to avoid thinking about low level implementation details for every requirement. On one hand, I feel it's important to consider them because I will know about the feasibility but I also understand it hinders considering all choices.

Finally, as a developer, what is something that I should not be worried about?

答:
Answering your questions in order:

The mindset going into this in terms of preparation, is to ask the right questions so that the user knows exactly what they want. This is much more difficult than it seems. I need to emphasize asking the right questions. Be specific, if there is any ambiguity, ask another question. Usually during these meetings one answer from the client spawns 3 new questions. Once you know what they want, then it is your job to figure out how it impacts the different systems, not theirs.

If there is anything that the client wants in terms of requirements that are impossible, then tell them (my professors used to call it "wings on a car"). Sometimes clients just don't know software well enough to know the capabilities. Work around that.

No, that comes during the design phase. If it can't work, the requirements are revisited.

Don't worry about getting it right the first time, because you most definitely won't. Requirements get screwed up all the time by feature creep, clients wanting new things but never telling you, etc. Over prepare then go with the flow.


问:
What is idiomatic way in f# to express sequence of if statements with overlapping conditions
Let's say I got code like this:

```<code>if(conditionA)
    do something
if(conditionA && conditionB)
    do something more
</code>```

Obviously, I could nest the ifs (although when there are more complicated conditions this is not readable for me) like below:

```<code>if(conditionA)
    do something
    if(conditionB)
        do something more
</code>```

How can I write similar code in f# idiomatically. I assume if statement isn't considered idiomatic in functional programming (am I wrong?)

I could write two subsequent match expressions or nested match expression to mimic above code but doing so seems pretty ugly to me.

答:
There's nothing wrong with using if-then-else in F#. It's as idiomatic as things can get. Most people do highlight match expressions when talking about F#, so it may seem that ifs are discouraged, but that's not really the right way to think of things.

That's true that match expressions are powerful. It doesn't take much to benefit from using them instead of ifs when:

you can use deconstruction in the patterns,

or you want to match a tuple of values,

or you have more cases than a binary true/false and want to handle them without building if-else towers.

That said using the construct below should be called out in a code review (and rightly so - this should have been an if):

```<code>match cond with
| true -> ...
| false -> ...
</code>```

It's roughly on the same level of code smelliness as the dreaded construction you might sometimes see in bad c-style lang code:

```<code>/// this is bad, don't do this at home (just return cond).
if (cond) {
    return true;
} else {
    return false;
}
</code>```

I don't see a problem in your second snippet with nested ifs. That said, there might be better solutions, but it's hard to suggest one when talking about do somethings and conditionBs.

Body:
There's nothing wrong with using if-then-else in F#. It's as idiomatic as things can get. Most people do highlight match expressions when talking about F#, so it may seem that ifs are discouraged, but that's not really the right way to think of things.

That's true that match expressions are powerful. It doesn't take much to benefit from using them instead of ifs when:

you can use deconstruction in the patterns,

or you want to match a tuple of values,

or you have more cases than a binary true/false and want to handle them without building if-else towers.

That said using the construct below should be called out in a code review (and rightly so - this should have been an if):

match cond with
| true -> ...
| false -> ...

It's roughly on the same level of code smelliness as the dreaded construction you might sometimes see in bad c-style lang code:

/// this is bad, don't do this at home (just return cond).
if (cond) {
    return true;
} else {
    return false;
}

I don't see a problem in your second snippet with nested ifs. That said, there might be better solutions, but it's hard to suggest one when talking about do somethings and conditionBs.