答:
You can't make anyone change who doesn't already want to change.  Which is why the teams you have coached have reverted to their old ways.

So really, your question should be "how do I make developers want to change to OO approaches?"

Start simple, start small, let things build from there.  Show the benefits to the individual developer instead of abstract or philosophical benefits to the code, other developers, or the company.

Show how the various OO techniques will lead to less code that they have to write as well as faster development times.  Almost any developer will listen to a proposal that will make their job easier.

Then start demonstrating how OO techniques will lead to more easily maintained code.  Everyone in that type of environment lives in fear of making "the change" that obliterates a third of the production application.  Encapsulation is the key to avoiding this risk and allows each (soon-to-be) layer of the application maintain its contract with the other layers.

I would look at how data is being passed around.  If it's a long list of variables every time, consider wrapping some of those up within a structure (or gasp! a class!!!) as a preliminary step.  Simply wrapping the data within an object is the beginnings of contracts between layers.

At a broader level - consider getting management buy-in for this effort if you haven't already.  Management needs to see the concrete benefits of decreased defects and lower risk from making changes.  Eventually, the refactoring process should lead to faster development times, but that is a long term benefit.

Your ideas of review teams and OO evangelists are good ones.  It needs to be more than just you who is pushing this agenda.

Title: Tips about how to spread Object Oriented practices

Body:
I work for a medium company that has around 250 developers. Unfortunately, lots of them are stuck in a procedural way of thinking and some teams constantly deliver big Transactional Script applications, when in fact the application contains rich logic. They also fail to manage the design dependencies, and end up with services which depend on another large number of services (a clean example of Big Ball of Mud).

My question is: Can you suggest how to spread this type of knowledge?

I know that the surface of the problem is that these applications have a poor architecture and design. Another issue is that there are some developers who are against writing any kind of test.

A few things I'm doing to change this (but I'm either failing or the change is too small are)

Running presentations about design principles (SOLID, clean code, etc).

Workshops about TDD and BDD.

Coaching teams (this includes using sonar, findbugs, jdepend and other tools).

IDE & Refactoring talks.

A few things I'm thinking to do in the future (but I'm concern that they might not be good)

Form a team of OO evangelists, who disseminate an OO way of thinking in differet teams (these people would need to change teams every few months).

Running design review sessions, to criticise the design and suggest improvements (even if the improvements are not done because of time constraints, I think this might be useful)

.

Something I found with the teams I coach, is that as soon as I leave them, they revert back to the old practices. I know I don't spend a lot of time with them, usually just one month. So whatever I'm doing, it doesn't stick.

I'm sorry this question is spattered with frustration, but the alterative to write this was to hit my head on the wall until I pass out.


问:
What is a good way to keep track of strings for dictionary lookups?
I am working through the Windows 8 app tutorial.

They have some code about saving app data like so:

```<code> private void NameInput_TextChanged(object sender, TextChangedEventArgs e)
        {
            Windows.Storage.ApplicationDataContainer roamingSettings =
                Windows.Storage.ApplicationData.Current.RoamingSettings;
            roamingSettings.Values["userName"] = nameInput.Text;
        }
</code>```

I have worked with C# in the past and found that things like using constant string values (like "userName" in this case) for keys could get messy because auto-complete did not work and it was easy to forget if I had made an entry for a setting before and what it was called. So if I don't touch code for awhile I end up accidentally creating multiple entries for the same value that are named slightly differently.

Surely there is a better way to keep track of the strings that key to those values. What is a good solution to this problem?

答:
It may depend on the speed and memory performance that you need.

I have been thinking a lot about radix sort recently.  It has some great characteristics. It could be a great candidate for anything where you might want to implement your own auto complete.  Speed wise it is O(kN) complexity where k is the length of the strings and N is the number of strings.  Storage is also O(kN).

Unlike a STL vector, a linked list, or an array of strings, maintaining a sorted table does not involve a linear search for an insertion point because the data is in a tree with branches that are indexed by the letters in the string which is a constant time operation repeated once for each character in the string.  Checking to see if a string is in the table takes the same or less effort, terminating when one of the letters indexes a NULL pointer.  Finding the first M strings to display for an auto complete is similarly fast.

A hash table can also be a great tool for dictionary lookup.

ContentLicense: CC BY-SA 3.0


问:
Tips about how to spread Object Oriented practices
I work for a medium company that has around 250 developers. Unfortunately, lots of them are stuck in a procedural way of thinking and some teams constantly deliver big Transactional Script applications, when in fact the application contains rich logic. They also fail to manage the design dependencies, and end up with services which depend on another large number of services (a clean example of Big Ball of Mud).

My question is: Can you suggest how to spread this type of knowledge?

I know that the surface of the problem is that these applications have a poor architecture and design. Another issue is that there are some developers who are against writing any kind of test.

A few things I'm doing to change this (but I'm either failing or the change is too small are)

Running presentations about design principles (SOLID, clean code, etc).

Workshops about TDD and BDD.

Coaching teams (this includes using sonar, findbugs, jdepend and other tools).

IDE & Refactoring talks.

A few things I'm thinking to do in the future (but I'm concern that they might not be good)

Form a team of OO evangelists, who disseminate an OO way of thinking in differet teams (these people would need to change teams every few months).

Running design review sessions, to criticise the design and suggest improvements (even if the improvements are not done because of time constraints, I think this might be useful)

.

Something I found with the teams I coach, is that as soon as I leave them, they revert back to the old practices. I know I don't spend a lot of time with them, usually just one month. So whatever I'm doing, it doesn't stick.

I'm sorry this question is spattered with frustration, but the alterative to write this was to hit my head on the wall until I pass out.

答:
I agree with Shuvo Naser. It's a big hurdle, so treat it more like a climb. Learning how to design an entire application using OOP is going to take time.

Identify those that understand OOP and move them closer to team leaders, trainers, designers, code reviewers, etc.

Use an existing project as a training reference. Possibly those in #1 were on that team.

Refactor some existing projects. This can help some people build a bridge between their procedural code to OO code. Start with basics. They have to see when, where and why you use these principles.

Involve them in design sessions with people who know what they're doing.

Put them on dev teams with someone who can provide design guidance and make sure the project sticks to OO principles (Assuming the reason you want to do this in the first place is because it will improve developement.).

Adoption rarely precedes seeing the benefits. We're talking about complex design and not using T.P.S. Report Cover Sheets.


答:
I would encapsulate it somehow. Maybe like this:

```<code>class Settings
{
    Windows.Storage.ApplicationDataContainer roamingSettings;

    public Settings()
    {
        roamingSettings =  Windows.Storage.ApplicationData.Current.RoamingSettings;
    }

    public String UserName {
        get { return (String)roamingSettings.Values["userName"]; }
        set { roamingSettings.Values["userName"] = value ;}
    }

    // etc..
}
</code>```

The value is only in one place, so you can always check what you have already declared. It enables intellisense and it makes it type safe. You could also make the whole thing static or singleton.

Title: What is a good way to keep track of strings for dictionary lookups?

Body:
I am working through the Windows 8 app tutorial.

They have some code about saving app data like so:

```<code> private void NameInput_TextChanged(object sender, TextChangedEventArgs e)
        {
            Windows.Storage.ApplicationDataContainer roamingSettings =
                Windows.Storage.ApplicationData.Current.RoamingSettings;
            roamingSettings.Values["userName"] = nameInput.Text;
        }
</code>```

I have worked with C# in the past and found that things like using constant string values (like "userName" in this case) for keys could get messy because auto-complete did not work and it was easy to forget if I had made an entry for a setting before and what it was called. So if I don't touch code for awhile I end up accidentally creating multiple entries for the same value that are named slightly differently.

Surely there is a better way to keep track of the strings that key to those values. What is a good solution to this problem?

Body:
I would encapsulate it somehow. Maybe like this:

class Settings
{
    Windows.Storage.ApplicationDataContainer roamingSettings;

    public Settings()
    {
        roamingSettings =  Windows.Storage.ApplicationData.Current.RoamingSettings;
    }

    public String UserName {
        get { return (String)roamingSettings.Values["userName"]; }
        set { roamingSettings.Values["userName"] = value ;}
    }

    // etc..
}

The value is only in one place, so you can always check what you have already declared. It enables intellisense and it makes it type safe. You could also make the whole thing static or singleton.


问:
How important is it for a programmer to know how to implement a QuickSort/MergeSort algorithm from memory?
I was reviewing my notes and stumbled across the implementation of different sorting algorithms.

As I attempted to make sense of the implementation of QuickSort and MergeSort, it occurred to me that although I do programming for a living and consider myself decent at what I do, I have neither the photographic memory nor the sheer brainpower to implement those algorithms without relying on my notes. All I remembered is that some of those algorithms are stable and some are not. Some take O(nlog(n)) or O(n^2) time to complete. Some use more memory than others...

I'd feel like I don't deserve this kind of job if it weren't because my position doesn't require that I use any sorting algorithm other than those found in standard APIs. I mean, how many of you have a programming position where it actually is essential that you can remember or come up with this kind of stuff on your own?

答:
I'm of two minds on this subject. I know many programmers who don't know what a sorting algorithm is, but do their job fairly well. I also believe in understanding principles in order to truly understand the domain.

It is hard for me to have an unbiased answer on this subject as I have been programming for so long I have probably forgotten more algorithms that I currently know - but I still know the sorting ones mentioned in this question. I think the thought leaders in Agile (e.g. Ron Jeffries, Alistair Cockburn) have some good ideas near this idea (e.g. Shu-Ha-Ri).

