问:
Languages supporting unicode logic operators
Are there any programming languages that support the use of unicode logic operators? For example, many programming languages use "!=" as the "does not equal"
operator, but in mathematics the symbol is "≠".
Since unicode is now more standardized, why don't programming languages support their use as logical operators? Many of the symbols are on my keyboard, I just hit the alt button.

答:
Julia uses unicode extensively. It allows you to define both operators and variables using unicode symbols.

The best way I found to input the chars is to use the "latex-completions" plugin for Atom, which allows you to replace, for example "\alpha" with "α" just by pressing tab.

I guess since Julia is more oriented towards scientific computing having greek variable names is a big plus for readability (compare gradient_delta with ∇δ). Also, ≠ is slightly better than != or not (=) when you get used to it.

Anyway, at first freely using unicode chars in code seemed quite odd, and I was afraid things would break. Living in a spanish-speaking country I learned to hate programmers who would input accented vowels (á,é,í,ó,ú) in code comments, because it usually wreaks havoc with tools that only support strict ascii. But I must say that in Julia (also in Latex with xetex or unicode support packages) it works perfectly.


问:
When should I use Perl's DBIx::Class?
DBIx::Class is a popular Perl interface to any database that you can connect to through DBI. There is good documentation for its technical details, but scant information about its proper use (including the situations where you probably don't want it).

In many situations, people reach for it reflexively because they think they should use it for everything that involves a database. However, I most often seen it misused to the point where it becomes a pain point. My question during code and architecture reviews is always "What benefit is Foo giving you?" Most often, the developers I see in these situations can't form a coherent answer to that. But then, they also often don't understand simple SQL.

For the past couple of months I've been asking people "Why do you use DBIx::Class?" and have only received one good answer (and that person also was able to answer the follow-up "When wouldn't you use it"). Peter Rabbitson, the lead developer, got close to an answer in his interview on FLOSS Weekly, but it's a bit buried in the middle of the interview.

So, how can I decide whether using DBIx::Class is appropriate for a project?

答:
I'm not a Perl expert, but I use it a lot.  There's a lot of stuff I don't know or know how to do better; some stuff I just am not yet capable of understanding, despite documentation.

I tend to start off with DBI because I think, "Oh, this is a simple project, I don't need the bloat of an ORM and I don't want to hassle with the setup and Schema modules."  But very quickly -- almost every time -- I quickly start to curse myself for that decision.  When I want to start getting creative in my SQL queries (dynamic queries, not just comparison placeholders) I struggle to maintain sanity using DBI.  SQL::Abstract helps a lot, and typically that's probably sufficient for me.  But my next mental struggle is maintaining so much SQL inside my code.  It's very distracting for me to have lines and lines of embedded SQL inside ugly heredocs.  Maybe I need to use a quality IDE.

In the end, more times than not, I stick with straight DBI.  But I keep wishing there were a better way.  DBIx::Class has some really neat characteristics and I've used it a few times, but it seems just so overkill for all but the biggest projects.  I'm not even sure which I find more cumbersome to manage: DBI with scattered SQL or DBIC with scattered Schema modules.

(Oh, stuff like constraints and triggers is a huge plus to me for DBIC.)

ContentLicense: CC BY-SA 3.0

Title: When should I use Perl's DBIx::Class?


问:
Is 4-5 years the “Midlife Crisis” for a programming career?
I’ve been programming C# professionally for a bit over 4 years now. For the past 4 years I’ve worked for a few small/medium companies ranging from “web/ads agencies”, small industry specific software shops to a small startup. I've been mainly doing "business apps" that involves using high-level programming languages (garbage collected) and my overall experience was that all of the works I’ve done could have been more professional. A lot of the things were done incorrectly (in a rush) mainly due to cost factor that people always wanted something “now” and with the smallest amount of spendable money. I kept on thinking maybe if I could work for a bigger companies or a company that’s better suited for programmers, or somewhere that's got the money and time to really build something longer term and more maintainable I may have enjoyed more in my career. I’ve never had a “mentor” that guided me through my 4 years career. I am pretty much blog / google / self taught programmer other than my bachelor IT degree.

I’ve also observed another issue that most so called “senior” programmer in “my working environment” are really not that senior skill wise. They are “senior” only because they’ve been a long time programmer, but the code they write or the decisions they make are absolutely rubbish! They don't want to learn, they don't want to be better they just want to get paid and do what they've told to do which make sense and most of us are like that. Maybe that’s why they are where they are now. But I don’t want to become like them I want to be better. I’ve run into a mental state that I no longer intend to be a programmer for my future career. I started to think maybe there are better things out there to work on. The more blogs I read, the more “best practices” I’ve tried the more I feel I am drifting away from “my reality”. But I am not a great programmer otherwise I don't think I am where I am now. I think 4-5 years is a stage that can be a step forward career wise or a step out of where you are.

I just wanted to hear what other have to say about what I’ve mentioned above and whether you’ve experienced similar situation in your past programming career and how you dealt with it. Thanks.

答: I think it is time to be Sr, Developer for you, I m new at this market and learning and improving but employers are not looking for humans, they are looking for monsters like Jr. developer with 6 + years of experience and it is really frustrating.</p>

OwnerDisplayName: Jeff

ContentLicense: CC BY-SA 2.5

OwnerDisplayName: Braveyard


答:
(Before I start I should say this just compares DBIC, DBI and Mojo-based DBI wrappers. I don't have experience with any other Perl ORM and so will not comment on them directly).

DBIC does many things very well. I am not a heavy user of it, but I know the theory. It does quite a nice job of SQL generation and especially (as I've been told) handling joins etc. It also can quite nicely do prefetching of other related data.

The major advantage as I see it is the ability to DIRECTLY use the results as your model class. This is otherwise known as "adding result-set methods" wherein you can get your results and call methods on those results. The common example is fetching a user object from DBIC and then calling a method to check whether their password is valid.

Sure schema deployment can be hard, but it is always hard. DBIC has tools (some in external modules) which make it easier, and probably easier than managing your own schemas by hand.

On the other side of the coin, there are other tools that appeal to other sensibilities, like the mojo flavored DBI wrappers. These have the appeal of being lean and yet still usable. Most have also have taken a cue from Mojo::Pg (the original) and added handy features like schema management in flat-files and pubsub integration.

These Mojo flavored modules grew out of one other weak point of DBIC, which is that it is not (yet) capable of doing asynchronous queries. The authors have assured me that it is technically possible perhaps even quickly but there are problems designing an api that would be fitting. (Admittedly I've even been asked to help with this, and while I would, I just don't know how to move the needle in the time I have to devote to it).

TL;DR use DBIC unless you love SQL or you need async, in which case investigate the Mojo flavored DBI wrappers.

Title: When should I use Perl's DBIx::Class?

Body:
DBIx::Class is a popular Perl interface to any database that you can connect to through DBI. There is good documentation for its technical details, but scant information about its proper use (including the situations where you probably don't want it).

In many situations, people reach for it reflexively because they think they should use it for everything that involves a database. However, I most often seen it misused to the point where it becomes a pain point. My question during code and architecture reviews is always "What benefit is Foo giving you?" Most often, the developers I see in these situations can't form a coherent answer to that. But then, they also often don't understand simple SQL.

For the past couple of months I've been asking people "Why do you use DBIx::Class?" and have only received one good answer (and that person also was able to answer the follow-up "When wouldn't you use it"). Peter Rabbitson, the lead developer, got close to an answer in his interview on FLOSS Weekly, but it's a bit buried in the middle of the interview.

So, how can I decide whether using DBIx::Class is appropriate for a project?


问:
Tips for working with badly designed software
Currently in my job im working with a lot of older php systems which don't follow normalised design patterns and are frankly, a mess.

As an ethical developer and someone who always strives to better what Im working on, but also keep within the budget of my employer and their clients, what are my obligations here?

Most of the systems should be rewritten, but often the budget only allows for bodge jobs to fix minor issues. Apart from writing tests, what can I do to both fit within the budget but also add value and integrity to the code?

答:
First, do no harm.

You are faced with a system that works, at least to some extent. It may have huge problems, it may be unmaintainable, but it works, and it keeps the business running.  Without a running business, the code has no meaning, so don't kill the business.

Several practices can kill the business:

A big rewrite can take so long that the business can't adapt to changing conditions in the middle, and the business may fold before the rewrite comes out. Just ask Joel how this can turn out

A big-bang implementation has huge amounts of risk all in one big implementation. If you've gotten anything wrong, it may be hard or impossible to recover.  How will your business be if you cannot fulfill any orders during your busiest time of year?

There's business logic in the old code that is there for a reason, even though none of the people who understand why it's there are still with the company.  A rewrite may not notice the pieces that need to be maintained.

Be surgical and incremental

The best thing to do in this case is to work in very small pieces. When asked to add something to the existing system, take pains to find the smallest possible change that will do the job while minimizing the impact on other portions of the code.  Use these tasks to learn the system and understand which portions of the system are subject to frequent change and which are fairly static.

Let sleeping dogs lie. Some parts of the system are ugly, but since they work and are never changed, they should be at the bottom of your list for improvement.  Changing these pieces mean you add risk to the business, but no real benefit.

Plan for parallel processing or rapid backout. When you find an area that changes frequently, is important to the business, and really needs to be improved, how do you do it?  Very carefully.  At a minimum, you should make it very easy to take your rewrite out of the main processing if there is a problem.  It is much safer for the business to make big changes if you can flip back to the time-tested legacy code in minutes than if you risk being stuck in a broken state for hours or days.  It is even better if you can run both paths at once and prove that the new code produce the same (or better) results as the existing code.

Looking at all these tips, the more you can break the system into discrete bits the more successful you will be at improving the system.


问:
Steps to begin versioning a web application
Until now, we have not had an application version strategy or version-ed our application. We are considering doing so with our next release. What are the minimum tasks necessary to transition to a version-ed application?  Answers in the form of bullet points, not necessarily in chronological order would be sufficient. It's okay to keep it high level. A few examples might be:

Determine version control name convention Major.Minor.Build.YY...

Revise on screen imaging or text to display current version

Prepare release notes template for communicating new releases to users

We're a .Net shop using TFS if that helps. If it's not obvious already, I'm not a developer. I'm the solution expert/BA acting often as the project manager.

答:
Imagine the life of your product as a string of pearls. The underlying string is the constant background plumbing: software definition processes, issue management processes, development processes, etc. It's the skeleton.

The pearls are distinct releases. Some are cultured. Others are natural. There are saltwater pearls and freshwater pearls.

Actually, that's a bad example because there's no concept of time. Time is important in release management. That's the thing we're really talking about: improving release management. So let's build a better metaphor. The life of your product is a running reel of film. That's better. The dark part between the cells -- the part perforated with holes so the projector can pull it through -- that's your plumbing. The releases are the cells. They're moving through time. The film is unfolding. We're watching it.

Now, the trick is to gather everything you need to create a particular cell so that it fits between its neighbors and let's the story flow smoothly. You need a director, writers, actors, set builders, producers (the money people), camera people, an assistant to the director, and maybe a cinematographer. Yeah, I like this. This is working.

Stuff that goes into a cell: plot (feature development), scene tuning (bug fixes), product placement (inane client requests), shot structure (UX).

You've got to coordinate all of those things perfectly so they not only fit together in the cell, but also in the film.

External shareholders want to know which cells their material appears in.

Internal shareholders want to know which cells they're expected to contribute to and what they're expected to contribute.

That's why strong (but flexible) scheduling is a must. With long time frames, a rough estimate is adequate, but as cells draw near it's important to communicate precisely where an issue or feature will appear.

The problem with the items mentioned in your question and other answers is that they represent a small part of the film-making process. Version numbers, version number display, CI... those are all important things but they represent a small part of a film. Maybe they're the camera people (developers) and cinematographer (architect). Remember that a lot more goes into each cell.

Most importantly (and here's where our metaphor really shines!), remember that a good story isn't told with a single cell in a film. It's a bunch of cells together to form a scene, a bunch of scenes together to form an act. It's all about timing. Not only does everything need to come together for the cell, but the cells need to be sequenced to maximize their affect!

More:

8 Ways to Improve your Enterprise Release Management Process

Filmmaking


问:
Decoupled architecture in Android
I am building an app for Android that will have multiple data sources depending on who is using it. N-tier architecture with a repository pattern seems like the right way to go about this but I am struggling trying to stick to some base rules.

From what I understand I should be able to write the BL without actually knowing anything about the UI or the DAL. My problem is that once I started writing the UI layer I realized that I need the DAL to run on a background thread. This then forces me to modify my BL/DAL to run async w/ callbacks based on a UI layer requirement which screams "NO" to me.

Here are the 4 layers I have right now and their dependencies.

UI - dependencies on BL and Common/Repository = Android Activities/Fragments. UI Layer will tell the BL which concrete repository/DAL type to use

BL - dependencies on Common/Repository interfaces = Separate package with business rules. Uses the concrete repository that was passed from the UI layer to do stuff

Common/Repository - no dependencies = Separate package with models and interfaces that allows all other layers to interact w/o know about each other. Is this even considered a layer? Should the models be concrete classes or interfaces?

DAL - dependencies on Common/Repository package = Separate package with concrete implementations of the repositories. If Common doesn't have concrete models should each layer have their own concrete models in order to create objects to pass between layers?

What am I missing? I'm trying to force myself out of my comfort zone on this :)

答:
On Android, the canonical answer to this is to use a combination of Activities, Fragments, Adapters, Loaders and Services.

In my project we're pretty strict about how we do things.  Activities are used to set up the base view, which is usually just a FrameLayout or something similar.  We have no UI logic whatsoever in there.  An Activity is just used to wire everything up.  UI is all handled by Fragments (except for some special fragments that we treat like an Activity).  Network reads are all done in Loaders and we use Adapters to push the data to the UI.  Adapters are owned by the Fragments, but the Activity wires them up to the Loaders with callbacks.  Services are used for network writes.

This setup takes a pretty decent amount of time to set up the infrastructure and develop your basic workflow, but once you've got a working pattern, you can just kind of coast your way through the rest of it.  Want a Floating Action Button, just include the right fragment and base classes handle the boilerplate.

So in the end it's more of an MVP stack than a MVC.  Our views are aware of what a user is trying to do and indicate that via interfaces.  Here's a typical workflow:

User is looking at a thing and wants to vote on it.  The fragment that holds the upvote button catches the click.  First it updates the vote count locally to the expected new value.  Note that this isn't official, and it's not persisted anywhere.  It just improves latency in the most common case where the vote hasn't changed.  It casts the parent (Activity or Fragment) to an interface with an upvote() method and calls it.  The Activity (or Fragment) gets the upvote call and passes it on to the service, with a callback.  The service puts the action into a queue (we single-thread writes) and eventually calls the API.  The API returns the new number of votes.  The Service calls the callback, which gets routed into the Loader.  The Loader updates the relevant section of the model, and then signals to the Adapter that new data is available, which the UI then picks up and shows.  Note that if it's too difficult to patch the model, the Loader could just go re-request it.

So that's a pretty big mouthful, but our logic is separated, the entire UI is built via composition, the components are all highly testable, and it's usually pretty easy to trace out what happened when there's a bug.

Title: Decoupled architecture in Android


答:
The difference between senior and junior programmers, when talking about people with experience at all, is generally just a pay-based one. There's a lot of variability in what organizations value in order to change the title, and often it's determined by what you demand when you get hired.

If it's any consolation I'm having a 10 year mid-programming-life crisis, although I did start programming on an Apple ][+ so it might be a 24 year point; I don't know. I just wish people didn't expect magic from programmers.

Title: Is 4-5 years the “Midlife Crisis” for a programming career?

Body:
I’ve been programming C# professionally for a bit over 4 years now. For the past 4 years I’ve worked for a few small/medium companies ranging from “web/ads agencies”, small industry specific software shops to a small startup. I've been mainly doing "business apps" that involves using high-level programming languages (garbage collected) and my overall experience was that all of the works I’ve done could have been more professional. A lot of the things were done incorrectly (in a rush) mainly due to cost factor that people always wanted something “now” and with the smallest amount of spendable money. I kept on thinking maybe if I could work for a bigger companies or a company that’s better suited for programmers, or somewhere that's got the money and time to really build something longer term and more maintainable I may have enjoyed more in my career. I’ve never had a “mentor” that guided me through my 4 years career. I am pretty much blog / google / self taught programmer other than my bachelor IT degree.

I’ve also observed another issue that most so called “senior” programmer in “my working environment” are really not that senior skill wise. They are “senior” only because they’ve been a long time programmer, but the code they write or the decisions they make are absolutely rubbish! They don't want to learn, they don't want to be better they just want to get paid and do what they've told to do which make sense and most of us are like that. Maybe that’s why they are where they are now. But I don’t want to become like them I want to be better. I’ve run into a mental state that I no longer intend to be a programmer for my future career. I started to think maybe there are better things out there to work on. The more blogs I read, the more “best practices” I’ve tried the more I feel I am drifting away from “my reality”. But I am not a great programmer otherwise I don't think I am where I am now. I think 4-5 years is a stage that can be a step forward career wise or a step out of where you are.

I just wanted to hear what other have to say about what I’ve mentioned above and whether you’ve experienced similar situation in your past programming career and how you dealt with it. Thanks.

OwnerDisplayName: dlamblin


答:
I wrote my thoughts on this in DBIC vs DBI three years ago. To summarise, I listed two main reasons:

DBIC means that I don't have to think about all of the trivial SQL that is needed for pretty much any application that I write.

DBIC gives me objects back from the database rather than dumb data structures. This means that I have all of the standard OO goodness to play with. In particular I find it really useful to be able to add methods to my DBIC objects.

As for anti-patterns, well the only one I can think of is performance. If you really want to squeeze every clock cycle out of your CPU then perhaps DBIC isn't the right tool for the job.  But, certainly for the applications that write, those cases are increasingly rare. I can't remember the last time I wrote a new application that talked to a database and didn't use DBIC. Of course, it helps if you know a little about tuning the queries that DBIC generates.

Title: When should I use Perl's DBIx::Class?


答:
You open a very interesting question. I wholeheartedly agree with you. I've made similar observations.

I've been programming professionally for several years already and what I have observed is that the amount of good programmers out there, of great developers who love their work and can do it with quality and passion is pretty much close to zero. I probably met only one person who could teach me something. Most of what I know I have learned by myself, reading books and forums, asking in forums and googling for revelation thoughts.

After a while I don't regret this much.

The options to learn in a working environment can often be limited. You don't start things. You don't finish them. You don't design, don't improve, don't refactor, don't think about architecture, you just code and hack things together. It's how most of the shops work. Not only you don't learn anything, it's more likely that you will learn mostly wrong things how NOT to develop software. I've been continuously seeing scary things around me, all those anti-pattern you have heard of. What is worse, I'm forced to do them myself.

I don't know how it happened, but I managed to somehow build an input barrier. I stay open, listen and if I see some potential for self-improvement I research and maybe adopt some technique or idea. But no BS can ever get through. I have worked in badly run projects for a long time, but I have not adopted any of those bad techniques for myself.

I pretty much soon understood that if you wish satisfaction with programming, forget about job and have your own personal project. It's where you can apply all your love, passion and knowledge to do things right with the high quality level. You will learn a great deal of stuff, a myriad of things you would never have been exposed to and challenged with when hacking boring corporate staff. I only do my job for paycheck and get satisfaction with my own personal projects.

One thing I truly don't understand is how this situation is possible nowadays. Software development has matured a lot. It has had good and bad experience. Many successful projects and a great deal of failed ones. There is experience with long-term projects and understanding what long-term effects one or the other organization will bring upon the project. There are numerous studies available and good books written. "Pragmatic Programmer", "Code Complete", "Mythical Man-Month", "Design of everyday things" and others. Why nobody but us, the programmers ever reads them? How it is possible that even after 20 years of working in IT most developers and managers never found a time to read one or the other methodology book. They are written for, but hardly read by, those who need this medication most.

Regarding career perspectives. What I also have noticed in general on the job market for employees, is that employers out there increasingly lose interest in quality work (imagine they had it once) are shopping more and more for the cheapest work craft available. You find it hard to sell your knowledge, experience and understanding of the universe to anyone. It's not in demand. What is in demand is having your projects ruined by the juniors who have no experience and desire to do professional work. Cheap people are used and abused and then thrown out so that the next round begins. Projects are also outsourced to low-wage destination where they are done by people who apparently begin to learn programming just with your project. That's one thing I truly don't understand.

I'm entertaining more and more the idea that I will drop employed programming work at some time in the future. I would very much like to work in my own start-up with my own project. If not that, I'm considering trying freelancing or probably changing the payed job nature. After all, I hardly learn anything during working hours and I don't get any satisfaction at all. I can do anything 9-5 and always have satisfaction with my own personal projects. I learn much from online communities. I receive here attention, support for my ideas and on occasions even recognition I could never get with my job and my work colleagues. Will see where I will be in the future.


问:
Integration Tests - How much is too much?
First of I'm not sure if I chose the right name for my question, I'm not sure if
they are functional tests or integration ( or other ). I'm talking about tests which test (or it should) the app from a http request to database.

With simple controllers where they query the db for some data it's pretty easy to test them. Problem comes when things get complicated. When events are fired and there are a few listeners, which either send emails, create pdfs, make api calls. What's the best practice in this case ? Mock this events and ensuring from the functional tests that they are called with the right parameters? and unit test the events and listeners? or its safer to let them fire the events and see if the result is the expected one ?

In other words should functional and  unit tests work together and should rely one on another ?

LE: I'm not trying to see benefits of unit vs integration, I just want to see how others do in the above scenarios.

答:
I believe that what you're talking about (connecting up the layers) is called integration testing.

How much testing of this variety should you do? Well, I'm firstly going to assume your unit tests are in place with good code coverage.

After that, concentrate on:

Racing line

How the software should behave most of the time.

Known corner cases

Exceptions to the above rules.

Limit testing

Pass in data outside accepted ranges of lookup values and at (and beyond) the limits of the datatypes.

Exotics

Anything else rare that wouldn't be covered by the above, like people using the system improperly, one of the layers going down, doing things out of sequence etc.

Clearly, the further down the list we get, the more time consuming it becomes and fewer bugs are yielded. There comes a point where further testing become uneconomical/expedient. Where exactly this line in the sand is varies from development to development. If you stop testing and the users find further bugs, then clearly testing wasn't sufficient. In the wild however, testing time tends to be squeezed especially is development overruns. Why the development would overrun is a whole different topic.


答:
Your question is really hard to answer in a general way. The purpose of integration testing is to test as much of the system as possible (ideally end-to-end), but the more end-to-end it gets, the more complex it gets.

For example you might want to automatically ensure PDF's are generated correctly with the correct information and layout. This is possible but quite complex, since you need to automatically parse and query the generated PDF's. If you are say creating an invoicing system where the PDF's are of critical importance, it might be worth it. In a system where the PDF's are less critical, you might be satisfied with mocking the PDF generation.

There is no "best practices" that can save you here, you have to make a trade-off decision based on available resources and your understanding of the business requirements.

Title: Integration Tests - How much is too much?

Body:
First of I'm not sure if I chose the right name for my question, I'm not sure if
they are functional tests or integration ( or other ). I'm talking about tests which test (or it should) the app from a http request to database.

With simple controllers where they query the db for some data it's pretty easy to test them. Problem comes when things get complicated. When events are fired and there are a few listeners, which either send emails, create pdfs, make api calls. What's the best practice in this case ? Mock this events and ensuring from the functional tests that they are called with the right parameters? and unit test the events and listeners? or its safer to let them fire the events and see if the result is the expected one ?

In other words should functional and  unit tests work together and should rely one on another ?

LE: I'm not trying to see benefits of unit vs integration, I just want to see how others do in the above scenarios.


问:
Data structure for sorting by multiple attributes
I have a list of integer pairs. The value of this list is the sum of the maximum values of each pair.

For

(0, 5) (20, 5) (6, 8)

the value would be

5 + 20 + 8 = 33

Given a list like this, I need to maximize its value. The only operation I can do is swap an element of a pair with another element of a different pair.

Edit: Just to clarify, I need to do this operation as many times as needed to obtain the maximum value, not just once.

For the example above I can do this

(6, 8) <> (0, 5)

The list would become

(0, 6) (20, 5) (5, 8) => value = 6 + 20 + 8 = 34

So in order to increase the value I'd need to find 2 pairs (a,b) (c,d) with min(a,b) > max(c,d) and then swap min(a,b) with max(c,d), right ? If no such pairs exist then the we cannot increase the value of the list.

Solution:

I need to keep the pairs sorted in descending order by the minimum value and in ascending order by the maximum value because I need to compare the pair with the greatest minimum to the one with smallest maximum.

I thought about using two heaps for this, a minheap which orders by maximums and a maxheap which orders by minimums. This way, each time I need to compare I can just compare the roots of the two heaps.

The problem is that I also need to update these values, by exchanging the minimum value of 1 pair with the maximum value of another pair and then reorder the heaps so I would need to also keep additional information for each heap node for the position in the other heap. Something like Dual heap in Double-ended priority queue.

Is this the best way to approach this problem, or are there better data structures that I can use?

答:
Your problem can be broken down into two parts:

a sorting algorithm that takes a value from a given element

a method to choose which value to extract, given a feature name

So something like (python-like pseudocode):

```<code>data = new MyDataStructure()
#Insert some values into it..
#...

def extract_feature(myObject, feature_name):
   return myObject.getFeature(feature_name)

print sorted(data, (key=lambda x: extract_feature(x))
</code>```

Re: your comment
You are talking about datastructures, but what you are really after..is a sorting algorithm that can take an object type and change the a.compareTo(b) call on the fly. In other words, any datastructure will do. Your sorting algorithm just needs to know how to compute the key with which the sorting will be done.

Example: https://wiki.python.org/moin/HowTo/Sorting#Key_Functions

Body:
Your problem can be broken down into two parts:

a sorting algorithm that takes a value from a given element

a method to choose which value to extract, given a feature name

So something like (python-like pseudocode):

data = new MyDataStructure()
#Insert some values into it..
#...

def extract_feature(myObject, feature_name):
   return myObject.getFeature(feature_name)

print sorted(data, (key=lambda x: extract_feature(x))

Re: your comment
You are talking about datastructures, but what you are really after..is a sorting algorithm that can take an object type and change the a.compareTo(b) call on the fly. In other words, any datastructure will do. Your sorting algorithm just needs to know how to compute the key with which the sorting will be done.

Example: https://wiki.python.org/moin/HowTo/Sorting#Key_Functions


答:
Before I answer the question, I think some background is in order.

The Core of the Problem

After years of interviewing and hiring developers, I've learned two things:

The vast majority of developers have very little experience in
database design.

I've noticed a loose correlation between those who don't understand
databases and those who hate ORMs.

(Note: and yes, I know there are those who understand databases very well who hate ORMs)

When people don't understand why foreign keys are important, why you don't embed the manufacturer name in the item table, or why customer.address1, customer.address2, and customer.address3 fields are not a good idea, adding an ORM to make it easier for them to write database bugs isn't going to help anything.

Instead, with a properly designed database and an OLTP use case, ORMs are golden. Most of the grunt work goes away and with tools such as DBIx::Class::Schema::Loader, I can go from a good database schema to working Perl code in minutes. I would cite the Pareto Rule and say that 80% of my problems have been solved with 20% of the work, but in actuality, I find the benefits even greater than that.

Abusing the Solution

Another reason some people hate ORMs is because they'll let the abstraction leak. Let's consider the common case of MVC web apps. Here's something we commonly see (pseudo-code):

```<code>GET '/countries/offices/$company' => sub {
    my ( $app, $company_slug ) = @_;
    my $company = $app->model('Company')->find({ slug => $company_slug })
      or $app->redirect('/');
    my $countries = $app->model('Countries')->search(
     {
         'company.company_id' => $company->company_id,
     },
     {
         join     => [ offices => 'company' ],
         order_by => 'me.name',
     },
   );
   $app->stash({
     company   => $company,
     countries => $country,
   });
}
</code>```

People write controller routes like that and pat themselves on the back, thinking it's good, clean code. They'd be aghast at hard-coding SQL in their controllers, but they've done little more than expose a different SQL syntax. Their ORM code needs to be pushed down into a model and then they can do this:

```<code>GET '/countries/offices/$company' => sub {
   my ( $app, $company_slug ) = @_;
   my $result = $app->model('Company')->countries($company_slug)
     or $app->redirect('/');
   $app->stash({ result => $result });
}
</code>```

You know what's happened now? You've properly encapsulated your model, you've not exposed the ORM, and later, when you find that you could fetch that data from a cache instead of the database, you don't need to change your controller code (and it's easier to write tests for it and to reuse the logic).

In reality, what happens is that people leak their ORM code all over their controllers (and views) and when they hit scalability issues, they start blaming the ORM rather than their architecture. The ORM gets a bad rap (I see this repeatedly for many clients). Instead, hide that abstraction so that when you've genuinely hit ORM limits, you can choose appropriate solutions for your problem rather than let code be so tightly coupled to the ORM that you're hog-tied.

Reporting and Other Limitations

As Rob Kinyon made clear above, reporting tends to be a weakness in ORMs. This is a subset of a larger problem where complicated SQL or SQL which spans multiple tables sometimes doesn't work well with ORMs. For example, sometimes the ORM forces a join type I don't want and I can't tell how to fix that. Or maybe I want to use an index hint in MySQL, but it's not easy. Or sometimes the SQL is just so darned complicated that it would be nicer to write the SQL rather than the abstraction provided.

This is part of the reason I've started writing DBIx::Class::Report. So far it works well and solves the majority of issues people have here (so long as they're OK with a read-only interface). And while it seems like a crutch, in reality, so long as you're not leaking your abstraction (as explained in the previous section), it makes working with DBIx::Class even easier.

So When Would I Choose DBIx::Class?

For me, I would choose it most of the times that I need an interface to a database. I have been using it for years. However, I might not choose it for an OLAP system, and newer programmers are certainly going to struggle with it. Also, I often find I need meta-programming and while DBIx::Class provides the tools, they're very poorly documented.

The key to using DBIx::Class correctly is the same as for most ORMs:

Don't leak the abstraction.

Write your damned tests.

Know how to drop down to SQL, as needed.

Learn how to normalize a database.

DBIx::Class, once you learn it, will take care of most of your heavy lifting for you and makes it a breeze to quickly write applications.

Title: When should I use Perl's DBIx::Class?

Body:
Before I answer the question, I think some background is in order.

The Core of the Problem

After years of interviewing and hiring developers, I've learned two things:

The vast majority of developers have very little experience in
database design.

I've noticed a loose correlation between those who don't understand
databases and those who hate ORMs.

(Note: and yes, I know there are those who understand databases very well who hate ORMs)

When people don't understand why foreign keys are important, why you don't embed the manufacturer name in the item table, or why customer.address1, customer.address2, and customer.address3 fields are not a good idea, adding an ORM to make it easier for them to write database bugs isn't going to help anything.

Instead, with a properly designed database and an OLTP use case, ORMs are golden. Most of the grunt work goes away and with tools such as DBIx::Class::Schema::Loader, I can go from a good database schema to working Perl code in minutes. I would cite the Pareto Rule and say that 80% of my problems have been solved with 20% of the work, but in actuality, I find the benefits even greater than that.

Abusing the Solution

Another reason some people hate ORMs is because they'll let the abstraction leak. Let's consider the common case of MVC web apps. Here's something we commonly see (pseudo-code):

GET '/countries/offices/$company' => sub {
    my ( $app, $company_slug ) = @_;
    my $company = $app->model('Company')->find({ slug => $company_slug })
      or $app->redirect('/');
    my $countries = $app->model('Countries')->search(
     {
         'company.company_id' => $company->company_id,
     },
     {
         join     => [ offices => 'company' ],
         order_by => 'me.name',
     },
   );
   $app->stash({
     company   => $company,
     countries => $country,
   });
}

People write controller routes like that and pat themselves on the back, thinking it's good, clean code. They'd be aghast at hard-coding SQL in their controllers, but they've done little more than expose a different SQL syntax. Their ORM code needs to be pushed down into a model and then they can do this:

GET '/countries/offices/$company' => sub {
   my ( $app, $company_slug ) = @_;
   my $result = $app->model('Company')->countries($company_slug)
     or $app->redirect('/');
   $app->stash({ result => $result });
}

You know what's happened now? You've properly encapsulated your model, you've not exposed the ORM, and later, when you find that you could fetch that data from a cache instead of the database, you don't need to change your controller code (and it's easier to write tests for it and to reuse the logic).

In reality, what happens is that people leak their ORM code all over their controllers (and views) and when they hit scalability issues, they start blaming the ORM rather than their architecture. The ORM gets a bad rap (I see this repeatedly for many clients). Instead, hide that abstraction so that when you've genuinely hit ORM limits, you can choose appropriate solutions for your problem rather than let code be so tightly coupled to the ORM that you're hog-tied.

Reporting and Other Limitations

As Rob Kinyon made clear above, reporting tends to be a weakness in ORMs. This is a subset of a larger problem where complicated SQL or SQL which spans multiple tables sometimes doesn't work well with ORMs. For example, sometimes the ORM forces a join type I don't want and I can't tell how to fix that. Or maybe I want to use an index hint in MySQL, but it's not easy. Or sometimes the SQL is just so darned complicated that it would be nicer to write the SQL rather than the abstraction provided.

This is part of the reason I've started writing DBIx::Class::Report. So far it works well and solves the majority of issues people have here (so long as they're OK with a read-only interface). And while it seems like a crutch, in reality, so long as you're not leaking your abstraction (as explained in the previous section), it makes working with DBIx::Class even easier.

So When Would I Choose DBIx::Class?

For me, I would choose it most of the times that I need an interface to a database. I have been using it for years. However, I might not choose it for an OLAP system, and newer programmers are certainly going to struggle with it. Also, I often find I need meta-programming and while DBIx::Class provides the tools, they're very poorly documented.

The key to using DBIx::Class correctly is the same as for most ORMs:

Don't leak the abstraction.

Write your damned tests.

Know how to drop down to SQL, as needed.

Learn how to normalize a database.

DBIx::Class, once you learn it, will take care of most of your heavy lifting for you and makes it a breeze to quickly write applications.


答:
As one of the core developers of the Interchange6 e-commerce platform (and the primary schema chainsaw) I have pretty in-depth experience with DBIC. Here are a few of the things that make it such a great platform:

Query generator allows you to write once for many database engines (and multiple versions of each). We currently support Interchange6 with MySQL, PostgreSQL and SQLite and will add support for more engines once we get the time and resources. There are currently only two code paths in the entire project which have additional code to cater for differences between engines and this is purely due to either lack of a specific database function (SQLite is lacking in places) or due to the idiocy of MySQL which changed the way its LEAST function handles NULLs between two minor versions.

Predefined queries mean I can build simple methods which can be called (with or without args) from application code so I keep my queries mostly inside the schema definition instead of littering my application code.

Composable query generation allows queries to be broken up into small understandable predefined queries and then chained together to create complex queries that would be difficult to maintain long-term in either DBIC (even worse in pure SQL) if they were constructed in a single step.

Schema::Loader has allowed us to use DBIC with legacy applications giving a new lease of life and much simpler path to the future.

DBIC plugins, DeploymentHandler & Migration all add immensely to the set of tools that make my life simpler.

One of the huge differences between DBIC and most other ORM/ORM-like platforms is that although it tries to guide you in its way of doing things it also does not stop you doing any crazy stuff you like:

You can use SQL functions and stored procedures that DBIC does not know just by supplying the function name as a key in the query (can also lead to some fun when you try to use LEAST in MySQL ^^ but that isn't DBIC's fault).

Literal SQL can be used when there is no 'DBIC way' to do something and the result returned is still wrapped up in nice classes with accessors.

TL;DR I probably wouldn't bother to use it for a really simple applications with just a couple of tables but when I need to manage anything more complex, especially where cross-engine compatibility and long-term maintainability are key, then DBIC is generally my preferred path.

Title: When should I use Perl's DBIx::Class?


答:
Good question Jeffrey. Do you still enjoy programming? Are you passionate about it, do you do it in your spare time? Or are you just sick of some of the terrible programming jobs you have had.

If you are feeling like programming is not for you, there are many other disciplines you could branch off and do at this point in your career - project management, sales, pre-sales, analyst. These opportunities wouldn't have existed when you were a junior/grad, so you might not have started thinking beyond cutting code. Perhaps you can approach your employer to sponsor you for certification exams, or if you want to explore a new technology (Azure/Silverlight/WPF?)

On the other hand, if you feel like your workplace resembles a true-to-life Dilbert comic, maybe it is just time to move on. If you have been working in a large company, how about interviewing for a few startups, or vice versa. You don't have to tell anyone you are applying for jobs, and even when you get an offer, you are in a great negotiation position since already have a job, you can simply decide if the offer get is better than your current situation, and if not then just keep looking. Larger companies can often offer you better career development opportunities and training, whereas with a small company you get more intangibles, like responsibility for the success of the company, flexible working hours, stock options, and seniority/respect later on if/when the company hits it big.


答:
I think you may be thinking about it too hard, or trying to complicate it more than necessary.

When a pair is to be modified, remove it from both heaps, modify it, then add it to both heaps.

One little issue you might encounter is that once you have found in one heap a pair that you want to alter, how to locate that pair in the other heap.  If pairs are unique, this is not an issue.  If pairs are not unique, then you may need to add a field to uniquely identify them. Unless the language you are using already provides some kind of identity.  (For example, in Java, you could compare by reference.)

Title: Data structure for sorting by multiple attributes

Body:
I have a list of integer pairs. The value of this list is the sum of the maximum values of each pair.

For

(0, 5) (20, 5) (6, 8)

the value would be

5 + 20 + 8 = 33

Given a list like this, I need to maximize its value. The only operation I can do is swap an element of a pair with another element of a different pair.

Edit: Just to clarify, I need to do this operation as many times as needed to obtain the maximum value, not just once.

For the example above I can do this

(6, 8) <> (0, 5)

The list would become

(0, 6) (20, 5) (5, 8) => value = 6 + 20 + 8 = 34

So in order to increase the value I'd need to find 2 pairs (a,b) (c,d) with min(a,b) > max(c,d) and then swap min(a,b) with max(c,d), right ? If no such pairs exist then the we cannot increase the value of the list.

Solution:

I need to keep the pairs sorted in descending order by the minimum value and in ascending order by the maximum value because I need to compare the pair with the greatest minimum to the one with smallest maximum.

I thought about using two heaps for this, a minheap which orders by maximums and a maxheap which orders by minimums. This way, each time I need to compare I can just compare the roots of the two heaps.

The problem is that I also need to update these values, by exchanging the minimum value of 1 pair with the maximum value of another pair and then reorder the heaps so I would need to also keep additional information for each heap node for the position in the other heap. Something like Dual heap in Double-ended priority queue.

Is this the best way to approach this problem, or are there better data structures that I can use?


答:
I'm currently working at my third job. I worked at my first job for 4 years and then at my second one also 4 years.

I think it's a combination of wanting to do something and carreer opportunities and job offers.

I mean: when you work at a job for a year or so and headhunters contact you, you're like: "no sorry, I'm happy at my current job". But then like after a few years you might get tempted into looking around for better opportunities until you reach that breaking point and really look out for something else.

OwnerDisplayName: Gerrie Schenck


问:
About Node serving dynamic pages
I am doing a tech test for a job (Web Development) and in the test I need to get data from an API and there is this conditions for the server:

Focus on client-side (AngularJS)

nodeJS

The server must not serve dynamic pages

My question is: Should I call the API directly using Angular? Or I can get the data from API using the node server?

What does "The server must not serve dynamic pages" mean?

答:
Since this is a tech test, I don't think it's a good idea to answer your first question. The assessors are likely very curious about your answer. Good luck!

As for, "The server must not serve dynamic pages", this just means you can only server static HTML from the web server. That is, you can't merge data into your HTML on the server side. No template engines. If I browse to a URL with a view, that view should always return the same HTML.

The server can also serve raw data

The trick is, you can change HTML in the browser based on the data. All dynamic behavior happens in the browser.

Title: About Node serving dynamic pages


问:
Does immutability hurt performance in JavaScript?
There seems to be a recent trend in JavaScript towards treating data structures as immutable. For example, if you need to change a single property of an object, better to just create a whole new object with the new property, and just copy over all the other properties from the old object, and let the old object be garbage collected. (That's my understanding anyway.)

My initial reaction is, that sounds like it would be bad for performance.

But then libraries like Immutable.js and Redux.js are written by smarter people than me, and seem to have a strong concern for performance, so it makes me wonder if my understanding of garbage (and its performance impact) is wrong.

Are there performance benefits to immutability I'm missing, and do they outweigh the downsides of creating so much garbage?

答:
For example, if you need to change a single property of an object, better to just create a whole new object with the new property, and just copy over all the other properties from the old object, and let the old object be garbage collected.

Without immutability, you might have to pass an object around between different scopes, and you do not know beforehand if and when the object will be changed. So to avoid unwanted side effects, you start creating a full copy of the object "just in case" and pass that copy around, even if it turns out no property has to be changed at all. That will leave a lot more garbage than in your case.

What this demonstrates is - if you create the right hypothetical scenario, you can prove anything, especially when it comes to performance. My example, however, is not so hypothetical as it might sound. I worked last month on a program where we stumbled over exactly that problem because we initially decided against using an immutable data structure, and hesitated to refactor this later because it did not seem worth the hassle.

So when you look at cases like this one from an older SO post, the answer to your questions becomes probably clear - it depends. For some cases immutability will hurt performance, for some the opposite might be true, for lots of cases it will depend on how smart your implementation is, and for even more cases the difference will be negligible.

A final note: a real world problem you might encounter is you need to decide early for or against immutability for some basic data structures. Then you build a lot of code upon that, and several weeks or months later you will see if the decision was a good or a bad one.

My personal rule of thumb for this situation is:

If you design a data structure with only a few attributes based on primitive or other immutable types, try immutability first.

If you want to design a data type where arrays with large (or undefined) size, random access and changing contents are involved, use mutability.

For situations between these two extremes, use your judgement. But YMMV.


问:
Why is %s better than + for concatenation?
I understand that we should use %s to concatenate a string rather than + in Python.

I could do any of:

```<code>hello = "hello"
world = "world"

print hello + " " + world
print "%s %s" % (hello, world)
print "{} {}".format(hello, world)
print ' '.join([hello, world])
</code>```

But why should I use anything other than the +? It's quicker to write concatenation with a simple +. Then if you look at the formatting string, you specify the types e.g. %s and %d and such. I understand it could be better to be explicit about the type.

But then I read that using + for concatenation should be avoided even though it's easier to type. Is there a clear reason that strings should be concatenated in one of those other ways?

答:
An example clarifying readability argument:

```<code>print 'id: ' + id + '; function: ' + function + '; method: ' + method + '; class: ' + class + ' -- total == ' + total

print 'id: %s; function: %s; method: %s; class: %s --total == %s' % \
   (id, function, method, class, total)
</code>```

(Note that second example is not only more readable but also easier to edit, you can change the template on one line and list of variables on another)

A separate issue is that %s code also converts to the string, otherwise you have to use str() call which is also less readable than a %s code.

Body:
An example clarifying readability argument:

print 'id: ' + id + '; function: ' + function + '; method: ' + method + '; class: ' + class + ' -- total == ' + total

print 'id: %s; function: %s; method: %s; class: %s --total == %s' % \
   (id, function, method, class, total)

(Note that second example is not only more readable but also easier to edit, you can change the template on one line and list of variables on another)

A separate issue is that %s code also converts to the string, otherwise you have to use str() call which is also less readable than a %s code.


答:
I agree with Nikos completely.  I think what you maybe need is to find a company that is technology and product focused.  There are companies where the software developers consider development just their job, and they never think about this stuff in their own time.  The working environment is usually much more geared up for managers doing desk work rather than developers doing mind work.  The average standard of the developers is generally low (although you occasionally get one "guru").  They tend to cut corners on (or even avoid) things like version control, testing, process etc.  The company generally ships software that does do the job, but it's often ugly and buggy, and nobody seems particularly bothered.

On the other hand there are companies where the developers are just there doing what they'd otherwise be doing at home anyway; they go home in the evening and work on their own home dev projects, and and learn new languages/tech in their spare time.  The working environment is usually quite developer-centric, with good monitors, good tools, good atmosphere conducive to thinking.  The developers are often of a high standard, and you find yourself learning from all your peers on a daily basis.  They generally take things like version control, testing and process seriously.  The company generally ships decent quality software, because the developers take it personally if there are bugs or imperfections.

I don't think finding a larger company will necessarily improve matters; in fact in many cases larger companies are worse, and the problems are just deeper ingrained and more institutional.

Obviously I'm polarising things a bit....but if you're a software engineer at heart (and not just on your resume), then you need to try and find one of the latter types of company.  Think of some good interview questions you can ask them, to find out whether a company is that kind of place.

OwnerDisplayName: NeilDurant


答:
First of all, your characterization of immutable data structures is imprecise.  In general, most of a data structure is not copied, but shared, and only the changed portions are copied.  It is called a persistent data structure.  Most implementations are able to take advantage of persistent data structures most of the time.  The performance is close enough to mutable data structures that functional programmers generally consider it negligible.

Second, I find a lot of people have a fairly inaccurate idea of the typical lifetime of objects in typical imperative programs.  Perhaps this is due to the popularity of memory-managed languages.  Sit down sometime and really look at how many temporary objects and defensive copies you create compared to truly long-lived data structures.  I think you'll be surprised at the ratio.

I've had people remark in functional programming classes I teach about how much garbage an algorithm creates, then I show the typical imperative version of the same algorithm that creates just as much.  Just for some reason people don't notice it anymore.

By encouraging sharing and discouraging creating variables until you have a valid value to put in them, immutability tends to encourage cleaner coding practices and longer lived data structures.  This often leads to comparable if not lower levels of garbage, depending on your algorithm.

Title: Does immutability hurt performance in JavaScript?

Body:
There seems to be a recent trend in JavaScript towards treating data structures as immutable. For example, if you need to change a single property of an object, better to just create a whole new object with the new property, and just copy over all the other properties from the old object, and let the old object be garbage collected. (That's my understanding anyway.)

My initial reaction is, that sounds like it would be bad for performance.

But then libraries like Immutable.js and Redux.js are written by smarter people than me, and seem to have a strong concern for performance, so it makes me wonder if my understanding of garbage (and its performance impact) is wrong.

Are there performance benefits to immutability I'm missing, and do they outweigh the downsides of creating so much garbage?


答:
What you are really looking for is the sum of the highest 50% of the elements.

Given a set of n pairs, there are 2n elements. Let us split these elements into two sequences (not sets, elements can occur multiple times), s and t each containing n elements. We swap elements until it is true that for every element e in s and f in t, that e <= f.

Now you simply sum up the elements in t, giving us the result. This is very similar to sorting the elements in each pair, discarding the half of the elements that are smallest, and summing the rest.

The easy way to achieve these results is with the following algorithm:

Split each pair in n into two numbers and insert them into list t.

Sort the list t in descending order.

Sum up the first c/2 elements where c is the count of elements in the list (guaranteed to be even).


答:
If it's any consolation, I've felt exactly the same way about seniors in my work place.  Last week I filed an evidence-based report, and this week I had an hour long meeting with personnel to officially file a complaint about the managers (taking into account I'm a junior).  It was either grow some balls or be unhappy in my job.  It's not malicious complaints, it's constructive complaints.  There's a difference, and it can certainly have a huge impact on your happiness in work.

EDIT

I'd also say don't just "give up" because you have an issue with people.  It seems a lot of people in here just say "quit your job", the truth is that you can make the difference in the work place.  It sounds cheesy I know, but I think you'd make change happen and gain more respect if you're pro-active for things to change.  Don't be scared to take your boss aside and make a complaint.  I have, and already it's made a big difference.  We're adopting new technologies, changing our work procedures and more formally approaching tasks all because of what I said (and I'm a junior developer)

OwnerDisplayName: Kezzer


问:
HTTP Async/Await Task: avoid flooding server with requests?
I have a scenario where I have a Windows Store Application, there is a page with a search functionality, the user types names in a textbox and the app searches for names similar to the typed text.

Searching is done through calling a RESTful GET Api method. the call takes about 4 seconds to retrieve the results. This uses the c# async/await technique.

What I want to do is to do is to call the HTTP method as the user types, but I want to do this by cancelling any executing tasks and making sure that only one task executes at a time to avoid flooding the server with requests.

I implemented the following approach, it works fine, but I believe there can be better solution:

I hold a reference to the async task and a cancellation token source.

As the user types, I check if there is a running task, I cancel it and start a new one with the most recent search text.

```<code>private CancellationTokenSource cts = null;
private Task<List<string>> searchTask;

private async void textbox_TextChanged(object sender, TextChangedEventArgs e)
{
  //there is already running task
  if (searchTask != null && !searchTask.IsCompleted)
    {
      //cancel the running and start a new one
         cts.Cancel();
         cts = new CancellationTokenSource();
       searchTask=SearchWeb(txtbox.Text, cts.Token);
       searchTask.ContinueWith(t =>{
           //show results
       });
  }
  else
  {
      //start a new one
        searchTask = dataRepository.SearchUsers(txtbox.Text, cts.Token);
        searchTask.ContinueWith(t =>{
          //show results
      });
  }
}
</code>```

My primary concern is that I don't like the overhead generated with tracking the running tasks.  It seems like there should be a more efficient way of doing this that doesn't require cancelling so many requests.

答:
If you wish to reduce the number of starts and cancels - I would put a delay into your execution. Another approach would be to use polling but that's more overhead and more complex; and violates the good policy of "Tell, don't ask".

I think something like this may do the job of only grabbing the search string after a delay, so they can type a few characters before it grabs it and begins the execution. If execution is already occurring you still want to cancel and kick off another delay though. This way if they start and finish typing before the delay is over, it won't be cancelled and it'll execute the query right then.

```<code>private CancellationTokenSource cts = null;
private Task<List<string>> searchTask;
private Task delayTask;
private string searchString;

private async void textbox_TextChanged(object sender, TextChangedEventArgs e)
{
  searchString = txtbox.Text;

  if (delayTask != null && !delayTask.IsCompleted)
    return;

  //there is already running task
  if (searchTask != null && !searchTask.IsCompleted)
  {
    //cancel the running and start a new one
    cts.Cancel();
    cts = new CancellationTokenSource();
  }

  ExecuteQueryAfterDelay();
}

private void ExecuteQueryAfterDelay()
{
  delayTask = Task.Delay(2000).ContinueWith(t =>
  {
    searchTask=SearchWeb(searchString, cts.Token);
    searchTask.ContinueWith(t =>
    {
      //show results
    });
  });
}
</code>```

Body:
If you wish to reduce the number of starts and cancels - I would put a delay into your execution. Another approach would be to use polling but that's more overhead and more complex; and violates the good policy of "Tell, don't ask".

I think something like this may do the job of only grabbing the search string after a delay, so they can type a few characters before it grabs it and begins the execution. If execution is already occurring you still want to cancel and kick off another delay though. This way if they start and finish typing before the delay is over, it won't be cancelled and it'll execute the query right then.

private CancellationTokenSource cts = null;
private Task<List<string>> searchTask;
private Task delayTask;
private string searchString;

private async void textbox_TextChanged(object sender, TextChangedEventArgs e)
{
  searchString = txtbox.Text;

  if (delayTask != null && !delayTask.IsCompleted)
    return;

  //there is already running task
  if (searchTask != null && !searchTask.IsCompleted)
  {
    //cancel the running and start a new one
    cts.Cancel();
    cts = new CancellationTokenSource();
  }

  ExecuteQueryAfterDelay();
}

private void ExecuteQueryAfterDelay()
{
  delayTask = Task.Delay(2000).ContinueWith(t =>
  {
    searchTask=SearchWeb(searchString, cts.Token);
    searchTask.ContinueWith(t =>
    {
      //show results
    });
  });
}


问:
How to store ordered information in a Relational Database
I am trying to understand how to properly store ordered information in a relational database.

An example:

Say I have a Playlist, consisting of Songs. Inside my Relational Database, I have a table of Playlists, containing some metadata (name, creator, etc). I also have a table called Songs, containing a the playlist_id, as well as song-specific info (name, artist, duration, etc).

By default, when a new Song is added to a Playlist, it is appended to the end. When ordering on Song-ID (ascending), the order will be the order of addition. But what if a user should be able to re-order songs in the playlist?

I came up with a couple of ideas, each with their advantages and disadvantages:

A column called order, which is an integer. When a song is moved, the order of all songs between its old and new position are changed, to reflect the change. The drawback of this is that a lot of queries need to be done each time a song is moved, and the moving algorithm is not as trivial as with the other options.

A column called order, which is a decimal (NUMERIC). When a song is moved, it is assigned the floating point value between the two adjacent numbers. Drawback: Decimal fields take more space, and it might be possible to run out of precision, unless care is taken to re-distribute the range after every few changes.

Another way would be to have a previous and a next field that reference other Songs. (or are NULL in the case of the first, resp. last song in the playlist right now; Basically you create a linked-list). Drawback: Queries like 'find the Xth Song in the list' are no longer constant-time, but instead linear-time.

Which of these procedures is most often used in practice? Which of these procedures is fastest on medium to large databases? Are there any other ways to archieve this?

EDIT: For simplicities sake, in the example a Song only belongs to one Playlist (a many-to-one relationship). Of course, one could also use a Junction Table so song⟷playlist is a many-to-many relationship (and apply one of above strategies on that table).

答:
First of all, it is not clear from your description of what you have done, but you need a PlaylistSongs table which contains a PlaylistId and a SongId, describing which songs belong to which playlists.

It is in this table that you have to add ordering information.

My favorite mechanism is with real numbers.  I implemented it recently, and it worked like a charm.  When you want to move a song to a specific position, you calculate its new Ordering value as the average of the Ordering values of the previous song and the next song.  If you use an 64-bit real number, you will run out of precision at about the same time that hell will freeze over, but if you are really writing your software for posterity, then consider reassigning nice rounded integer Ordering values to all songs in each playlist every once in a while.

As an added bonus, here is the code that I have written which implements this.  Of course you cannot use it as it is, and it would be too much work for me right now to sanitize it for you, so I am only posting it for you to get ideas from it.

The class is ParameterTemplate (whatever, don't ask!)  The method gets the list of parameter templates to which this template belongs from its parent ActivityTemplate. (Whatever, don't ask!)  The code contains some guard against running out of precision.  The divisor is used for testing: the unit test uses a large divisor so as to run out of precision quickly, and thus trigger the precision guarding code. The second method is public and "for internal use only; do not invoke" so that the testing code can invoke it.  (It could not be package-private because my testing code is not in the same package as the code it tests.)  The field which controls the ordering is called Ordering, accessed via getOrdering() and setOrdering(). You don't see any SQL because I am using Object-Relational Mapping via Hibernate.

/**
 * Moves this {@link ParameterTemplate} to the given index in the list of {@link ParameterTemplate}s of the parent {@link ActivityTemplate}.
 *
 * The index must be greater than or equal to zero, and less than or equal to the number of entries in the list.  Specifying an index of zero will move this item to the top of
 * the list. Specifying an index which is equal to the number of entries will move this item to the end of the list.  Any other index will move this item to the position
 * specified, also moving other items in the list as necessary. The given index cannot be equal to the current index of the item, nor can it be equal to the current index plus
 * one.  If the given index is below the current index of the item, then the item will be moved so that its new index will be equal to the given index.  If the given index is
 * above the current index, then the new index of the item will be the given index minus one.
 *
 * NOTE: this method flushes the persistor and refreshes the parent node so as to guarantee that the changes will be immediately visible in the list of {@link
 * ParameterTemplate}s of the parent {@link ActivityTemplate}.
 *
 * @param toIndex the desired new index of this {@link ParameterTemplate} in the list of {@link ParameterTemplate}s of the parent {@link ActivityTemplate}.
 */
public void moveAt( int toIndex )
{
    moveAt( toIndex, 2.0 );
}

/**
 * For internal use only; do not invoke.
 */
public boolean moveAt( int toIndex, double divisor )
{
    MutableList<ParameterTemplate<?>> parameterTemplates = getLogicDomain().getMutableCollections().newArrayList();
    parameterTemplates.addAll( getParentActivityTemplate().getParameterTemplates() );
    assert parameterTemplates.getLength() >= 1; //guaranteed since at the very least, this parameter template must be in the list.
    int fromIndex = parameterTemplates.indexOf( this );
    assert 0 <= toIndex;
    assert toIndex <= parameterTemplates.getLength();
    assert 0 <= fromIndex;
    assert fromIndex < parameterTemplates.getLength();
    assert fromIndex != toIndex;
    assert fromIndex != toIndex - 1;

    double order;
    if( toIndex == 0 )
    {
        order = parameterTemplates.fetchFirstElement().getOrdering() - 1.0;
    }
    else if( toIndex == parameterTemplates.getLength() )
    {
        order = parameterTemplates.fetchLastElement().getOrdering() + 1.0;
    }
    else
    {
        double prevOrder = parameterTemplates.get( toIndex - 1 ).getOrdering();
        parameterTemplates.moveAt( fromIndex, toIndex );
        double nextOrder = parameterTemplates.get( toIndex + (toIndex > fromIndex ? 0 : 1) ).getOrdering();
        assert prevOrder <= nextOrder;
        order = (prevOrder + nextOrder) / divisor;
        if( order <= prevOrder || order >= nextOrder ) //if the accuracy of the double has been exceeded
        {
            parameterTemplates.clear();
            parameterTemplates.addAll( getParentActivityTemplate().getParameterTemplates() );
            for( int i = 0; i < parameterTemplates.getLength(); i++ )
                parameterTemplates.get( i ).setOrdering( i * 1.0 );
            rocs3dDomain.getPersistor().flush();
            rocs3dDomain.getPersistor().refresh( getParentActivityTemplate() );
            moveAt( toIndex );
            return true;
        }
    }
    setOrdering( order );
    rocs3dDomain.getPersistor().flush();
    rocs3dDomain.getPersistor().refresh( getParentActivityTemplate() );
    assert getParentActivityTemplate().getParameterTemplates().indexOf( this ) == (toIndex > fromIndex ? toIndex - 1 : toIndex);
    return false;
}
```


答:
Databases are optimized for certain things. Updating lots of rows quickly is one of them.  This becomes especially true when you let the database do its work.

Consider:

```<code>order song
1     Happy Birthday
2     Beat It
3     Never Gonna Give You Up
4     Safety Dance
5     Imperial March
</code>```

And you want to move Beat It to the end, you would have two queries:

update table
  set order = order - 1
  where order >= 2 and order <= 5;

update table
  set order = 5
  where song = 'Beat It'
```

<p>And that's it.  This scales up very well with very large numbers. Try putting a few thousand songs in a hypothetical playlist in your database and see how long it takes to move a song from one location to another.  As these have very standardized forms:</p>

<pre class="lang-sql prettyprint-override"><code>update table
  set order = order - 1
  where order >= ? and order <= ?;

update table
  set order = ?
  where song = ?
</code>```

You have two prepared statements that you can reuse very efficiently.

This provides some significant advantages - the order of the table is something that you can reason about. The third song has an order of 3, always. The only way to guarantee this is to use consecutive integers as the order.  Using pseudo-linked lists or decimal numbers or integers with gaps won't let you guarantee this property; in these cases the only way to get the nth song is to sort the entire table and get the nth record.

And really, this is a lot easier than you think it is. It is simple to figure out what you want to do, to generate the two update statements and for other people to look at those two update statements and realize what is being done.

Title: How to store ordered information in a Relational Database

Body:
I am trying to understand how to properly store ordered information in a relational database.

An example:

Say I have a Playlist, consisting of Songs. Inside my Relational Database, I have a table of Playlists, containing some metadata (name, creator, etc). I also have a table called Songs, containing a the playlist_id, as well as song-specific info (name, artist, duration, etc).

By default, when a new Song is added to a Playlist, it is appended to the end. When ordering on Song-ID (ascending), the order will be the order of addition. But what if a user should be able to re-order songs in the playlist?

I came up with a couple of ideas, each with their advantages and disadvantages:

A column called order, which is an integer. When a song is moved, the order of all songs between its old and new position are changed, to reflect the change. The drawback of this is that a lot of queries need to be done each time a song is moved, and the moving algorithm is not as trivial as with the other options.

A column called order, which is a decimal (NUMERIC). When a song is moved, it is assigned the floating point value between the two adjacent numbers. Drawback: Decimal fields take more space, and it might be possible to run out of precision, unless care is taken to re-distribute the range after every few changes.

Another way would be to have a previous and a next field that reference other Songs. (or are NULL in the case of the first, resp. last song in the playlist right now; Basically you create a linked-list). Drawback: Queries like 'find the Xth Song in the list' are no longer constant-time, but instead linear-time.

Which of these procedures is most often used in practice? Which of these procedures is fastest on medium to large databases? Are there any other ways to archieve this?

EDIT: For simplicities sake, in the example a Song only belongs to one Playlist (a many-to-one relationship). Of course, one could also use a Junction Table so song⟷playlist is a many-to-many relationship (and apply one of above strategies on that table).

Body:
Databases are optimized for certain things. Updating lots of rows quickly is one of them.  This becomes especially true when you let the database do its work.

Consider:

order song
1     Happy Birthday
2     Beat It
3     Never Gonna Give You Up
4     Safety Dance
5     Imperial March

And you want to move Beat It to the end, you would have two queries:

update table
  set order = order - 1
  where order >= 2 and order <= 5;

update table
  set order = 5
  where song = 'Beat It'

And that's it.  This scales up very well with very large numbers. Try putting a few thousand songs in a hypothetical playlist in your database and see how long it takes to move a song from one location to another.  As these have very standardized forms:

update table
  set order = order - 1
  where order >= ? and order <= ?;

update table
  set order = ?
  where song = ?

You have two prepared statements that you can reuse very efficiently.

This provides some significant advantages - the order of the table is something that you can reason about. The third song has an order of 3, always. The only way to guarantee this is to use consecutive integers as the order.  Using pseudo-linked lists or decimal numbers or integers with gaps won't let you guarantee this property; in these cases the only way to get the nth song is to sort the entire table and get the nth record.

And really, this is a lot easier than you think it is. It is simple to figure out what you want to do, to generate the two update statements and for other people to look at those two update statements and realize what is being done.

ContentLicense: CC BY-SA 4.0


问:
Do people in non-English-speaking countries code in English?
I've heard it said (by coworkers) that everyone "codes in English" regardless of where they're from. I find that difficult to believe, however I wouldn't be surprised if, for most programming languages, the supported character set is relatively narrow.

Have you ever worked in a country where English is not the primary language?

If so, what did their code look like?

答:
Yes, although I am Dutch I want everything on my computer to happen in English except for communicating with Dutch people or visiting Dutch sites... I really don't like the mix of Dutch and English all over the system and have thus chosen to do everything in the same language for consistency reasons.

When I was young I wanted to write everything in my own language, as for keywords...

But that just doesn't seem possible to accomplish. Maybe this is a good idea for a future language?


答:
First thing : a big warning : if you've been developing for 10 years (like I have) there's nothing else you can do better than developing. So if you want to do something else, something new, do it quickly otherwise it may be too late and you won't be good at doing something else.

Just to share my own point of view : I'm a self driven person, self taught.
I've learned alone pure C, C#, Amiga C developing, Windows, COM, Delphi, PHP, Cinema4D and now Blender and Python. I've almost always worked alone.
Here's the biggest problem I've encountered so far : small companies try to survive and you're one of the ones who keep it alive : it's very stressing but it's also rewarding : you work more, you learn faster, and a lot of stuff quickly, you make products faster (event though you often can't do your job properly). Anyway : too much stress. On the contrary, big companies will always survive but the problem is about people : too many people. It's a jungle, but far worse : nothing is clear : if  the people you work with feel like you're better than they are, they'll try their best not to help you to go up but only to shoot you because they fear for their own career. It's the way it works in France (I don't know for other countries).

To make it short : try to find a middle sized company where you feel like it's your place.
No matter how long you've been programming, no matter how long you've been working, the day when you get in your car to go to your job and you think "I'm happy to go to my work" will be the day you found your place.

It's not a question of 4-5 years or whatever.

NB : there's a huge difference between "I'm happy to go to my work" and "I'm happy to go to my work because I'll do this and that" / or / "I'm happy to go to my work because I'll learn this and that". If you think "because I'll do this and that" this means "this and that" will end one day and you may not be happy after. This is my 12 years programming experience. And I'm a senior, I'm well paid, but I do realize that 20-25 y.o. people think faster than I do. They just don't have my experience so I've just admitted I can help them to do things, they do it faster, but I help them to make them properly (which is almost never the case when you start developing).

Sorry for my English which isn't perfect, don't hesitate to correct my post to make it proper English.

OwnerDisplayName: Olivier Pons


问:
Handling changes specific to a customer
The problem

C# project consisting of WCF services used by a Flex application.

A customer may request a functionality change that requires me to alter code to work just for them. It could be a single line of code in a method or maybe a method acts in a totally different way for customer x.

My Ideas

Use branches for customers that have a customization. When a release is ready, merge to customer branches and try not to break / forget what their customization was for. We use SVN. I'm not a huge fan of this as the code base is very large.

Use inversion of control, dependency injection, and MEF. Create an interface for the class/s that needs to be modified. Create a new class library project (that is, customerabc), add a new class that implements the class just created, override method/s as needed for customer changes. Add a MEF export. I then place this in a customization folder and point MEF there. If it finds a DLL file in the folder, it uses that instead of the export from the executing assembly.

I like option 2.

Pros :

Easy - normal deployment install, then drop in their DLL file.

Obvious - it might not be overly clear wether or not a customer is running code from their branch. With this option I could just look at the customization folder.

Clean - only the files that need to be customized exist. There isn't any need for a full copy of trunk. d. It promotes better SOLID for future development and refactoring (this project has little OOP).

Cons:

It will be harder to manage changes in trunk out to the customer projects.

It doesn't necessarily solve my problem with database changes or changes on the Flex side.

If the change is a single line of code in a 500 line method, I don't see any other option than to override that method, copy paste the code to the customer override and make the one line change. This isn't good use of DRY to me, but is there a good way around this?

OK, so better use of OOP and SOLID principles could mitigate some of this, but it also means that to implement a simple customer request, I have to do some major refactoring to the whole class... potentially many classes.

What should I do?

答:
Option #1 is reasonable, but it represents a big hassle.

Option #2 should be the dictionary definition of overengineering.

Option #3: consider this:

Instead of multiple disjoint sets of requirements, (one for each client,) suppose that you have a single set of requirements, covering all customers, with the additional requirement that any given installation of your application should cater to a single customer, which is specified in configuration.

True, this means that specific customer concerns will be scattered throughout the codebase, and that every customer will be receiving inactive features, possibly even unused database tables and columns, but do they need to know?  And if they do need to know, do they care?  Do they mind?  Would they please mind their own business and let you do your job in whatever way is more productive for you?

My car engine has some hooks on it.  I have no use for these hooks, but they are there because they made it much easier to install the engine in the factory, and much safer for the workers there, so the presence of these hooks has actually lowered the end price that I paid for my car.  So, I am perfectly fine with these otherwise useless hooks.

Title: Handling changes specific to a customer


问:
Getting a Candidate to do a Pull Request Review in an Interview
I was wondering if somebody has done this, and to what success compared to the classic method of getting them to solve a problem as you watch on?

Any pointers on how to carry this out in an interview situation?

I was thinking about doing this for mid/senior candidates which should definitely have the technical knowledge to do a pull request. I was thinking it may be a good character test, if they give a full fleshed out and kind response, as apposed to "NO!". You may really see how they will get on in a team.

Edit:

The way I was thinking about doing this, was to send a candidate a small piece of code before the test. They could read through it, ensure they understand what it does and any technologies it uses (I hate interviews that confuse peoples ability to program with how well they know an API).

Then when the candidate comes to the interview you give them the pull request, using Jira to do a git pull request. Since the web interface just shows you what has changed and the ability to view the raw before and after I do not think this would be large barrier to entry.

My core theory of this was to try to remove the memory game test/pressure cooker a lot of interviews can be and try to let somebody show how they would work on a daily basis with you.

To summarise, I want to know if any one is currently doing/done something similar and what outcomes they see. Also any pain points that became evident when you started this process.

Aside:

Thank to everyone who has answered so far they are helpful and very valid but my original intent with this idea was to stop interviews looking for cookie cutter programmers and to let raw talent shin. I wanted to test this theory here with fellow programmers that would possibly go through such a process.

答:
I'd say no to this. Reviewing anything but a trivial pull request (spelling fixes) requires an understanding of the code base. If you do not know the codebase, you may not be aware that a certain (obviously good) change might have some major effects somewhere else. Even veterans to a codebase can miss the repercussions of 'simple' change.

What you want to measure is how they will get along with the team. This requires them to get along on a personal level, as well as them being flexible enough to adjust to the flow of the team. To figure that out, talking to them works pretty well.


问:
User roles array vs string
I'm working on a meanjs project.  The user roles are set as an array:

roles = ['admin', 'user', 'manager'];

why not just set it like this?

role = 'admin'

Why is the convention of an array used rather than just setting a role value as a string?

答:
The whole idea of role based access control is that any user can fulfil multiple roles. In simple system, each role may be a superset of another role, e.g. admin > manager > user. But this doesn't have to be the case.

The most obvious example is when we have multiple instances of a resource, and a role only applies to a single instance. E.g. one user could be admin on server-5 and server-3, but not on any other server where they only are a user. This would necessitate a setup like

```<code>roles = ['admin-5', 'admin-3', 'user']
</code>```

It is also possible that a system has multiple roles that are separate rather than subsets of permissions. E.g. an FTP server can be configured so that anyone may upload data but not download it, but also in a way that everyone can download data but not upload any. So different users would have different role sets:

```<code>user1 = ['uploader']
user2 = ['downloader']
user3 = ['uploader', 'downloader']
</code>```

So why don't we create a super-role that has both permissions? Why not user3 = ['fullaccess']? When permissions are connected to a single role rather than a group of roles, this makes permissions easier to manage (issue, view, revoke) and to implement. To check whether a user may upload:

```<code>if UPLOADER in roles:
  ok
</code>```

With roles that inherit from another, this would either become

```<code>if role == UPLOADER or role == FULLACCESS:
  ok
</code>```

which is difficult to extends when creating a new super-role that includes the uploader permission.

Or if we introduce an inheritance system where each permission may involve a complicated check:

```<code>if role.permits(UPLOAD):
  ok
</code>```

Clearly, these are more complicated. When implementing security, complexity is dangerous. Too easy to introduce a hard-to-see bug that grants anyone permission.

Considering that RBAC is simple for both users and developers, it's not surprising it is common in state-of-the-art systems.

(This answer does not discuss the difference between roles and permissions, which introduces an additional layer of mappings.)

Title: User roles array vs string

Body:
The whole idea of role based access control is that any user can fulfil multiple roles. In simple system, each role may be a superset of another role, e.g. admin > manager > user. But this doesn't have to be the case.

The most obvious example is when we have multiple instances of a resource, and a role only applies to a single instance. E.g. one user could be admin on server-5 and server-3, but not on any other server where they only are a user. This would necessitate a setup like

roles = ['admin-5', 'admin-3', 'user']

It is also possible that a system has multiple roles that are separate rather than subsets of permissions. E.g. an FTP server can be configured so that anyone may upload data but not download it, but also in a way that everyone can download data but not upload any. So different users would have different role sets:

user1 = ['uploader']
user2 = ['downloader']
user3 = ['uploader', 'downloader']

So why don't we create a super-role that has both permissions? Why not user3 = ['fullaccess']? When permissions are connected to a single role rather than a group of roles, this makes permissions easier to manage (issue, view, revoke) and to implement. To check whether a user may upload:

if UPLOADER in roles:
  ok

With roles that inherit from another, this would either become

if role == UPLOADER or role == FULLACCESS:
  ok

which is difficult to extends when creating a new super-role that includes the uploader permission.

Or if we introduce an inheritance system where each permission may involve a complicated check:

if role.permits(UPLOAD):
  ok

Clearly, these are more complicated. When implementing security, complexity is dangerous. Too easy to introduce a hard-to-see bug that grants anyone permission.

Considering that RBAC is simple for both users and developers, it's not surprising it is common in state-of-the-art systems.

(This answer does not discuss the difference between roles and permissions, which introduces an additional layer of mappings.)