In summary to this rambling answer: Definitely use the API (NIH is a sign of developer immaturity), but always understand the underlying principles. I hope this helps.


问:
Rule engine in .NET
I have to import data from Excel to an SQL database. The Excel data contains various parameters and their values, like P1, P1, P4, P5, etc. I have to apply business rules. Like if (P1 > 100 and P1 < 200) then insert the record in database. Similarly, in some cases, string values are also validated. Can I have any open source rule engine that contains UI to change, add, and delete the rules? I am using C# to read the Excel data and and inserting the records.

One more thing, which of the following is the best approach?

Read Excel data first and store every record as an object in a collection, iterate through the collection, apply business rules on every object and insert record in the database. Or

Read one record from Excel, apply business rules and insert the record in the database. Repeat the process for the whole Excel spreadsheet.

答:
You may look for different simple rule engines in .NET. However, in the case if you're using .NET 3.0 or later, you can use the Rules Engine of Windows Workflow Foundation without using actually Workflow.

We have done this in our project, and you are free to use SQL or XML as the back-end, and it works pretty good. You can use the IDE that comes with the Workflow examples and use that in your own application.

You may also consider the following options:

Open Source Rule Engines in C#

Simple Rule Engine from SourceForge site

Title: Rule engine in .NET


问:
Understanding clojure keywords
I'm taking my first steps with Clojure. Otherwise, I'm somewhat competent with JavaScript, Python, Java, and a little C.

I was reading this artical that describes destructuring vectors and maps. E.g.

```<code>=> (def point [0 0])
=> (let [[x y] point]
=>   (println "the coordinates are:" x y))
the coordinates are: 0 0
</code>```

but I'm having a difficult time understanding keywords. At first glance, they seem really simple, as they just evaluate to themselves:

```<code>=> :test
:test
</code>```

But they seem to be used is so many different ways and I don't understand how to think about them. E.g., you can also do stuff like this:

```<code>=> (defn full-name [& {first :first last :last}]
=>   (println first last))
=> (full-name :first "Tom" :last "Brennan")
Tom Brennan
nil
</code>```

This doesn't seem intuitive to me. I would have guessed the arguments should have been something more like:

```<code>(full-name {:first "Tom" :last "Brennan"})
</code>```

because it looks like in the function definition that you're saying "no required arguments, but a variable number of arguments comes in the form of a single map". But it seems more like you're saying "no required arguments, but a variable number of arguments comes which should be a list of alternating keywords and values... ?" I'm not really sure how to wrap my brain around this.

Also, things like this confuse me too:

```<code>=> (def population {:humans 5 :zombies 1000})
=> (:zombies population)
1000
=> (population :zombies)
1000
</code>```

How do maps and keywords suddenly become functions?

If I could get some clarification on the use of keywords in these two examples, that would be really helpful.

Update I've also seen https://stackoverflow.com/questions/3337888/clojure-named-arguments and while the accepted answer is a great demonstration of how to use keywords with destructuring and named arguments, I'm really looking more for understanding how to think about them--why the language is designed this way and how I can best internalize their use.

答:
the & in (defn full-name [& {first :first last :last}] indicates that all the remaining arguments should be gathered into a single collection. the map destructuring form following the & then takes the map created by the & apart again.

```<code>(defn full-name [& {first :first last :last}]...)
(full-name :first "Tom" :last "Brennan")
</code>```

without the & becomes

```<code> (defn full-name [{first :first last :last}]...)
(full-name {:first "Tom" :last "Brennan"})
</code>```

for your other question: symbols and keywords implement the function interface so they can be called just like any other Clojure function. when called as a function they look themselves up in their argument which they expect to be a map (or something implementing the appropriate interface)

Title: Understanding clojure keywords

Body:
the & in (defn full-name [& {first :first last :last}] indicates that all the remaining arguments should be gathered into a single collection. the map destructuring form following the & then takes the map created by the & apart again.

(defn full-name [& {first :first last :last}]...)
(full-name :first "Tom" :last "Brennan")

without the & becomes

 (defn full-name [{first :first last :last}]...)
(full-name {:first "Tom" :last "Brennan"})

for your other question: symbols and keywords implement the function interface so they can be called just like any other Clojure function. when called as a function they look themselves up in their argument which they expect to be a map (or something implementing the appropriate interface)


问:
Finding Database issue for iphone app
I know this is a dumb question and it might not be "conceptual" but, as a self-starter I really want to know how to get connected to some sort of "commercial database"?

I'm designing a local gas station utility app for iphone and ipad. I have absolutely no clue how to find a relevant database. For example, if I want to make an app for pizza ordering in the great Chicago area. How do I get info (price, menu, location, etc.) of those pizza stores scattered all over Chicago?

Can any one shed some light on this for me?

答:
I have absolutely no clue how to find a relevant database.

There are three ways you could go:

1. Find an existing data source. Sometimes you can use data that other people/companies make available, either for free or by licensing it. The government is a great source for some kinds of data -- various agencies like the National Weather Service, NOAA, USGS, and many others provide lots of free and easily accessible information. I'm not aware of a National Pizza Price Registry, so the government probably won't help much in your example. You might need to look for a proprietary database that you can license.

2. Develop your own data source. I'm sure there are quite a few pizza joints in Chicago, and calling them all up periodically to gather their prices will be a lot of work. Many of those places will have web sites, though, so maybe you can automate part of that data collection, and the work to fill in the holes may be worth the effort to you.

3. Get other people to build your database for you. Crowd sourcing data to improve your database is a popular way to go, especially among small developers, because it lets you cover lots of ground at zero cost to you. It works best for sites like Twitter, Facebook, and StackOverflow where the data is naturally provided by the users. But it can also work for gathering other kinds of data, especially if helping to update the data has some benefit to the users. For example, your pizza app might let users review their pie consuming experiences, and your review submission process might include questions like 'what kind of pizza did you order?' and 'how much did your pizza cost?'. Users get to express their opinions, and you get the pizza price data you're looking for.

Title: Finding Database issue for iphone app


问:
Whiteboard "testing" during an interview: legitimate way to back up your (whiteboard) code?
As I get it, having an error (even a typo like or missing ";") in your whiteboard code will often cost you some interview points. Avoiding that will inevitably make one proof-reading code again and again (losing time and possibly neural energy / concentration) or even using a simpler (and thus less effective) algorithm -- and both these ways are "costly" again!

So, why not just fast write code as elegant and effective as you would having a (unit) testing framework at your disposal and then just normally test it it (just on the whiteboard)?

Has anyone tried / seen this approach? Is the whole idea worthy?

[this also applies to the pen-and-paper case of course]

答:
I use this approach when taking tests for school. I first write the function, then off to the side I write a little table of inputs, outputs, and vars. I've caught a few stupid errors this way. Testing, even on-paper/whiteboard testing, is always better than not testing.

I disagree with freaking out over semicolons in a professional setting, though.

ContentLicense: CC BY-SA 2.5


问:
Greenspun's Tenth Rule, does every large project include a Lisp interpreter?
Greenspun's tenth rule (actually the only rule) states that:

Any sufficiently complicated C or Fortran program contains an ad hoc, informally-specified, bug-ridden, slow implementation of half of Common Lisp.

My memory is that there are some papers on the topic, perhaps for Borland's Quattro (spreadsheet) project and possibly others.  Google is unhelpful, maybe the right search terms are not coming to mind.  I am looking for papers or articles supporting this claim, if any.

答:
That may be true, as others stated, many programs require configurability and so do contain various lisp-like interpreters.

However the statement also implies with a smirk that all you need to make a program is Lisp, and that all other languages are inferior to it.

But it's wrong, Lisp may be expressive, but it's also too abstract, it tries to hide details of a platform and pretend nothing but lists exist in computer world.

The reality of high performance programming, is that sometimes we do need to mingle with bits and bytes, and do OS specific stuff, so it's not possible to do everything with just Lisp as statement implies.

It is rather the other way around, no matter how complicated, clever or sophisticated of a language you invent, all it ends up being is just another way to write assembly.


答:
Don't try to push OOP, it will only make things worse. Not because OOP is a bad idea in general: it's not. But because it seems that whoever is responsible for that hairball code not only lacks the tools to avoid these problems, but also the experience and, more importantly, the motivation.

People who have a desire to write clean code will do so in any given paradigm, be it OOP, procedural, functional, etc. But not all programmers are like this, and if you push any clean-code tool onto people who don't understand the need, you will end up with these tools abused just like the tools that are already there. You will see unrelated methods grouped into a class, classes inheriting from unrelated classes, method visibility set based on trial-and-error debugging rather than conscious design, static methods that shouldn't be static and non-static methods that should, in short, you'll be wasting your time.

Instead, see if you can invest in raising consciousness for maintainability and elegance. After all, core goals of OOP aren't any different from any other complexity-management strategy (which is what programming paradigms are really all about): encapsulation, modulatisation, loose coupling, low degree of interdependency, keeping mutable state and its scope to a minimum, etc. etc. OOP certainly isn't the only paradigm that gives you the tools you need to achieve this.

Which brings me to the last point: OOP is a great idea, and it works well for certain kinds of problems, but (and I'm speaking both from my own experience and paraphrasing the opinion of some great minds here) for many kinds of problems, it is completely unsuitable. When you're fresh into OOP, and semi-procedural spaghetti code is the only alternative you're familiar with, OOP naturally appears as a gift from heaven (and in a relative way, it is), and you're likely to approach all problems as nails for your OOP hammer. That's only natural, and driving OOP to (and beyond) its limitations a good way to build up your OOP skills, so it's not all negative. But maybe (just maybe) this particular codebase does not need OOP after all. Maybe it would benefit much more from a procedural architecture, albeit a more structured one with proper design, a decent project layout, the right abstractions, and appropriate encapsulation.

TL;DR: If you want to evangelize anything at all, let it be (platform-agnostic) good coding practices, not any particular paradigm or methodology.


问:
Software bug/defect classification
We're trying to come up with terms that better describe our bugs/defects.  To us, the term 'bug' or 'defect' is too generic and doesn't accurately reflect what is happening.  For example, instead of saying that there is a bug (in the general sense), we'd rather say what type of bug (an error, or enhancement, or improvement, etc.).  What names do you use for describing 'bugs'?

We found http://www.softwaredevelopment.ca/bugs.shtml which has some pretty good classifications.  How do you classify them?

答:
Code Complete's "Developer Testing" section classifies defects as:

Structural

Data

Functionality as Implemented

Construction

Integration

Functional requirements

Test definition or execution

System, software architecture

Unspecified

(this is originally from a 1990 Beizer study)

Of course, the defect's class may only be evident with hindsight of it being fixed!

This classification scheme keeps the type of the defect distinct from its severity (serious -> insignificant) and priority (I need it yesterday -> pipe dream).

Also, this assumes the ticket has already been classified as a DEFECT, rather than an ENHANCEMENT or TASK (which are very standard types of ticket).

OwnerDisplayName: Dustin K

Title: Software bug/defect classification


答:
Something like orthogonal defect classification might be useful. The idea is to have a series of options such that each one only has a single possible value that makes sense for any and every submission. IBM Research has a few web pages devoted to the concept.

As far as types of defects, the only thing that I really want to know is if it's a defect or if it's an enhancement. A defect exists in a work product that doesn't conform to the artifact that generated it. For example, a requirement that doesn't adequately capture the intention of the stakeholder is defective. Similarly, a design that doesn't address a requirement or an implementation that doesn't realize a design or a requirement is defective. An enhancement or change request would be a modification to an artifact that otherwise meets the specified requirements. This could be a new requirement or a refactoring to reduce technical debt, as examples.

I believe that any comprehensive classification scheme would address the following questions. What you actually need to capture varies based on how you are using the data - using defect data to track tasks needs to be less complete than defect data that is an input into a rigorous metrics program.

Is this a problem in an existing work product or an enhancement to a work product? Either something, whether it's a requirement, a document, or a code module, doesn't conform to specifications or the specifications upon which the work products are based must be enhanced.

What work product is affected? Does this affect a document (and which one)? Is it against code? Unit tests? Depending on your release process, it might also address which version(s) are affected by this defect or enhancement as an additional data element.

What is the priority and/or severity of this defect? Priority is generally a customer or user-defined element that addresses how quickly they need a resolution of some kind. Defects of higher priority need to be addressed first. Note that "addressed" might simply mean analyzing and developing a work-around until a defect can be adequately resolved. Severity typically defines how work is impacted - low severity means that it has a minimal impact on the business value, while a higher severity means that the users are having extreme difficulty accomplishing their business objectives because of the defect.

When was the defect reported? When was it finally resolved? This can be used to track time-to-closure data, which might be related to customer satisfaction metrics.

Where was the defect found and where was it originally injected? This is really only required if you're capturing defect containment metrics, such as total defect containment (the number of defects found/fixed in house versus post-release) or phase containment effectiveness. I prefer an activity-based concept (something along the lines of requirements, architecture/design, implementation, testing, field), neglecting iterations, so that I can determine which activities are associated with processes that are proficient at capturing defects and where I need to improve my processes.

What caused this defect to exist or be injected? How you define this depends on your organization, but the PSP gives a good starting point. You don't want anything too complicated such that it can't be understood, and in line with Orthogonal Defect Classification, don't want defects to be able to be put into multiple categories.

How can I identify and refer to this defect? Typically, a unique identifier and a short, human-readable title provide this information. These can be used to provide information to customers/users about work-in-progress and in status reports or commit logs to help track defects to closure.

Most of these, except for the reason for existing, apply equally to defects and enhancements.

Title: Software bug/defect classification

Body:
We're trying to come up with terms that better describe our bugs/defects.  To us, the term 'bug' or 'defect' is too generic and doesn't accurately reflect what is happening.  For example, instead of saying that there is a bug (in the general sense), we'd rather say what type of bug (an error, or enhancement, or improvement, etc.).  What names do you use for describing 'bugs'?

We found http://www.softwaredevelopment.ca/bugs.shtml which has some pretty good classifications.  How do you classify them?


答: Any sufficiently complex system will have domain specific concepts and requirements that are extremely hard to express with the abstractions of the language you are working in. This inadvertently forces programmers to create domain specific abstractions to ease the burden of bridging the semantic gap between the programming language and the specific domain. Once there are enough of these abstractions you basically have an interpreter of a domain specific language. This is an unavoidable part of software development.</p>

Title: Greenspun's Tenth Rule, does every large project include a Lisp interpreter?

Body:
Greenspun's tenth rule (actually the only rule) states that:

Any sufficiently complicated C or Fortran program contains an ad hoc, informally-specified, bug-ridden, slow implementation of half of Common Lisp.

My memory is that there are some papers on the topic, perhaps for Borland's Quattro (spreadsheet) project and possibly others.  Google is unhelpful, maybe the right search terms are not coming to mind.  I am looking for papers or articles supporting this claim, if any.


问:
What corners have have you cut at work?
Have you ever let your coding standards slip to meet deadlines or save time?

I'll start with a trivial example:

We came up with a coding standard at work around which brackets/formatting to use and so on. I ignore it and use the auto-format tool in netbeans.

答:
Writing methods to do one thing instead of all permutations and exceptions of it.

Sometimes I have to cut corners by coding a method to be very specific to what I'm currently releasing.  A very basic example would be if you have a requirement to write a method to take an order and ship it to somewhere from a warehouse.  Sometimes I write the code so that it is with the assumption that everything comes from that single warehouse.  But what about when they get a new warehouse?  Or 5?  We're growing, so it may be soon, but to do multiples it would require methods to translate inventory and closeness of a warehouse to which warehouse would actually ship the goods. What if one is out of the product?  Do you wait since it's the closest?  Do you just ship from the other if they have it?  What about shipping costs?  Is it worth it?  That's way too much extra coding on a deadline.  Sometimes I have to cross that bridge when I come to it.

Title: What corners have have you cut at work?


问:
What algorithms can I use to detect if articles or posts are duplicates?
I'm trying to detect if an article or forum post is a duplicate entry within the database. I've given this some thought, coming to the conclusion that someone who duplicate content will do so using one of the three (in descending difficult to detect):

simple copy paste the whole text

copy and paste parts of text merging it with their own

copy an article from an external site and masquerade as their own

Prepping Text For Analysis

Basically any anomalies; the goal is to make the text as "pure" as possible.
For more accurate results, the text is "standardized" by:

Stripping duplicate white spaces and trimming leading and trailing.

Newlines are standardized to \n.

HTML tags are removed.

Using a RegEx called Daring Fireball URLs are stripped.

I use BB code in my application so that goes to.

(ä)ccented and foreign (besides Enlgish) are converted to their non foreign form.

I store information about each article in (1) statistics table and in (2) keywords table.

(1) Statistics Table
The following statistics are stored about the textual content (much like this post)

text length

letter count

word count

sentence count

average words per sentence

automated readability index

gunning fog score

For European languages Coleman-Liau and Automated Readability Index should be used as they do not use syllable counting, so should produce a reasonably accurate score.

(2) Keywords Table

The keywords are generated by excluding a huge list of stop words (common words), e.g., 'the', 'a', 'of', 'to', etc, etc.

Sample Data

text_length, 3963

letter_count, 3052

word_count, 684

sentence_count, 33

word_per_sentence, 21

gunning_fog, 11.5

auto_read_index, 9.9

keyword 1, killed

keyword 2, officers

keyword 3, police

It should be noted that once an article gets updated all of the above statistics are regenerated and could be completely different values.

How could I use the above information to detect if an article that's being published for the first time, is already existing within the database?

I'm aware anything I'll design will not be perfect, the biggest risk being (1) Content that is not a duplicate will be flagged as duplicate (2) The system allows the duplicate content through.

So the algorithm should generate a risk assessment number from 0 being no duplicate risk 5 being possible duplicate and 10 being duplicate. Anything above 5 then there's a good possibility that the content is duplicate. In this case the content could be flagged and linked to the article's that are possible duplicates and a human could decide whether to delete or allow.

As I said before I'm storing keywords for the whole article, however I wonder if I could do the same on paragraph basis; this would also mean further separating my data in the DB but it would also make it easier for detecting (2) in my initial post.

I'm thinking weighted average between the statistics, but in what order and what would be the consequences...

答: There are many algorithms that deal with document similarity in NLP. Here's a seminal paper describing various algorithms. Also wikipedia has a larger collection. I favor the Jaro Winkler measure and have used it for grad school projects in aglomerative clustering methods.

OwnerDisplayName: Michael Rich


答:
A first go at this might be to detect sentences (or some other reasonable block of data. Take those blocks and strip any mete data, html random white space, returns etc.  Take an MD5 of result and store it in a table.  You could then match against these blocks to try to find matches.

If this does not work you might try n-grams.  Here you need one entry of each word on the page, but it should be able to give you pretty good matches.

http://en.wikipedia.org/wiki/N-gram

Title: What algorithms can I use to detect if articles or posts are duplicates?

Body:
I'm trying to detect if an article or forum post is a duplicate entry within the database. I've given this some thought, coming to the conclusion that someone who duplicate content will do so using one of the three (in descending difficult to detect):

simple copy paste the whole text

copy and paste parts of text merging it with their own

copy an article from an external site and masquerade as their own

Prepping Text For Analysis

Basically any anomalies; the goal is to make the text as "pure" as possible.
For more accurate results, the text is "standardized" by:

Stripping duplicate white spaces and trimming leading and trailing.

Newlines are standardized to \n.

HTML tags are removed.

Using a RegEx called Daring Fireball URLs are stripped.

I use BB code in my application so that goes to.

(ä)ccented and foreign (besides Enlgish) are converted to their non foreign form.

I store information about each article in (1) statistics table and in (2) keywords table.

(1) Statistics Table
The following statistics are stored about the textual content (much like this post)

text length

letter count

word count

sentence count

average words per sentence

automated readability index

gunning fog score

For European languages Coleman-Liau and Automated Readability Index should be used as they do not use syllable counting, so should produce a reasonably accurate score.

(2) Keywords Table

The keywords are generated by excluding a huge list of stop words (common words), e.g., 'the', 'a', 'of', 'to', etc, etc.

Sample Data

text_length, 3963

letter_count, 3052

word_count, 684

sentence_count, 33

word_per_sentence, 21

gunning_fog, 11.5

auto_read_index, 9.9

keyword 1, killed

keyword 2, officers

keyword 3, police

It should be noted that once an article gets updated all of the above statistics are regenerated and could be completely different values.

How could I use the above information to detect if an article that's being published for the first time, is already existing within the database?

I'm aware anything I'll design will not be perfect, the biggest risk being (1) Content that is not a duplicate will be flagged as duplicate (2) The system allows the duplicate content through.

So the algorithm should generate a risk assessment number from 0 being no duplicate risk 5 being possible duplicate and 10 being duplicate. Anything above 5 then there's a good possibility that the content is duplicate. In this case the content could be flagged and linked to the article's that are possible duplicates and a human could decide whether to delete or allow.

As I said before I'm storing keywords for the whole article, however I wonder if I could do the same on paragraph basis; this would also mean further separating my data in the DB but it would also make it easier for detecting (2) in my initial post.

I'm thinking weighted average between the statistics, but in what order and what would be the consequences...


问:
Is it bad practice to pass instances through several layers?
In my program design, I often come to the point where I have to pass object instances through several classes. For example, if I have a controller that loads an audio file, and then passes it to a player, and the player passes it to the playerRunnable, which passes it again somewhere else etc. It looks kind of bad, but I don´t know how to avoid it. Or is it OK to do this?

EDIT: Maybe the player example is not the best because I could load the file later, but in other cases that does not work.

答:
As the other answers have pointed out, this is not an inherently poor design.  It can create tight coupling between the nested classes and those that nest them, but loosening the coupling may not be a valid option if nesting the references provides a value to the design.

One possible solution is to "flatten" the nested references in the controller class.

Instead of passing a parameter several times through nested objects, you could maintain in the controller class references to all of the nested objects.

How exactly this is implemented (or if it is even a valid solution) is dependent on the current design of the system, such as:

Are you able to maintain some sort of map of the nested objects in the controller without it getting too complicated?

When you pass the parameter to the appropriate nested object, can the nested object recognize the parameter immediately, or was there additional functionality occurring while passing it through the nested objects?

etc.

This is an issue that I encountered in an MVC design pattern for a GXT client.  Our GUI components contained nested GUI components for several layers.  When the model data was updated, we ended-up passing it through the several layers until it reached the appropriate component(s).  It created unwanted coupling between the GUI components because if we wanted a new GUI component class to accept model data, we had to create methods to update the model data in all of the GUI components that contained the new class.

To fix it, we maintained in the View class a map of references to all of the nested GUI components so that whenever the model data was updated, the View could send the updated model data directly to the GUI components that needed it, end of story.  This worked well because there were only single instances of each GUI component.  I could see it not working so well if there were multiple instances of some GUI components, making it difficult to identify which copy needed to be updated.


问:
How do I develop database-utilizing application in an agile/test-driven-development way?
I want to add databases (traditional client/server RDBMS's like Mysql/Postgresql as opposed to NoSQL, or embedded databases) to my toolbox as a developer.  I've been using SQLite for simpler projects with only 1 client, but now I want to do more complicated things (ie, db-backed web development).  I usually like following agile and/or test-driven-development principles.  I generally code in Perl or Python.  Questions:

How do I test my code such that each run of the test suite starts with a 'pristine' state?  Do I run a separate instance of the database server every test?  Do I use a temporary database?

How do I design my tables/schema so that it is flexible with respect to changing requirements?

Do I start with an ORM for my language?  Or do I stick to manually coding SQL?  One thing I don't find appealing is having to change more than one thing (say, the CREATE TABLE statement and associated crud statements) for one change, b/c that's error prone.  On the other hand, I expect ORM's to be a low slower and harder to debug than raw SQL.

What is the general strategy for migrating data between one version of the program and a newer one?  Do I carefully write ALTER TABLE statements between each version, or do I dump the data and import fresh in the new version?

答:
The features and questions that have been asked, can be found in a good ORM tooling.

As an example, in Entity Framework ORM we are able to have an approach of modeling your database schema in your code through Models. And incrementally migrate your changes to the actual database of your application. - This approach is code first approach in EF starting from 4.1 version.

Although previously EF supported only MS SQL server, nowadays it supports all other major databases as well. Thus, i would look at ORM frameworks that meets your needs.

As an alternative you may also look at NHibernate : Code First approach with fluent NHibernate

ContentLicense: CC BY-SA 4.0


问:
Why should one use a single-use temporary variable?
Let's say we have a class called 'Automobile' and we have an instance of that class called 'myCar'. I would like to ask why do we need to put the values that our methods return in a variable? Why don't we just call the method?

For example, why should one write:

```<code>string message = myCar.SpeedMessage();
Console.WriteLine(message);
</code>```

instead of:

```<code>Console.WriteLine(myCar.SpeedMessage());
</code>```

答:
There are some times when an extra variable can help general readability, especially when evaluating some long expression inside an if or a function call that makes the code go way off the right-hand side of the screen.

In Java 5 or later (and many similar languages), an extra variable can be useful to find the cause of null-pointer exceptions.  Consider the following Java code which relies on Java's "autoboxing" to convert Integer objects to an int primitive:

```<code>public int add(Integer firstIntObj, Integer secondIntObj) {
    return firstIntObj + secondIntObj;
}
</code>```

It will throw a NullPointerException when it converts an uninitialized (null) Integer object into a primitive int, but this exception doesn't indicate which object caused the exception because the two possible culprits are on the same line.  The following would blow up on a different line for each null input, thus the line number in the exception will indicate which one was null:

```<code>public int add(Integer firstIntObj, Integer secondIntObj) {
    int first = firstIntObj;
    int second = secondIntObj;
    return first + second;
}
</code>```

A better way to handle this is to throw your own descriptive exception so that someone calling this method can diagnose their own problem without needing your source code:

```<code>public int add(Integer firstIntObj, Integer secondIntObj) {
    if (firstIntObj == null) {
        throw new IllegalArgumentException("First argument was null!");
    }
    if (secondIntObj == null) {
        throw new IllegalArgumentException("Second argument was null!");
    }
    return firstIntObj + secondIntObj;
}
</code>```

This technique can be used to split up any line that could throw more than one exception.  Using separate variables is less typing than throwing a descriptive exception - it's quick and dirty.

Body:
There are some times when an extra variable can help general readability, especially when evaluating some long expression inside an if or a function call that makes the code go way off the right-hand side of the screen.

In Java 5 or later (and many similar languages), an extra variable can be useful to find the cause of null-pointer exceptions.  Consider the following Java code which relies on Java's "autoboxing" to convert Integer objects to an int primitive:

public int add(Integer firstIntObj, Integer secondIntObj) {
    return firstIntObj + secondIntObj;
}

It will throw a NullPointerException when it converts an uninitialized (null) Integer object into a primitive int, but this exception doesn't indicate which object caused the exception because the two possible culprits are on the same line.  The following would blow up on a different line for each null input, thus the line number in the exception will indicate which one was null:

public int add(Integer firstIntObj, Integer secondIntObj) {
    int first = firstIntObj;
    int second = secondIntObj;
    return first + second;
}

A better way to handle this is to throw your own descriptive exception so that someone calling this method can diagnose their own problem without needing your source code:

public int add(Integer firstIntObj, Integer secondIntObj) {
    if (firstIntObj == null) {
        throw new IllegalArgumentException("First argument was null!");
    }
    if (secondIntObj == null) {
        throw new IllegalArgumentException("Second argument was null!");
    }
    return firstIntObj + secondIntObj;
}

This technique can be used to split up any line that could throw more than one exception.  Using separate variables is less typing than throwing a descriptive exception - it's quick and dirty.


答:
Some answers:

You should have an initialization script that rebuilds the database in the "pristine" state for the integration tests.  This will also be useful when you go-live to make sure the initial database is initialized properly. For unit testing you may want to mock the data with in-memory mocks of the database for faster test runs and reduced dependency. There are different strategies for mocking depending on the ORM or data access layer that is chosen.

Try to model your DB schema to as closely resemble your application domain model as much as possible when you design the entities, attributes, and relationships.  Some ORM's can help you do this automatically by generating the database from the application's classes.

ORM's can be useful to reduce boiler plate code and can help address the other questions raised here. They are usually slower than raw SQL methods but the reduced development time can offset these issues.

Write (or generate if the ORM can do it) the SQL statements to update to the next version and also statements to roll back to the previous version after the update in case it is required. Also make sure these (and any other database code) are committed to your source control repository like any other application code.

Title: How do I develop database-utilizing application in an agile/test-driven-development way?

Body:
I want to add databases (traditional client/server RDBMS's like Mysql/Postgresql as opposed to NoSQL, or embedded databases) to my toolbox as a developer.  I've been using SQLite for simpler projects with only 1 client, but now I want to do more complicated things (ie, db-backed web development).  I usually like following agile and/or test-driven-development principles.  I generally code in Perl or Python.  Questions:

How do I test my code such that each run of the test suite starts with a 'pristine' state?  Do I run a separate instance of the database server every test?  Do I use a temporary database?

How do I design my tables/schema so that it is flexible with respect to changing requirements?

Do I start with an ORM for my language?  Or do I stick to manually coding SQL?  One thing I don't find appealing is having to change more than one thing (say, the CREATE TABLE statement and associated crud statements) for one change, b/c that's error prone.  On the other hand, I expect ORM's to be a low slower and harder to debug than raw SQL.

What is the general strategy for migrating data between one version of the program and a newer one?  Do I carefully write ALTER TABLE statements between each version, or do I dump the data and import fresh in the new version?


答: Take a look at the Rabin-Karp algborithm. It uses a rolling hash somewhat like rsync uses to minimize bytes transmitted during a sync. By adjusting the size of the window you use for the hash you can make it more or less sensitive. R-K is used for, amongst other things, plagiarism detection, which is basically looking for sort-of dupes. </p>


答:
You Already Have Good Ideas

The ideas you outline in your question sound excellent.  It is a big surprise that you are not finding success.  It is 2012 and the object-oriented revolution has long since passed from state-of-the-art to state-of-the-practice.  It seems like unless you have very low turn over and very little hiring, you would have a hard time not getting several dozen or even a hundred good solid object oriented programmers.

Agile or Object Oriented?

You mention some Agile technologies like TDD and some newer concepts, so don't be too harsh on people for not embracing something that is still actively fought by some management teams.  Some claim to embrace Agile, but when they talk about it, it means what they say it means.  The organization is not characterized by teams that make decisions and adapt, but instead by strong hierarchical contract-style control.

But back to object oriented.  You don't mention object oriented analysis or design, and I am not quite sure which programming language is giving way to which object oriented programming language.  I know UML is having popularity problems among many object-oriented programmers.  Having been thoroughly trained in OOAD, I believe that it may be like learning the culture and history of a country whose natural language you want to learn.  For example, if I wanted to learn Greek, I could learn the alphabet, vocabulary, and grammar, but if I ignored the rich history and culture, I would miss a lot.  In any case, if you learn all about an object oriented programming language, but nothing about OOAD, I think an important opportunity has been lost.

Problems to Overcome?

Bridge too far?  If you ask people to learn one small thing a week, in a year, among the people who participate, there will be a lot of change.  If you ask them to change everything they know, it will be welcomed by a few, hard for many, and impossible for others.  Some changes like source control are localized.  You transitions from not doing it before, you had training that was not stressing the limits of memory, someone walked you through it the first time, and then the day-to-day was pretty easy.

Other changes are pervasive.  For example, dumping C and switching to Java requires significant training, setup, and big changes in the day-to-day to adopt a new IDE, new compiler, new language, new API, new deployment model, etc.  This is the kind of thing that happens most often in conjunction with a pilot program or corporate restructuring.

Leading a Revolution?  If the people currently doing the work have a history of being rewarded, and the company does not seem in danger of failing, what is their motivation for change?  If you seem like an outsider who wants to point the direction and leave them accountable for results they can't predict, it may seem like all risk, no reward.

Position Power or Idea Leadership?  Many organizations operate based on position power.  If you lack visible support from managers, sections heads, directors, and Vice Presidents, you are merely an idea leader.  Some people are in the dangerous position of having one idea, and not being able to entertain a second one.  If you can show them instead of telling them, that will go a long way to quiet skeptics and to interest talented allies.

Base of Support Too Small?  Do a triage among those 250 people and sort them into three categories: ready to embrace, willing to learn, and unwilling to learn.  You have good reasons to be frustrated with some of the people who have no interest in making a change.  You might as well be pushing on a rope.  This is wasted effort.  If you have a feel for who supports change, you can find out what interests them.

Unlike a medical triage where the ethical and practical choice is to help the middle group that can make it with help, you can invest your energy and time based on your judgment and preference.  For your success, why not cultivate the group that is ready to embrace new ideas?  They may be few a first, but like a snowball, your visibility and credibility as an advocate will grow.  Soon people will be asking you when the next training will be.

In it for the Long Term?  Until you cultivate a champion to carry things after you, you should expect to invest time building relationships.  You may need to stay with the teams you coach for more than one month.  Until the team owns improved practices for themselves, you are just a technology or methodology cop.  Mentoring is a process that can take years.  There are a lot of things your developers don't want to do that you think are important (you specifically mentioned unit testing I think).  It may take a while to build a shared vision of the value this brings.  I know this by experience because I once advocated for a code coverage tool at a Fortune 500 company that had a great reputation for quality, but managers and peers alike were wary about committing to it.

Expert or Grassroots?  Much faster than mentoring would be to foster grassroots support that comes from each team member.  Starting with a team of ten software specialists, if I had my choice to have one person work on process all the time or ten people work on process ten percent of the time, I would pick the second.  Grassroots process permits the advocates to feel the impact of the approach, and for the approach to be tailored to best solve the problems of the team who owns the work.

Do you see the Freedom Line?  Part of introducing "Best Practices" is to get people to give up some freedom to do things in a common way.  Giving up programmer's discretion will be more palatable if you look for opportunities to leave many choices to developers.  What they pick is delineated from what is mandated by a partition that we can call the freedom line.  It may be necessary to similar, well justified divisions about organizational, regional/site specific, team, and personal practices.


问:
How do people maintain their test suite?
In particular, I'm curious about the following aspects:

How do you know that your test cases are wrong (or out-of-date) and needed to be repaired (or discarded)? I mean, even if a test case became invalid, it might still pass and remain silent, which could let you falsely believe that your software works okay. So how do you realize such problems of your test suite?

How do you know that your test suite is no longer sufficient and that new test cases should be added? I guess this has something to do with the requirement changes, but is there any systematic approach to check the adequacy of test suite?

答:
Short answer: use known tools that help maintain the quality of test cases such as the following code coverage and code quality tools: Cobertura, PMD, Sonar, etc. that will help you notice when a critical component of the program is not tested enough. Also, write integration tests, that are most likely to break first whenever something goes wrong.

Long answer:

How do you know that your test cases are wrong (or out-of-date) and
  needed to be repaired (or discarded)? I mean, even if a test case
  became invalid, it might still pass and remain silent, which could let
  you falsely believe that your software works okay. So how do you
  realize such problems of your test suite?

Using code coverage tools such as Cobertura, you are able to detect that test cases for a given class, or complex methods, are no longer sufficient. You don't need to reach 100% code coverage everywhere and in most cases it will be hard to achieve and not necessarily useful ; but tests for the most critical aspects of a program should be maintained with a goal of at least 80% of code coverage.

Using continuous build and integration tools, such as Jenkins which I'm very fond of, in combination with the Sonar plugin, you are able to set triggers that send emails and other types of alerts to persons responsible for the latest changes. Various graphs and statistics (Sonar also uses Cobertura among many other tools) help code reviewers and test case developers to focus on what's critical.

How do you know that your test suite is no longer sufficient and that
  new test cases should be added? I guess this has something to do with
  the requirement changes, but is there any systematic approach to check
  the adequacy of test suite?

What I've wrote for the first question is part of the answer for your second question. I'll also add the following points here:

Write integration test cases (or "business" cases if you prefer) in addition to test cases. These are most likely to change/break first because they often depend on multiple classes/methods. And since they break often, it's less likely that will be forgotten. The only approach/methodology that, from my personal experience, helps writing good tests is Test-Driven Development. Particularly if the person writing the test case is NOT the same person writing the code for it. Writing good test cases using TDD also takes time, but the results, at least for me, were extremely satisfactory.

Whenever a bug comes out, write the fix and the test case that comes with it. The test case should only cover this particular bug. Since you've completely covered the code that responsible for the bug, it should not come out again.


答:
There really isn't any way to make sure your test cases are correct, except by concentrating really well when creating them - understanding the requirement, understanding the code, and making sure that they agree. The point of having a test suite is that you only have to do this once, and from then on you can just re-execute the tests and check that they pass, whereas without a test suite you would have to concentrate really hard all the time, i.e. whenever you do anything to your code base. But the fundamental problem of having to make sure that you were doing the right thing in the first place remains - computers simply are not intelligent enough to relieve us of that task.

Therefore, (1) if your test suite is incomplete, there is no simple way of seeing that. Code coverage analysis can prove that some lines of code are never executed, i.e. that the suite is deficient in some way, but not how severe that deficiency is, and it can never prove that it is sufficient. Even with 100% code coverage you have no guarantee that all relevant states of the system are exercised, and complete state coverage is inachievable for any realistic system because of the combinatorial number of states that could exist. One good technique of making sure that you test case is at least correct for checking the thing you want checked is to write the test, verify that it does indeed fail, write/change the code, and then verify that it now passes. Hence the enthusiasm for test-driven development: it allows you to be quite sure that an individual test does the right thing, and if you create your entire code base that way, you can get a similar level of confidence even in a large system.

(2) A test suite normally becomes insufficient whenever requirements change - you don't have to guess. If the customer wants some particular behaviour changed, and your tests would succeed both before and after the change, then clearly they weren't exercising that particular input/output relationship.

As for legacy systems that don't have test coverage, or where you don't know what the coverage is, there is no formal proof, but (parental advisory: personal opinion follows!) speaking from experience it is overwhelmingly likely that the tests are not adequate. When testing is viewed as an after-the-fact, optional, quality-enhancing-but-not-really-necessary activity, it tends to be incomplete and unsystematic because the incentive for making sure the tests keep up with the code base just isn't there.

Title: How do people maintain their test suite?

Body:
In particular, I'm curious about the following aspects:

How do you know that your test cases are wrong (or out-of-date) and needed to be repaired (or discarded)? I mean, even if a test case became invalid, it might still pass and remain silent, which could let you falsely believe that your software works okay. So how do you realize such problems of your test suite?

How do you know that your test suite is no longer sufficient and that new test cases should be added? I guess this has something to do with the requirement changes, but is there any systematic approach to check the adequacy of test suite?


问:
To refund or not to refund this client?
I'd really appreciate your advice on an ongoing project.

I presented my client with a proposal and design samples which he approved, and he paid in full instead of the 50% upfront deposit as I'd given him a generous discount. He was then slow in furnishing me with some of the content, but once we did, he expected the website to be finished immediately which was not possible. Because he needed it done urgently, we agreed to try to get it done about 10 working days after the content was provided, but the developer who was helping me let me down.

The next week, I completed the website myself and uploaded it to the server on a Friday afternoon. He then calls and texts me on following Sunday while I'm at church to say it's not online (there was probably a problem with his browser). The next morning, I received an email from him demanding a full refund within two days because he couldn't see the website (even though it was live, and I tested it on multiple browsers, a different computer and my phone), and he called me shouting at me because he couldn't access it. Finally when he was able to access it, he was unhappy with a certain detail regarding the slideshow which I began fixing and which was done the next day. He then referred me to another website and said he wanted it to look similar but not identical to it in terms of the layout. He also now wanted to add more features which were not in the original design.

I got a designer to work on a new design which I sent to him for review, which if approved would be completed by 15 October, and he approved it last Thursday. He then called me yesterday to say that he wanted to change the design - he only approved it out of impatience. He now wants the website to be more similar to the other website he referred me to and he wants it done before the 15th! Then, he says to me that other people have done websites for him in three days - website's he's complained to me about for lacking dimension because they were just premium themes, whereas we'd designed and coded from scratch.

I'm thinking of finishing the website but refunding him in full (or at least the refundable 50%) less domain registration and other non-refundable amounts, just to avoid further escalation of this matter and having him call me next week and say he wants to change it again.

These are the applicable terms and conditions as laid out in the agreement:

Total amount due for this project is Amount A.

Client shall pay Consultant a deposit of Amount B (50% of total amount due for project) in advance before any work commences on the
  Project.

The balance is due within 7 working days of completion of project.

Deposit is non-refundable.

Should client opt to host elsewhere, applicable transferral fee of Amount C will apply.

Estimated project completion time frame is 14 to 30 days from the date Client furnishes Consultant with Brief and all other required
  media and data, provided that Client has made payment to secure the
  project.

Consultant will make every effort to meet agreed upon due dates. The Client should be aware that failure to submit required information
  or materials, or last minute changes and excessive changes may cause
  subsequent delays. Client delays could result in significant delays in
  delivery of finished work.

Major changes in client input or direction or brief will be charged at normal rates.

Any work the Client wishes Consultant to create, which is not specified in the attached Proposal will be considered an additional
  service.

Client agrees to pay Consultant for any additional expenses or additional services not included in the attached quotation and
  proposal if requested by the Client.

Web design credit in the name of the Consultant, and link to Consultant’s website shall be placed on the footer of the final
  Website.

Either party may terminate this Agreement by giving 7 days written notice to the other of such termination.

In the event that Work is postponed or terminated at the request of the Client, Consultant shall have the right to bill pro rata at
  full rates for work completed through the date of that request, while
  reserving all rights under this Agreement.

If additional payment is due, this shall be payable within seven days of the Client's written notification to stop work. In the event
  of termination, the Client shall also pay any expenses incurred by
  Consultant and the Consultant shall own all rights to the Work.

Advice please?

答:
Do you have a written (e-mails etc) record of his prior approval of previous designs?  If so and he keeps changing the agreed spec (and you do not mention that he is paying for these changes, section 8) and is being abusive and unreasonable, I would invoke section 12 of the contract and terminate it entirely, taking your work with you.

If you want as a gesture, refund him the money and supply him with the domain registration details etc.

Also take this as a learning experience about having reliable developers and taking full payment upfront.

Title: To refund or not to refund this client?


问:
Hard-copy approaches to time tracking
I have a problem: I suck at tracking time-on-task for specific feature/defects/etc while coding them.  I tend to jump between tasks a fair bit (partly due to the inherit juggling required by professional software development, partly due to my personal tendancy to focus on the code itself and not the business process around code).

My personal preference is for a hard-copy system.  Even with gabillions of pixels of real-estate on-screen I find it terribly distracting to keep a tracking window convienient; either I forget about it or it gets in my ways.

So, looking for suggestions on time-tracking.  My only requirement is a simple system to track start/stop times per task.  I've considered going as far as buying a time-clock and giving each ticket a dedicated time-card.  When I start working on it, punch-in; when done working, punch-out.

答: It was mentioned in a comment by @Alex Feinman, but I think it deserves its own answer - I just discovered Workrave for keeping time and find it to be really useful.  It also fits in very nicely with the Pomodoro technique that was mentioned in another answer here.</p>

Title: Hard-copy approaches to time tracking


答: This is really, really a case for a lawyer. Should you refund your client? Not until your attorney told you so. By refunding your client, you might inadvertently admit that your work was not adequate, so your client might use that to claim damages for his work, for the lost business opportunity etc.</p>

Title: To refund or not to refund this client?

Body:
I'd really appreciate your advice on an ongoing project.

I presented my client with a proposal and design samples which he approved, and he paid in full instead of the 50% upfront deposit as I'd given him a generous discount. He was then slow in furnishing me with some of the content, but once we did, he expected the website to be finished immediately which was not possible. Because he needed it done urgently, we agreed to try to get it done about 10 working days after the content was provided, but the developer who was helping me let me down.

The next week, I completed the website myself and uploaded it to the server on a Friday afternoon. He then calls and texts me on following Sunday while I'm at church to say it's not online (there was probably a problem with his browser). The next morning, I received an email from him demanding a full refund within two days because he couldn't see the website (even though it was live, and I tested it on multiple browsers, a different computer and my phone), and he called me shouting at me because he couldn't access it. Finally when he was able to access it, he was unhappy with a certain detail regarding the slideshow which I began fixing and which was done the next day. He then referred me to another website and said he wanted it to look similar but not identical to it in terms of the layout. He also now wanted to add more features which were not in the original design.

I got a designer to work on a new design which I sent to him for review, which if approved would be completed by 15 October, and he approved it last Thursday. He then called me yesterday to say that he wanted to change the design - he only approved it out of impatience. He now wants the website to be more similar to the other website he referred me to and he wants it done before the 15th! Then, he says to me that other people have done websites for him in three days - website's he's complained to me about for lacking dimension because they were just premium themes, whereas we'd designed and coded from scratch.

I'm thinking of finishing the website but refunding him in full (or at least the refundable 50%) less domain registration and other non-refundable amounts, just to avoid further escalation of this matter and having him call me next week and say he wants to change it again.

These are the applicable terms and conditions as laid out in the agreement:

Total amount due for this project is Amount A.

Client shall pay Consultant a deposit of Amount B (50% of total amount due for project) in advance before any work commences on the
  Project.

The balance is due within 7 working days of completion of project.

Deposit is non-refundable.

Should client opt to host elsewhere, applicable transferral fee of Amount C will apply.

Estimated project completion time frame is 14 to 30 days from the date Client furnishes Consultant with Brief and all other required
  media and data, provided that Client has made payment to secure the
  project.

Consultant will make every effort to meet agreed upon due dates. The Client should be aware that failure to submit required information
  or materials, or last minute changes and excessive changes may cause
  subsequent delays. Client delays could result in significant delays in
  delivery of finished work.

Major changes in client input or direction or brief will be charged at normal rates.

Any work the Client wishes Consultant to create, which is not specified in the attached Proposal will be considered an additional
  service.

Client agrees to pay Consultant for any additional expenses or additional services not included in the attached quotation and
  proposal if requested by the Client.

Web design credit in the name of the Consultant, and link to Consultant’s website shall be placed on the footer of the final
  Website.

Either party may terminate this Agreement by giving 7 days written notice to the other of such termination.

In the event that Work is postponed or terminated at the request of the Client, Consultant shall have the right to bill pro rata at
  full rates for work completed through the date of that request, while
  reserving all rights under this Agreement.

If additional payment is due, this shall be payable within seven days of the Client's written notification to stop work. In the event
  of termination, the Client shall also pay any expenses incurred by
  Consultant and the Consultant shall own all rights to the Work.

Advice please?


答: The problem here is him constantly changing his requirements. You can easily tell him in a nice manner that I can do this for you but it will cost you an x amount  more. That will surely make him stop continuously changing his requirements and understand the development process better. You see if you don't do this people will take your work for-granted and try to misuse your service and you will eventually burn yourself out of your work. So keep it straight. There is no need for a refund as you have already worked for the amount payed and produced something which justifies the amount paid. I ran my own web development company for 2 years and so am aware of such clients.</p>

Title: To refund or not to refund this client?


问:
Proper attribution of derived work in a GPL project
This is a continuation of me rewriting GPL project.

What will be the correct way of attributing my project as being a derivative of some other GPL-licensed project? So far I came up with:

```<code>HgSharp

Original Copyright Matt Mackall <mpm@selenic.com> and contributors.

The following code is a derivative work of the code from the Mercurial project,
which is licensed GPLv2. This code therefore is also licensed under the terms
of the GNU Public License, verison 2.

For information on the license of this code when distributed with and used
in conjunction with the other modules in the HgSharp project, please see
the root-level COPYING file.

Copyright 2011-2012 Anton Gogolev <anton.gogolev@hglabhq.com>
</code>```

答: Looks good to me. Perhaps you should also link to the homepage and source code of the original project.</p>


答: As long as you have used a strict policy on only recognizing specs which have been signed off on then I think you're legally fine. However it may be worth invoking section 12 as the job may be more trouble than it's worth. Personally I'd have built it to the original spec and then treat any modifications as separate independent jobs.</p>

Title: To refund or not to refund this client?


问:
Design patterns to manage multiple windows/screens?
Supposed there is an application with multiple windows, like:

Logo splashscreen
Connecting... screen
Login-usercontrols screen
Logging in... screen
etc.

Is there a design pattern, which allows to effectively manage all those windows? (to hide previous windows, display a new window etc.)

答:
Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.

As an example, consider the implementation of a dialog box to present a collection of widgets such as buttons, menus, and entry fields. There may be dependencies between the widgets in the dialog, e.g., selecting an entry in a list box might change the contents of an entry field.

Such dependencies can be reduced by encapsulating collective behavior in a mediator object. A mediator is responsible for controlling and coordinating the interactions of these objects. The mediator acts as an intermediary and keeps objects in the group from referring to each other explicitly. The objects only know the mediator, thus reducing the number of interconnections.


问:
Does it make a difference if I declare variables inside or outside a loop in Java?
Does it make a difference if I declare variables inside or outside a loop in Java?

Is this

```<code>for(int i = 0; i < 1000; i++) {
   int temp = doSomething();
   someMethod(temp);
}
</code>```

equal to this (with respect to memory usage)?

```<code>int temp = 0;
for(int i = 0; i < 1000; i++) {
   temp = doSomething();
   someMethod(temp);
}
</code>```

And what if the temporary variable is for example an ArrayList?

```<code>for(int i = 0; i < 1000; i++) {
   ArrayList<Integer> array = new ArrayList<Integer>();
   fillArray(array);
   // do something with the array
}
</code>```

EDIT: with javap -c I got the following output

Variable outside the loop:

```<code>  public static void main(java.lang.String[]);
    Code:
       0: iconst_0
       1: istore_1
       2: iconst_0
       3: istore_2
       4: iload_2
       5: sipush        1000
       8: if_icmpge     25
      11: invokestatic  #2                  // Method doSomething:()I
      14: istore_1
      15: iload_1
      16: invokestatic  #3                  // Method someMethod:(I)V
      19: iinc          2, 1
      22: goto          4
      25: return
</code>```

Variable inside the loop:

```<code>  public static void main(java.lang.String[]);
    Code:
       0: iconst_0
       1: istore_1
       2: iload_1
       3: sipush        1000
       6: if_icmpge     23
       9: invokestatic  #2                  // Method doSomething:()I
      12: istore_2
      13: iload_2
      14: invokestatic  #3                  // Method someMethod:(I)V
      17: iinc          1, 1
      20: goto          2
      23: return
</code>```

And for the interested, this code:

```<code>public class Test3 {
    public static void main(String[] args) {
        for(int i = 0; i< 1000; i++) {
            someMethod(doSomething());
        }
    }
    private static int doSomething() {
        return 1;
    }
    private static void someMethod(int temp) {
        temp++;
    }
}
</code>```

produces this:

```<code>  public static void main(java.lang.String[]);
    Code:
       0: iconst_0
       1: istore_1
       2: iload_1
       3: sipush        1000
       6: if_icmpge     21
       9: invokestatic  #2                  // Method doSomething:()I
      12: invokestatic  #3                  // Method someMethod:(I)V
      15: iinc          1, 1
      18: goto          2
      21: return
</code>```

But the optimization happens at runtime then. Is there a way to look at the optimized code? (Sorry for the long EDIT)

答:
Short answer : no.
There were similar questions somewhere on this site already. There is no notable difference as per the generated bytecode. Declaring them when needed produces less lines of code.</p>


答:
At the level of the individual variable there is no significant difference in effeciency, but if you had a function with 1000 loops and 1000 variables (never mind the bad style implied) there could be systemic differences because all the lives of all the variables would be the same instead of overlapped.  This could affect things like stack size and the garbage collector's ability to clean up the variables that were being kept alive longer than necessary.

Also, it's much better style to give variables the smallest possible scope.  It prevents accidents.

Body:
Does it make a difference if I declare variables inside or outside a loop in Java?

Is this

```<code>for(int i = 0; i < 1000; i++) {
   int temp = doSomething();
   someMethod(temp);
}
</code>```

equal to this (with respect to memory usage)?

```<code>int temp = 0;
for(int i = 0; i < 1000; i++) {
   temp = doSomething();
   someMethod(temp);
}
</code>```

And what if the temporary variable is for example an ArrayList?

```<code>for(int i = 0; i < 1000; i++) {
   ArrayList<Integer> array = new ArrayList<Integer>();
   fillArray(array);
   // do something with the array
}
</code>```

EDIT: with javap -c I got the following output

Variable outside the loop:

```<code>  public static void main(java.lang.String[]);
    Code:
       0: iconst_0
       1: istore_1
       2: iconst_0
       3: istore_2
       4: iload_2
       5: sipush        1000
       8: if_icmpge     25
      11: invokestatic  #2                  // Method doSomething:()I
      14: istore_1
      15: iload_1
      16: invokestatic  #3                  // Method someMethod:(I)V
      19: iinc          2, 1
      22: goto          4
      25: return
</code>```

Variable inside the loop:

```<code>  public static void main(java.lang.String[]);
    Code:
       0: iconst_0
       1: istore_1
       2: iload_1
       3: sipush        1000
       6: if_icmpge     23
       9: invokestatic  #2                  // Method doSomething:()I
      12: istore_2
      13: iload_2
      14: invokestatic  #3                  // Method someMethod:(I)V
      17: iinc          1, 1
      20: goto          2
      23: return
</code>```

And for the interested, this code:

```<code>public class Test3 {
    public static void main(String[] args) {
        for(int i = 0; i< 1000; i++) {
            someMethod(doSomething());
        }
    }
    private static int doSomething() {
        return 1;
    }
    private static void someMethod(int temp) {
        temp++;
    }
}
</code>```

produces this:

```<code>  public static void main(java.lang.String[]);
    Code:
       0: iconst_0
       1: istore_1
       2: iload_1
       3: sipush        1000
       6: if_icmpge     21
       9: invokestatic  #2                  // Method doSomething:()I
      12: invokestatic  #3                  // Method someMethod:(I)V
      15: iinc          1, 1
      18: goto          2
      21: return
</code>```

But the optimization happens at runtime then. Is there a way to look at the optimized code? (Sorry for the long EDIT)


答:
The common answer to most of these questions should be "why don't you try it and find out?".  In Java you could probably take a look at the generated bytecode (I believe the tool is called javap), to see what the difference in byte code is between those two ways of declaring the variable.

Doing it like that is a better learning experience for you, because next time you're running into an optimization issue you can use the same tool to verify that the compiler is doing what you are expecting - it will help you avoid needlessly changing  your coding style when the optimizer does fine on its own, or finding actual tweaks when you really need that last bit of performance.

Title: Does it make a difference if I declare variables inside or outside a loop in Java?


问:
How Facebook's Ad Bid System Works
When you are creating an ad on Facebook, you are provided with a "suggested bid" range (e.g., $0.90 - $2.15 USD). According to this page:

The suggested bid range is there to help you pick a maximum bid so your ad will be successful. It’s based on how many other advertisers are competing to show their ad to the same audience as you are.

I'm interested in understanding what's actually going on (technically) under the hood here.

Say a user logs into Facebook. On the server-side, it the HTTP request that the user's browser sent (as part of the login) is handled, and the server needs to figure out which ad to display back to the user.

I assume this is where the "bidding" system comes into play? Say that, based on this user's demographics, and based on the audience targeting that several competing advertisers designed their campaign with, let's pretend that Facebook sees a pool of 20 different ads it could return.

How does this bidding system help Facebook determine which of the 20 ads it returns to the client-side? I'm guessing that advertisers who "bid more" get prioritized over those who "bid less". But when does this bidding take place? How often does an advertiser need to re-bid? How long is a bid binding for?

Once I understand these usage-related concepts behind ads, it will probably be obvious between which of the following "selection strategies" the backend is using:

Round robin

Prioritized round robin

Randomized (doubtful)

History-based

MVP-based

Thanks to anyone who can help point me in the right direction and explain what these suggested bid systems are and how they work.

答:
I agree with ChrisF.  Your not going to get a definitive answer short of having someone from facebook answer it.  If I was them them, I would base the bid off these factors:

Vertical

What are industry averages for this category. You could use a tool like Google's Keyword

Tool to find out

Popularity

What ratio of relavent content, to advertisers is there?  If the niche is extremely popular and rich with content and advertisers, I would put the minimum bid a little higher than the lowest advertisers bid.

CTR

This is a big one with Google. If you have an awesome click through rate, you can be in positions 1,2 or 3 with a lower price per click than the positions below you. I'm sure Facebook takes this into consideration.

Daily budget

Someone who has a smaller daily budget would have to pay more per click than someone with a larger budget

These where just a couple ideas to consider.  I'm sure there are many more factors to consider.

Title: How Facebook's Ad Bid System Works


答:
When designing the classification for our bugtracker, I tried to be very conscious of the purpose of each field. Here's what we ended up with.

When are we going to work on it?

This field records the results of our planning. It's very useful to search and filter on: when a developer is out of tasks (everything assigned to him is done, or his work is blocked for any reason) he can just grab an issue from the list of all unassigned bugs sorted by "When":

We frequently move issues between "Now" and "Next", and review the "Soon" tasks from time to time and after a release. Occasionally we go through the "Later" tasks. "If ever" is the field we use instead of closing requests we aren't sure we're ever going to work on.

How long is this going to take to fix?

This field is our best estimate of how long the work is likely to take. It is notoriously hard to have an idea of this, but due to the logarithmic scale and order-of-magnitude roughness we are rarely off by more than one entry in this list:

This field is used in several circumstances. First and foremost, it is a major consideration as to what goes into the "When" field discussed above. I like using this field to pick a very short task to start with on a Monday if I'm not feeling too motivated. It's also useful to find work for a new employee or someone else who's not feeling motivated.

How important is this task, in the grand scheme of things?

This field is for whoever does the project planning; developers don't really care. It helps record how much we think this work matters to our project's success:

It's used along with "How long will this take" to decide when we're going to work on this task.

What's the bug's state?

This field is used for workflow. Every state shown below is either an "open" or a "closed" state. The bugtracker has shortcuts for showing only open bugs, and that's the main use for this field.

The subdivision into open/closed is the main purpose of this field, however each individual "closed" entry acts to summarise the outcome. This can often be inferred from comments too, but it helps to have a standard place where this information can be found.

There is a bugtracker-enforced rule that a Duplicate bug must have at least one link to say what the bug is a duplicate of.

We're quite explicit about not using an "in progress" state because from past experience, it's very hard to keep this properly up-to-date, and as a result the state can do more harm than good.

What is this bug about?

This is the closest to the conventional "bug type" field. Its main purpose is to describe the entry to a human. We almost never search or filter on this field. It's only there to save words in the bug title. The list varies somewhat from project to project, but here's a representative example:

Empty fields

Picking a value for all of the above is not always easy, so we allow the person entering the bug to leave empty any fields they can't figure out right now. We maintain the attitude that this is perfectly fine, even if you're just feeling lazy, because otherwise we might end up with garbage in these.

The fields "When" and "Importance" are often left blank because these things are for the project planner to decide. Entries with blank "Much work" get reviewed from time to time, with potential assignees asked to estimate it.

Project-specific

While large projects use all of the above, certain small projects do away with fewer. If they have no use for "Importance", we insist that the field is deleted from that project. The idea is that only fields that are being actively used should be present.

P.S. The above screenshots are of YouTrack, which I really rather like. It had no problem with us getting rid of all the built-in fields and using our own.

Title: Software bug/defect classification


答:
For an exact mathematical math I'd store a hash and then compare that.

I think the systems which are used for exams measure groups of words and then the frequency of groups of each size. For example a chain of 30 words which are copied would score 5 risk points and 5 occurrences of 10 word chains wold score 5 points. Then you'd have a thresh hold of 30 points per 500 words.

Really you need a semantic algorithm so that words like 'also' and 'and' are parsed as the same.


问:
Which aspect of normal forms do entity-attribute-value tables violate, if any?
I'm not asking if EAV tables are good or bad. I'm wondering if they are considered "normalized", and if not, why? If they aren't normalized, which normal form are they violating and why?</p>

答:
Asking whether it's normalized or not is largely beside the point.

Instead, you should ask "is it based on a relational model of the subject matter"?  The answer is a resounding NO.  This is by intent.  When new features of the subject matter are discovered, like new entities or attributes,  the EAV can be updated with no changes to data definitions.

That means that the EAV is independent of the information requirements on the subject matter.  That in turn means we can build an EAV without doing any data analysis of the subject matter.  That's both the great attraction and the terrible pitfall of EAV.


问:
What should and what shouldn't be in a header file?

What things should absolutely always/never be included in a header file?

Functions: what must go in the header file and what mustn't

Constants: Is it good practice to define lot of constants in a header file?

Any other good practices for C header files?

Example

I'm writing a parser for a documented industry standard format that has a lots of constants, is it a good practice to define them in a header file?

答:
In addition to what has already been said.

H files should always contain:

Source code documentation!!! At a minimum, what is the purpose of the various parameters and return values of the functions.

Header guards, #ifndef MYHEADER_H #define MYHEADER_H ... #endif

H files should never contain:

Any form of data allocation.

Function definitions. Inline functions may be a rare exception in some cases.

Anything labelled static.

Typedefs, #defines or constants that have no relevance to the rest of the application.

(I would also say that there is never any reason to use non-constant global/extern variables, anywhere, but that's a discussion for another post.)


问:
.NET Properties - Use Private Set or ReadOnly Property?
In what situation should I use a Private Set on a property versus making it a ReadOnly property?  Take into consideration the two very simplistic examples below.

First example:

```<code>Public Class Person

    Private _name As String

    Public Property Name As String
        Get
            Return _name
        End Get
        Private Set(ByVal value As String)
            _name = value
        End Set
    End Property

    Public Sub WorkOnName()

        Dim txtInfo As TextInfo = _
            Threading.Thread.CurrentThread.CurrentCulture.TextInfo

        Me.Name = txtInfo.ToTitleCase(Me.Name)

    End Sub

End Class

// ----------

public class Person
{
    private string _name;
    public string Name
    {
        get { return _name; }
        private set { _name = value; }
    }

    public void WorkOnName()
    {
        TextInfo txtInfo = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo;
        this.Name = txtInfo.ToTitleCase(this.Name);
    }
}
</code>```

Second example:

```<code>Public Class AnotherPerson

    Private _name As String

    Public ReadOnly Property Name As String
        Get
            Return _name
        End Get
    End Property

    Public Sub WorkOnName()

        Dim txtInfo As TextInfo = _
            Threading.Thread.CurrentThread.CurrentCulture.TextInfo

        _name = txtInfo.ToTitleCase(_name)

    End Sub

End Class

// ---------------

public class AnotherPerson
{
    private string _name;
    public string Name
    {
        get { return _name; }
    }

    public void WorkOnName()
    {
        TextInfo txtInfo = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo;
        _name = txtInfo.ToTitleCase(_name);
    }
}
</code>```

They both yield the same results.  Is this a situation where there's no right and wrong, and it's just a matter of preference?

答:
Whenever I've needed to change the access level of a setter, I've generally changed it to either Protected (only this class and derived classes can change the value) or Friend (only members of my assembly can change the value).

But using Private makes perfect sense when you want to do other tasks in the setter besides changing the backing value. As pointed out earlier, it's good design to not reference your backing values directly but instead only access them through their properties. That ensures that later changes you make to a property are applied internally as well as externally. And there's virtually no performance penalty to referencing a property vs its backing variable.


问:
How to calculate Sin function quicker and more precisely?
I want to calculate y(n)=32677Sin(45/1024•n), where y is an integer and n ranges from 0 to 2048. How can I make this process quicker and more precisely?
   Now I want to show you a reference answer:
   Since   Sin(a+b)=Sin(a)Cos(b)+Cos(a)Sin(b)
   And  Cos(a+b)=Cos(a)Cos(b)-Sin(a)Cos(b).
   So I can store  Sin(45/1024•1) and  Cos(45/1024•1) only.Then use this formula:

Sin(45/1024•2)=Sin(45/1024•1+45/1024•1),
    Cos(45/1024•2)=Cos(45/1024•1+45/1024•1),
  Sin(45/1024•n)=Sin(45/1024•(n-1)+45/1024•1),
    Cos(45/1024•n)=Cos(45/1024•(n-1)+45/1024•1) ,
This way maybe quicker without storing large array.

答: If n ranges from 0 to 2048, you can pre-calculate the values, store then in an array. y(n) would become values[n].