问:
Create a loop that visits every link on a site
I want to create an HTML scraper in Ruby on Rails and I want to implement a progress bar and an ability for it to pick up where it left off if it's interrupted while scraping.

I think the best way to do this be to create a database table with a record for every single page, with a boolean column called 'scraped?'

I'm thinking my scraper would have two main parts. The first part would loop through the entire site, following all links and saving their URLs in the table, along with a false for their scraped? attribute. It would also reset the database's pk sequence, meaning the id column would accurately imply which number the record is at (more about that later!)

ActiveRecord::Base.connection.reset_pk_sequence!('pages')

The second part would then move through this table, visiting each link and, once it's done scraping that page, it would change its scraped? attribute to true.

This second part would start by getting the first page that hasn't been scraped yet:

```<code>first_page = Page.where(:scraped? = false).first
</code>```

The progress bar would work on the following principle:

```<code>total_page = Page.count
done_pages = first_page.id # (the first result would always be 1 because the first part resets the sequence)
percentage = done_pages / total_pages * 100
</code>```

I actually don't see why this wouldn't work, but I'm still really vague about how the scraper would spider over and visit every single link on a website.

What would be your ideas on this? I just want some pointers before I dive in:

How would it know not to stray off the current site? (I'm thinking regex on the URL to make sure it contains the specified domain before it visits it)

How would it not visit infinite loops? Such as a link to the home page on the home page? (I'm guessing a conditional would do the trick here):

```<code>if new_uri != current_uri
    visit new_uri
end
</code>```

How would the first part actually go about its spidering, saving an index of every single link? By creating an array of every single link on the page and then iterating through it until it finds a uri that doesn't exist in the database? (this is actually my biggest concern.)

If the above is true, how would it know when to stop? I'm guessing it would check every new_link with existing links in the database? This is only a local program that's going to be executed once so I'm not too concerned about performance here, but do you have a better idea to the below:

```<code>if new_uri != current_uri # technically not necessary but it would save a few database interactions
    if Page.exists?( uri: new_uri) == false
        visit new_uri
    end
end
</code>```

答:
It's actually much harder than you've described:

Some (different) links may refer to the same page. Consider /forum/index.html?topic=3&page=2 and /forum/index.html?page=2&topic=3 as a trivial example (request parameters are swapped). So detecting if page is crawled already would require you to check page contents.

Page contents may change on every request. Consider "posted 1 minute ago" on comment post.

Some pages may be inaccessible for many reasons (so URLs must be marked accordingly):

link is broken

authorization required

page no longer exists

Some URLs may be "internal", but actually redirect to other sites: http://vk.com/away.php?to=http%3A%2F%2Fexample.com%2F

Some URLs may lead to files, not HTML pages

Summing it up, you must be able to:

set "states" on URLs: "checked"/"not checked", "malformed", "access denied", "outside redirect", etc. - you may actually use HTTP response statuses here

store MIME type of response to distinguish HTML pages from downloadable files

make diffs on pages' content, so you can tell if two URLs point to the same page. Simple textual diff probably won't work because HTML structure must be respected. Definition of "same page" may be complicated and depend on how much of content is different (e.g. pages are considered to be the same if >95% of plaintext is the same and HTML structure is the same)

It's probably even more complicated than I think, because I never made a crawler myself. First thing to do here is find some existing crawlers and either using one of them or examine them as examples of how things whould work. Check out gems (if it has to be Ruby) like Mechanize to start (just found it after 2 minutes googling, so google deeper).


问:
UX designers working one Sprint ahead
Our UX designers usually have a story in Sprint X that the developers will implement in Sprint X+1 (The UX designers and the devs/testers are on one team). I think this makes sense because if you don't have screen mockups and clear specifications you can't really estimate the work during Sprint Planning.

However in Scrum you are only supposed to have user stories that provide value to the user. In our case the UX design stories don't provide such value (they are more like a backlog grooming activity). Also usually Scrum coaches don't recommend having complete specifications before the start of the Sprint, a recommendation which I find hard to understand.

So do you see any disadvantages in our approach? It seems to work for us, but it goes somewhat against Scrum principles.

答:
The main disadvantages are these:

You are pipe-lining: if your designers are late, your developers are left without work; if your developers are late, your designer will eventually work more than one iteration in advance. It's not a stable situation - it is not sustainable.

Your designers are working in advance, you are paying costs for stories that may or may not be developed. Even if it happens rarely, you are still throwing away money.

Your UX designers are making decisions in advance without involving the developers. You are missing useful insights and increasing the risk that the designs are flatly wrong or unrealistic. This is quite common because UX design is not an "abstract" exercise - it must be crafted out of the characteristics of the application (including what is feasible/advisable to do or not technically)

Excluding or disempowering your developers is not a sustainable way to run a project.

Designers are not delivering value: this means that it's hard, if not impossible, to prioritize their work correctly. Usually, developer work is prioritized using different concerns, value, feasibility in the next sprint, amount of effort. A lot of time stories are negotiated and changed in order to make them "fit" or because of useful discussion: if any of this changes the UI (and you can assume it does if it's not a mere implementation detail), what do you do with the story? You can't play it anymore.

You are assuming that a story that can fit in one iteration for the designers fits in one iteration for the developers: how can you split the stories so this assumption is correct?

ContentLicense: CC BY-SA 3.0

Title: UX designers working one Sprint ahead


问:
Encrypt and decrypt password for a specific application
I have a basic web application where users can login and edit their profile. In the profile they can submit an username and a password for a different application. I'd like to take that password and encrypt it. Later, when I want to connect to that different application I need to decrypt the password.

Is there a common pattern for this scenario? Or some other advice you might have?
Just in case it matters, I want to connect to JIRA without having the user have to submit his password on every request / login to the page.

Thanks, Sven

Update
To make it more clear, I have a web appliction where users can signup/login/etc, this uses a authentication/authorization library (friend) and its all well.
However, from my webapp I want users to connect to a jira instance that they can choose and where they have to provide a username/pw combo which I cannot hash, because I need to send them unhashed to the JIRA instance.

What I am thinking of right now is to display a login dialog to the user as soon as he wants to request his JIRA instance. There he provides his username/password combo for JIRA and I send a REST request to JIRA from the client side, so no user/password is sent to my server.
I get a sessionid back then, which I can use for further requests. What do you think about that approach?

答:
Your ammended approach sounds better -- see the caveats below.

Saving passwords on behalf of another application is a really big no no. First, if your encryption keys or datastore are compromised, then you've not only exposed the users of your app, but the users of the other app as well. And because people tend to re-use passwords, odds are you've compromised their security on other systems too. So saving user passwords to other systems is bad, bad, bad.

OAuth was developed for exactly this reason -- so two systems could share authorization without exposing user credentials to each other.

In fact, the constraints around passwords are so strict that usually even an application isn't allowed to save encrypted passwords itself. Rather, it salts and hashes the password and saves the salted hash. That way the best a hacker can do is get the hashes. However, remember when LinkedIn got hacked and released just their hashes? Even hashed passwords are like gold to hackers. That in itself was a big deal -- a bad security breach.

Your ammended solution is better -- where you hang on to the session ID rather than the client credentials.

The caveat is that users are still giving their username/password to your application, and trusting you to do the right thing. Phishing sites do pretty much the same thing, with bad intentions. Really the best thing to do would be to have users log in to Jira in a separate window, and then redirect over to your site with the session ID. That way they only ever send their password to Jira and you're not in the loop at all, until you get a session ID.

You might investigate and see if Jira supports OAuth, which would handle this case (full disclosure: it feels like I should know that). Or if they have some other mechanism for doing what you want. It may be that they've already provided a hook to do what you want to do.

... and if they haven't, then it raises a serious question of if you should ...

If this is an internal app within an organization, you have more leeway, because it's less likely someone inside the firewall will try to compromise the system (unless you are paranoid, or you work at Target).

But if this is an external, public-facing app exposed to the Internet, then ... hmmmm ... you'd want to get out of the business of handling people's external credentials as soon as you could.


问:
What is the most productive way to handle development-related failures?
We've all been there:

Your project failed or got cancelled.

The code you spent days working on got rejected by your team.

The design pattern you introduced to the team created chaos.

Everyone ignores your ideas.

My question is, what is the most productive way for a programmer to handle development-related failures such as these?

答:
Step 1:  It's ok to get angry!

First, it's understandable to be upset or angry when encountering failure.  If giving advice to someone in such a situation, they are most likely not going to want to hear "Just get over it and move on" or "Just think of it as a learning opportunity".

In fact, I think it can be healthy and productive to get upset and vent your frustrations, provided that you do it privately or with a friend.  People have different ways of doing that, but I think one of the most productive ways is to write an angry fake letter (Important! do not send this letter to anyone).  Explain your feelings, such as why you feel that whatever happened was unjustified.

Step 2:  Take some time to calm down.

Make sure that you've expressed everything that you're feeling, and after venting your anger just take some time to calm down.  Maybe you only need a few minutes, or perhaps a few hours.

Step 3:  Review what happened in step 1

At this point you will hopefully be able to think more objectively about the situation.  If you wrote a letter, then read it to yourself.  If you confided in someone, then try to remember what you said.  If you just imagined shouting at someone, then just mentally review that.

I often write a letter when I'm angry and then after I've calmed down I will work on refining the letter to more clearly communicate what I was originally trying to say until I'm satisfied that someone reading it would understand what I was feeling at the time.

The point is to objectively try to pick out what your points were.  Did they make sense?  Perhaps they need clarification or further detail.  Are they unfounded?  If you were to objectively put yourself in someone else's shoes, would you understand the points that you made?  Would you agree with those points?  You can use this opportunity to evaluate yourself.  What did you do well?  What were some things that you could have done better?

Step 4:  Decide on a course of action

Is there something that can be done to remedy the situation or to at least improve it?  Take a moment to consider if there is realistically anything that can be done to fix or improve the situation.  Often there isn't, but sometimes there is.

If you are at fault for something, then that might be as simple as a formal apology to someone, clearly explaining what went wrong, what you did, why you did it, and what you will do to fix it or prevent it from happening in the future.

Next, consider what you can do to improve the future.  What can you do to prevent the same thing from happening again?   Decide what it is that you want to achieve and use what you've learned from step 3 to create a plan for yourself.

If all else fails, try rebooting:

```<code>        try
        {
            // ...
        }
        catch (OhNoes111Exception)
        {
            // reboot fixes everything!
            System.Diagnostics.Process.Start("ShutDown", "/r");
        }
</code>```

Body:
Step 1:  It's ok to get angry!

First, it's understandable to be upset or angry when encountering failure.  If giving advice to someone in such a situation, they are most likely not going to want to hear "Just get over it and move on" or "Just think of it as a learning opportunity".

In fact, I think it can be healthy and productive to get upset and vent your frustrations, provided that you do it privately or with a friend.  People have different ways of doing that, but I think one of the most productive ways is to write an angry fake letter (Important! do not send this letter to anyone).  Explain your feelings, such as why you feel that whatever happened was unjustified.

Step 2:  Take some time to calm down.

Make sure that you've expressed everything that you're feeling, and after venting your anger just take some time to calm down.  Maybe you only need a few minutes, or perhaps a few hours.

Step 3:  Review what happened in step 1

At this point you will hopefully be able to think more objectively about the situation.  If you wrote a letter, then read it to yourself.  If you confided in someone, then try to remember what you said.  If you just imagined shouting at someone, then just mentally review that.

I often write a letter when I'm angry and then after I've calmed down I will work on refining the letter to more clearly communicate what I was originally trying to say until I'm satisfied that someone reading it would understand what I was feeling at the time.

The point is to objectively try to pick out what your points were.  Did they make sense?  Perhaps they need clarification or further detail.  Are they unfounded?  If you were to objectively put yourself in someone else's shoes, would you understand the points that you made?  Would you agree with those points?  You can use this opportunity to evaluate yourself.  What did you do well?  What were some things that you could have done better?

Step 4:  Decide on a course of action

Is there something that can be done to remedy the situation or to at least improve it?  Take a moment to consider if there is realistically anything that can be done to fix or improve the situation.  Often there isn't, but sometimes there is.

If you are at fault for something, then that might be as simple as a formal apology to someone, clearly explaining what went wrong, what you did, why you did it, and what you will do to fix it or prevent it from happening in the future.

Next, consider what you can do to improve the future.  What can you do to prevent the same thing from happening again?   Decide what it is that you want to achieve and use what you've learned from step 3 to create a plan for yourself.

If all else fails, try rebooting:

        try
        {
            // ...
        }
        catch (OhNoes111Exception)
        {
            // reboot fixes everything!
            System.Diagnostics.Process.Start("ShutDown", "/r");
        }

ContentLicense: CC BY-SA 2.5


问:
Communicating with Vendors
We are a small software team (as far as programmers go) and have a team of vendors on the other side of the world that program for us. We own the product, and simply dictate to them some of the tasks to work on. I am one of the lead programmers and also the project manager of this particular project, so I am a programming and fulfilling requirements as well as outlining them.

I am having difficulties communicating with the vendors exactly what is expected of them. Let me start by saying we are fairly new at this and I don't have a lot of experience leading a team of vendors, especially when it is difficult to communicate verbally in English, and they work during my night and then I come into the code they pushed while I was asleep. Problem is, I end up spending a lot of my day just checking what they did, and fixing bugs and cases they didn't think of. They're not thinkers, they do exactly what I tell them, and nothing more or less. Most of the time, I feel like it would just be easier to do it myself.

My question is how do most teams like this communicate? Right now we have weekly telephone meetings and I email them nightly the progress I made, as well as what is expected of them. When I think "how to communicate with other programmers" the answer seems to be UML. That's what it is. I certainly am very familiar with UML, learned it in school, but have never really used it on the job. It's just not something we do, in fact generally the requirements for a task are in my manager's head. I can get those into a spreadsheet or a flowchart, but never an official diagram.

Is UML something teams like this actually use? In all reality, I feel like I learned a lot in school that no one actually does. If so, which diagrams are the most useful/used? From my knowledge, in this project where we are revamping something that exists in the system already from the ground up, I feel like the following would be a good approach:

Create a quick ER diagram containing any added/updated/used entities in the project.

Create a detailed use case model, clearly defining and numbering each use case.

Create a sequence diagram for the complex use cases (as well as their alternate flows) to show exactly what is expected each step of the way.

I feel like this would be a good start. We don't really do a good job of capturing requirements currently, we kind of just start coding based on something we drew up on a whiteboard. Obviously this needs to change as well, to avoid getting a week into a project and realize that we forgot something.

What are your suggestions/experience? Unfortunately we're sort of flying blind and just don't have anyone experienced enough in these situations to go to. I want the project to be a success, but I can't keep having the vendors (understandably) making mistakes because I assume they know X or Y. How can I utilize them more effectively?

答:
Having been in your situation on a few occasions, I've found that UML diagrams and the like are not a good way to go. They tend to be overly complex, and much more difficult to communicate about - now your audience not only has to understand what you said in english, they also have to be familiar with UML diagrams and know how to apply them to the work they're doing.

The goal is not to come up with what you consider to be cleaner/more concise/more accurate ways to write down your instructions, but to make sure that your instructions are more easy to understand/follow for a non-english speaking audience, that is the product of a different educational system. With that in mind - UML diagrams will only help if your target audience is capable of understanding them better than spoken/written english.

The following approaches/ideas have worked for me:

You're stuck being a project manager. You might have to accept this means an entirely different set of skills, work, etc... that you'll find yourself using - a different job description from being a software developer. You might also have to accept (and explain to any overlords you may be reporting to) that this is a time consuming job - you can avoid having to rewrite their code, but there's other work you'll have to do instead, which will impact your job as a software developer

Make sure that you're regularly speaking with the people actually writing your code - try to eliminate the intermediary level(s) of management.

This might mean going for a more involved micro-management approach (say, daily agile-style standups, where you do video chats with the dev teams in groups of less than 7, and have them each report on what they've done today, what they're stuck on, what they'll do tomorrow). Or it might mean trying to get the lead developers to show up to your weekly meetings, and getting them involved in the discussion about what code they've written/they're about to write. Maybe you can put them on your chat contact list, and, when you code-review their work, ask them directly about code they've written; cultivate a friendly relationship.

The goal is to open up communication channels more - so that the people who are actually writing the code understand that (a) you're holding them to a higher standard, (b) you actually care about them, and the work that they're doing and (c) making yourself available more easily/quickly for any questions they have when they interpret your instructions.

Learn to speak "their" version of english. More frequent communication should help with this - expose yourself to as many different people on the otherside, all speaking about things that you're intimately familiar with, and try to pay attention to/pick up idioms, expressions, etc... that they are using - and then to use them when describing your requirements.

Add unit testing to your list of requirements. Have them write a unit test framework, and write unit tests for each story - it's often easier to send over an extra unit test and ask that it be green, than to explain what you want; maybe not everyone involved speaks english, but everyone involved should, theoretically, speak code.

Make sure that you include UAT with each story description, not just requirements. Use a standard pattern - "As a {user description} i want to do {some action} which results in {specific description of result}". Ask them to provide you with tests that show the code actually does what you asked it to do - again, it may be faster/easier to modify tests and send them back, than to re-explain yourself

Most outsourcing outfits are extrinsically motivated; they're in it for the money. For development work, especially if it requires any creativity, that's a major downfall. Either split the work up so that anything you send to them is the kind of work that's easy for people who are only in it for the money to do (work that doesn't take creative thinking is ideal), or get them to be more intrinsically motivated.

For that last bit - the low hanging fruit include switching to video chat for all your communication; learning about things like holidays, birthdays, etc... and mentioning them on calls; making it a point to highlight particularly well done work individually, and with a bit of delay - and highlighting bugs/issues as soon as they're discovered, and with a can-do-team-approach tone of voice ("here's a bug - can we get this fixed?" vs. "who wrote this? sam? dude, your code here was awesome!"), etc... If you do manage to get to know the devs, it should help - find out what they each like about the work you're sending, and see if you can help them to do the bits they like.

Title: Communicating with Vendors


答:
However in Scrum you are only supposed to have user stories that
  provide value to the user.

Value isn't measured only in lines of shippable code.

You seem to be implying that having a well designed UI doesn't provide any value. Of course it does. Obviously there's value to the end user, but there is also value to your development team, which is a perfectly valid stakeholder. If you don't have the tools and materials to do your job, you can't deliver value to the end user.

Don't get hung up on scrum dogma. Scrum is there to make you more efficient. If doing a UX story one sprint before you implement the UI helps you deliver better software, do it.

Title: UX designers working one Sprint ahead

Body:
Our UX designers usually have a story in Sprint X that the developers will implement in Sprint X+1 (The UX designers and the devs/testers are on one team). I think this makes sense because if you don't have screen mockups and clear specifications you can't really estimate the work during Sprint Planning.

However in Scrum you are only supposed to have user stories that provide value to the user. In our case the UX design stories don't provide such value (they are more like a backlog grooming activity). Also usually Scrum coaches don't recommend having complete specifications before the start of the Sprint, a recommendation which I find hard to understand.

So do you see any disadvantages in our approach? It seems to work for us, but it goes somewhat against Scrum principles.


问:
Web API architecture design
I'm learning and diving into Web API.  I've used web services before but not specifically with web API.  Below is how I am designing it so far and I was curious on the feedback.

I have a ReturnMessages object.  This basically is a standard object that gets returned from any of the API calls, correctly executed or an error happens.   Within each API method I have a try catch.  if everything is alright, I specify the values I need within my ReturnMessages object and than Ok(_returnMessages).   Now if an error happens I fill in the ReturnMessages object with the error information and once again return Ok(_returnMessages).

The ReturnMessages object contains a ReturnClass field to hold any type of other objects I may need to return.  A single object or an Array.  It also has a return code, return message, friendly message for the end user in case something wrong happens and than a generic string list of data that was passed in that I can send off and use for testing purposes to try and re-create the error.

Below is a code sample from one of the methods that shows off what I am talking about.  Is this approach ok with always returning Ok with the object I'm returning or am I missing potential pieces within the Web API that I should be utilizing?  I've seen the NotFound exceptions and all that other fun stuff.

**EDIT:  I made some changes in terms of what I was passing back for when things don't work out.  I took out the ReturnMessage and also the ReturnData and wrote that to the Application event log for a new string value for this particular web api.  Taht occurs in the _lw code line which is just a LogWriter class that uses the values passed in to write to the application event log.

```<code>public IHttpActionResult method1(string arg 1= null, string arg2 = null, string arg3 = null)
        {
            try
            {
                clrObject1 varClrObject = (from p in db.clrObject1
                                            where p.column1 == arg1
                                            select p).SingleOrDefault();

                if (varClrObject == null)
                {
                    _returnMessages = new ReturnMessages
                    {
                        ReturnCode = 204,
                        FriendlyErrorMessage = "Nothing found matches the supplied values."
                    };

                   _lw.SetupEventLog("No data was found that matched the information supplied.\n\nParameters:\narg1: " + arg1 + "\narg2: " + arg2 + "\narg2=" + arg2, "Warning");
                }
                else
                {
                    _returnMessages = new ReturnMessages
                    {
                        ReturnCode = 200,
                        ReturnClass = varClrObject,
                        ReturnMessage = "Information Successfully Retrieved"
                    };
                }
            }
            catch (Exception e)
            {
                _returnMessages = new ReturnMessages
                {
                    FriendlyErrorMessage = "An error has occurred while getting your information. Please try again in a few minutes.  A notification was already sent to the company about this issue.",
                    ReturnCode = 400
                };

                _lw.SetupEventLog("Parameters:\narg1: " + arg1 + "\narg2: " + arg2 + "\narg3=" + arg3, "Error", e);
            }

            return Ok(new { Response = _returnMessages });
        }
</code>```

答:
You are overcomplicating this.

Web API is designed to let you write your controller code is as "natural" a way as possible. The IHttpActionResult return type is mostly intended as an escape hatch when you want maximal flexibility.

Here is how I would write your method:

```<code>public ClrObject1 Method1(string arg1 = null, string arg2 = null, string arg3 = null)
{
    ClrObject1 varClrObject = (from p in db.clrObject1
                               where p.column1 == arg1
                               select p).SingleOrDefault();

    if (varClrObject == null)
    {
        // This line will automatically return a 404 error to the user.
        throw new HttpResponseException(HttpStatusCode.NotFound);
        // If you want, you can also build a full HttpResponseMessage
        // to give more details about the error.
    }

    // Web API will automatically return serialize and return your object
    // in a 200 OK response.
    return varClrObject;
}
</code>```

You will notice that I do not put a try-catch block to handle exceptions - the reason is that, if any exception is raised and unhandled, an error 500 will be returned to the user, which is usually what I want. Also, you do not want always to return details about your exception in the HTTP response, as such information could disclose information that could be used in helping hacking the application server.

Title: Web API architecture design

Body:
You are overcomplicating this.

Web API is designed to let you write your controller code is as "natural" a way as possible. The IHttpActionResult return type is mostly intended as an escape hatch when you want maximal flexibility.

Here is how I would write your method:

public ClrObject1 Method1(string arg1 = null, string arg2 = null, string arg3 = null)
{
    ClrObject1 varClrObject = (from p in db.clrObject1
                               where p.column1 == arg1
                               select p).SingleOrDefault();

    if (varClrObject == null)
    {
        // This line will automatically return a 404 error to the user.
        throw new HttpResponseException(HttpStatusCode.NotFound);
        // If you want, you can also build a full HttpResponseMessage
        // to give more details about the error.
    }

    // Web API will automatically return serialize and return your object
    // in a 200 OK response.
    return varClrObject;
}

You will notice that I do not put a try-catch block to handle exceptions - the reason is that, if any exception is raised and unhandled, an error 500 will be returned to the user, which is usually what I want. Also, you do not want always to return details about your exception in the HTTP response, as such information could disclose information that could be used in helping hacking the application server.


问:
What are the hardest parts of the C++/C#/Java programming languages?
Just wondered what are the features of the three main programming languages which show you are an 'expert'? Please exclude 'practical' skills such as indenting.

Am I right in saying for C++ the most difficult aspect to master is STL/generics? Java seems much easier as memory is handled for you. I'm not entirely sure on C# either?

I'm trying to use this to guage my current level of ability and what i wish to aim for.

ps this was posted on stackoverflow but got binned due to arguing, please do try to keep it civil as I am really interested in the answers from everyone :)

答:
Making programs thread-safe and scalable to many, many cores.

But that is hard in any language.

OwnerDisplayName: tom


问:
How can you design a application when you have not imagined completely how the app look like?
Here is the problem, an enthusiastic man wants to build an app, however he only knows roughly how the app should work. There are a lot of details he didn't know.

How does he design his app?

-Option1:

Do not design, but just code the most important first & test to see how it goes then modify gradually. After the highest priority function finished, he can move to the next priority function. Bottom-up Approach.

-Option2:

Keep thinking until he realized all the details then design a complete app before actually coding. Top-down Approach.

-Option3

Combine of both option1 &2, ie he can design some basic function first then start coding even he didn't realise all detail then modify accordingly.

It's very hard to know all the details right at the beginning cos we are human & we can't predict the future.

So i vote for option1 or 3. But it's kind of painful if you has to change ur code if you're missing the details. A lot of things has to be changed. My instructor in university say he always prefers top-down approach cos he believed it better.

However, i think build a software just like u live a life, you never know in advanced what will happen in the future, so option1,3 could be the better one.

What is your solution?

答:
Most software development involves a combination of top-down and bottom-up approaches. Personally I think it is very poor practice to start coding before you have designed the big picture. You will end up with a bunch of pieces that don't fit well together. Start by identifying your major components and their interfaces, and how they interact with each other. In OOP you should have all your important class interfaces designed before you start to write code. After you start coding you will surely add more helper classes which just help keep the code neat, but that's OK. If you program to an interface, not an implementation then you can sculpt out the entire flow without implementing any of the classes. You can start implementing each class one at a time and have the others return mock values until they are ready. This way you always have a complete picture.

As you said, you can't know 100% every curve ball that will be thrown which is why top-down and bottom-up are used together. Design your program, start writing, and if you need to go back to the drawing board because something doesn't fit right then that's OK.


问:
How do I evaluate if an Orchestration is the appropriate design pattern for a given problem?
Say that you're doing a code review, and you find yourself faced with an orchestration pattern:

```<code>class OrchestrationClass {
  private Configuration _configuration;
  private DataStore1 _dataStore1;
  private EfficientComputationService _service;
  private ResultPrettifier _formatter;

  public DoSomething () {
     with (data = _dataStore1.LoadForConfiguration(_configuration)) {
        return _formatter.PrettyPrint(_service.Process(data));
     }
  }
}
</code>```

Under which circumstances is this considered to be a valid pattern/anti-pattern (and how is the anti-pattern to be cleanedup/refactored)?

For context:

This recently came up in a code review, and several people mentioned the pattern/anti-pattern. Searches of the standard patterns literature, related Google search and scan through blog posts / wikis have all led to indirect mentions. Which leads me to believe that this pattern / anti-pattern hasn't yet had a formal treatment (or, if it has, it's not yet online). Hence - the question is here, where someone more experienced or widely read might be able to answer (or provide a reference)

答:
If by orchestration you mean take 2+ parts and use them to do 1 larger task... this is called programming where I live. Maybe "abstraction" if you're feeling frisky.

I mean seriously, this is basic object oriented programming. It isn't special, and doesn't need a fancy name.


问:
Is Spring Source Tool suite required for J2EE application development?
I am following a tutorial. The guy keeps on adding different plug-ins such as subclipse, maven, spring source tool suite, etc after setting up jdk, eclipse, tomcat.

While installing spring, he was saying that spring makes it difficult to debug the programme. That didn't make me happy. He also added, it's up to you if you want to install it.

Since I am not familiar with it, I just want to know if it's alright to not to have spring at all? Is there really any use of having all those plug-ins?

答:
Is Spring Source Tool suite required for J2EE application development?

No.

Not even if you are using Spring in your application.

And there is no requirement to use Spring either ... though it does make a lot of things simpler.

Is there really any use of having all those plug-ins?

Obviously there is some use, otherwise he wouldn't be using them in the first place!  But for each of the plugins you have identified, you can get by without using the plugin:

Instead of m2eclipse, you can construct the Eclipse buildpaths by hand, and then run your Maven builds from a command shell.  (I would not recommend this ... but it is possible.)

Instead of subclipse, you can do SVN checkins / checkouts by hand from a command shell.  You just need to remember to do an F5 refresh afterwards to keep Eclipse in sync with the file system changes.

Instead of the Spring Tool Suite, you can do spring programming, wiring, etc the old-fashioned way ... just like we all did before the plugin was available.

You need to make up your own mind ... like the guy in the tutorial said!


答:
I usually talk about Orchestration in the context of RESTful services. That is, we provide RESTful services that give direct access to various resources. But there are some teams out there that, for one reason or another, just aren't in a position to handle that. So we provide an orchestration layer on top, where we tie together the resource calls that we'd expect them to do.

(That idea is a little controversial, because as expected, when I've been on a team that did that, due to politics the orchestration layer just devoured and hid the REST layer completely.)

Except as a general idea, I haven't heard orchestration as such doesn't come up in application design, though. In a sense it's a consequence of modular, shallow-call-stack design. At some point you have to pull the low-level pieces together.

To me that's a question of whether you throw in a layer of abstraction between the main() or API layer and the logic layer. Do the orchestration at the very top? (i.e. in your main() method, or in the webapp handler classes) Or if you throw in a layer of abstraction between main() and the workers?

I usually do, because I like to pretend that the application logic is decoupled from the API implementation. But I don't know if there's a formal name for that, or rules around it.

Title: How do I evaluate if an Orchestration is the appropriate design pattern for a given problem?

Body:
Say that you're doing a code review, and you find yourself faced with an orchestration pattern:

```<code>class OrchestrationClass {
  private Configuration _configuration;
  private DataStore1 _dataStore1;
  private EfficientComputationService _service;
  private ResultPrettifier _formatter;

  public DoSomething () {
     with (data = _dataStore1.LoadForConfiguration(_configuration)) {
        return _formatter.PrettyPrint(_service.Process(data));
     }
  }
}
</code>```

Under which circumstances is this considered to be a valid pattern/anti-pattern (and how is the anti-pattern to be cleanedup/refactored)?

For context:

This recently came up in a code review, and several people mentioned the pattern/anti-pattern. Searches of the standard patterns literature, related Google search and scan through blog posts / wikis have all led to indirect mentions. Which leads me to believe that this pattern / anti-pattern hasn't yet had a formal treatment (or, if it has, it's not yet online). Hence - the question is here, where someone more experienced or widely read might be able to answer (or provide a reference)


问:
How to create a some kind of value for sentences?
I want to identify most matching sentence using some pattern. That means by using java algorithm I want to create identical value for each sentences.Each sentence when entering to that algorithm can be out some kind of identical value. How can I develop it? What can I refer any web sites do you know? What sort of sites should I look for? Actually I want clarify about when I'm giving as ex: 5 sentence to algorithm that possible to generate some kind of 5 values.Then I compare with those values with previously generated values(I should be store those values in my database) and get the gap between new 5 value and previously stored values.Then I get the distance and I selected most suitable sentence as most lowest gap value.

I'm use those things for my machine translation tool.
As an example we think using my ruled based translation model generate 2 sentences.
1. I want eat an apple.
2. I want eat a house.
In my corpus we think more sentences include and I store values for sentences in my database. (Value assign part is I don’t know yet)
I want to create java algorithm to assign value for each sentence. As an example if we think
Sentence 1 value:  250.8
Sentence 2 value   :  290.5

Database included values 248, 400,800
Then I got the difference.
So we can see here most minimum difference get for 250.8 and most suitable sentence is 1 one.

答:
If you want to create unique value for each and every sentence, then don't even try, because thanks to Pigeonhole principle, you are guaranteed to get collisions and thus the identifiers won't be unique. You could limit the input space accordingly, but in this case the algorithm looses it's purpose.

If you are looking for way to create identifier than might indicate that sentences are be equal, but not guarantee it, then hashing is practically created for this purpose. This the allows you to check if the sentences might be equal, but you still have to run the equality function to guarantee it.


问:
Best design pattern for library with data model
I am refactoring an old Codeigniter library to be php framework agnostic. There are these core parts:

the connection object - this will come from the "user"; ie, the framework's connection, or their own connection library. My library will not want to "know about it" except that (at first) it will be a PDO conn object (but things should be written to handle a native driver type)

the model object - runs the queries & returns results array (or error array) to the main lib. As it is now, it is very "thin" and only has transactions, query building & the actual db query

the model interface - I'm a big fan of interfaces, and I see this model as being swappable for a NoSQL model, for example

the main library - in my current CI library, this 1) receives the function request, 2) validates the params passed to it, 3) requests from the model, 4) creates a response array that includes the data & error codes and messages (multilingual configurable, but not really part of this question)

So, in summary, the way it works NOW is in your controller or calling page:

1) call main lib function
2) main lib calls model
3) model returns to main lib
4) main lib returns to controller

NOW starts my real design pattern question:

I would like to pass my conn object via DI to my main library, but I'd also like to pass my model via DI to the main library to keep things flexible. The flexibility goal I'm after would be to allow people to create their own Native DB or NoSQL models & pass non-PDO conns into this, so keeping them decoupled seems optimal.

BUT to do this, the only thing I can think of would end up as:

(controller - where I might actually execute the query in the lib, not model)

```<code>$conn //from the user
$model = new MyModel();
$lib = new MyLib($conn, $model);
</code>```

or

```<code>$conn //from the user
$model = new MyModel($conn);
$lib = new MyLib($model);
</code>```

which feels more "natural" since the query & conn tend to work together

Either case feels pretty clumsy for the user, and I can't help but wonder if I'm not either overdoing this whole DI thing or just have picked the wrong design pattern to go about this.

Any guidance as to how to best set this up would be greatly appreciated - spent more time thinking about this than it will take me to refactor the actual code!

TIA

答:
It might not be the "cleanest" but what about adding a function to overload the model.  So by default the library can instantiate your model so it's all clean for the user but when testing you have the option to overload it.

I'm not a testing die hard though so this answer might be refuted easily.


答:
A basic requirement of Scrum is that the scrum team has all the skills needed to create a potentially releasable product. In the situation you describe, this is not happening.

The UX team is not producing potentially releasable product and the scrum team are not capable of producing vertical slices of functionality as they do not have all the required skills. These are dysfunctions.

Sklivvz has written an excellent post on the problems that the above dysfunctions could lead to. In summary, I don't think you are practicing Scrum.

But there's absolutely nothing wrong with that. If you have discovered a way of working that delivers value for you all, and you're all happy with it, keep doing it. My only advice would be to inspect and adapt frequently.

Note, however, that if your aim is to deliver software using Scrum, you'll need to address the dysfunctions identified.

Title: UX designers working one Sprint ahead


问:
Understanding dependency injection
I'm reading about dependency injection (DI). To me, it is a very complicated thing to do, as I was reading it was referencing inversion of control (IoC) as well and such I felt I was going to be in for a journey.

This is my understanding: Instead of creating a model in the class which also consumes it, you pass (inject) the model (already filled with interesting properties) to where it is needed (to a new class which could take it as a parameter in the constructor).

To me, this is just passing an argument. I must have miss understood the point? Maybe it becomes more obvious with bigger projects?

My understanding is non-DI (using pseudo code):

```<code>public void Start()
{
    MyClass class = new MyClass();
}

...

public MyClass()
{
    this.MyInterface = new MyInterface();
}
</code>```

And DI would be

```<code>public void Start()
{
    MyInterface myInterface = new MyInterface();
    MyClass class = new MyClass(myInterface);
}

...

public MyClass(MyInterface myInterface)
{
    this.MyInterface = myInterface;
}
</code>```

Could some one shed some light as I'm sure I'm in a muddle here.

答:
At the class level, it is easy.

'Dependency Injection' simply answers the question "how will I find my collaborators" with "they are pushed at you  - you don't have to go and get them yourself".  (It is similar to - but not the same as - 'Inversion of Control' where the question "how will I order my operations over my inputs?" has a similar answer).

The only benefit that having your collaborators pushed at you is that enables client code to to use your class to compose an object graph which suits its present need ... you have not arbitrarily pre-determined the shape and mutability of the graph by privately deciding the concrete types and life-cycle of your collaborators.

(All those other benefits, of testability, of loose coupling, etc, follow largely from the use of interfaces and not so much from the dependency-injection-ness, although DI does naturally promote the use of interfaces).

It is worth noting that, if you do avoid instantiating your own collaborators, your class must therefore get its collaborators from a constructor, a property, or a method-argument (this latter option is often overlooked, by the way ... it does not always make sense for a class' collaborators to be a part of its 'state').

And that's a good thing.

At the application level ...

So much for the per-class view of things.  Let's say you have a bunch of classes which follow the "do not instantiate your own collaborators" rule, and wish to make an application from them.  The simplest thing to do is to use good old code (a really useful tool for invoking constructors, properties, and methods!) to compose the object graph you want, and throw some input at it.  (Yes, some of those objects in your graph will themselves be object-factories, which have been passed around as collaborators to other long-lived objects in the graph, ready for service ... you can't pre-construct every object!).

... your need to 'flexibly' configure your app's object-graph ...

Depending on your other (non-code-related) objectives, you might want to give the end-user some control over the object graph thus deployed.  This leads you in the direction of a configuration scheme, of one sort or another, whether it be a text file of your own design with some name/value pairs, an XML file, a custom DSL, a declarative graph-description language such as YAML, an imperative scripting language such as JavaScript, or something else appropriate to the task at hand.  Whatever it takes to compose a valid object graph, in a way that meets the needs of your users.

... may be a significant design force.

In the most extreme circumstance of that type, you could elect to take a very general approach, and give end users a general mechanism for 'wiring up' the object-graph of their choosing and even allow them to provide concrete realizations of interfaces to the runtime!  (Your documentation is a gleaming jewel, your users are very smart, familiar with at least the coarse outline of your application's object graph, but don't happen to have a compiler handy).  This scenario can theoretically occur in some 'enterprise' situations.

In that case, you probably have a declarative language which allows your users to express any type, the composition of an object graph of such types, and a palette of interfaces which the mythical end-user can mix and match.  To reduce the cognitive load on your users, you prefer a 'configuration by convention' approach, so that they only have to step in and over-ride the object-graph-fragment of interest, instead of wrestling with the whole thing.

You poor sod!

Because you didn't fancy writing up all that yourself (but seriously, check out a YAML binding for your language), you are using a DI framework of some kind.

Depending on the maturity of that framework, you may not have the option of using constructor-injection, even when it makes sense (the collaborators don't change over the lifetime of an object), thereby forcing you to use Setter Injection (even when collaborators don't change over the lifetime of an object, and even when there is not really a logical reason why all concrete implementations of an interface must have collaborators of a specific type).  If so, you are currently in strong-coupling hell, despite having diligently 'used interfaces' throughout your code-base - horror!

Hopefully though, you used a DI framework which gives you option of constructor injection, and your users are only a bit grumpy at you for not spending more time thinking about the specific things they needed to configure and giving them a UI more suited to the task at hand.  (Although to be fair, you probably did try to think of a way, but JavaEE let you down and you had to resort to this horrible hack).

Bootnote

At no point are you ever using Google Guice, which gives you the coder a way to dispense with the task of composing an object-graph with code ... by writing code.  Argh!

Title: Understanding dependency injection


答:
What you are describing are different project management approach - Option#2 is a waterfall approach, where you first describe the product, then you design it, then you implement it...

Advocates of Agile software development argue the waterfall model is a
  bad idea in practice—believing it impossible for any non-trivial
  project to finish a phase of a software product's lifecycle perfectly
  before moving to the next phases and learning from them.[citation
  needed]

For example, clients may not know exactly what requirements they need
  before reviewing a working prototype and commenting on it. They may
  change their requirements constantly. Designers and programmers may
  have little control over this. If clients change their requirements
  after the design is finalized, the design must be modified to
  accommodate the new requirements. This effectively means invalidating
  a good deal of working hours, which means increased cost, especially
  if a large amount of the project's resources has already been invested
  in Big Design Up Front.[citation needed]

Options #1 and #3 are very simplistic descriptions of a more Agile approach, though these approaches tend to be more formal than "let's hack through it!"

I believe the enthusiastic man should first understand fully what problem he wants to solve. Then, an approach which resembles Option #1 - a Behavior Driven Design - might work for him, where he writes the main User Stories for his solution, describes them, then implements them in short-iterations.

This will produce a quick MVP he can assess, quickly decide if the product is indeed viable, and if it is, start adding more user stories for another iteration...

Title: How can you design a application when you have not imagined completely how the app look like?

Body:
Here is the problem, an enthusiastic man wants to build an app, however he only knows roughly how the app should work. There are a lot of details he didn't know.

How does he design his app?

-Option1:

Do not design, but just code the most important first & test to see how it goes then modify gradually. After the highest priority function finished, he can move to the next priority function. Bottom-up Approach.

-Option2:

Keep thinking until he realized all the details then design a complete app before actually coding. Top-down Approach.

-Option3

Combine of both option1 &2, ie he can design some basic function first then start coding even he didn't realise all detail then modify accordingly.

It's very hard to know all the details right at the beginning cos we are human & we can't predict the future.

So i vote for option1 or 3. But it's kind of painful if you has to change ur code if you're missing the details. A lot of things has to be changed. My instructor in university say he always prefers top-down approach cos he believed it better.

However, i think build a software just like u live a life, you never know in advanced what will happen in the future, so option1,3 could be the better one.

What is your solution?


问:
Which statement performs best?
I was faced with this question by my team leader.

Statement 1 (written by me):

```<code>lnkbtn1.visible = lnkbtn2.visible = lnkbtn.visible = false;
</code>```

Statement 2 (written by my team leader):

```<code>lnkbtn1.visible = false;
lnkbtn2.visible = false;
lnkbtn3.visible = false;
</code>```

Which statement is better and why?

答:
Just because somebody's a "team leader" doesn't mean much.

If .visible is nothing more than a simple boolean variable, the difference in cost is either zero or basically nothing.

However, if .visible is a property, all bets are off.
Assigning to a property is one method call, and retrieving the value is another.
In that case, the separate assignments might be faster, but watch out for another problem.
Properties have some nice properties (!), but I've seen them treated as if they were free, performance-wise, when in fact they can be major sink-holes of CPU time if they are implemented recursively.

In terms of style, I take a dim view of giving this more than a nanosecond of thought.
Nearly all serious software has far bigger issues than this.

Title: Which statement performs best?


问:
Why do most sites require email activation
Most popular applications nowadays require account activation by email. I've never done it with apps that I've developed so am I missing some crucial security feature?

By email activation I mean when you register to a site they send you an email that contains a link that you have to click before your account gets activated.

答: Confirmed Opt-In could also be done to comply with anti-spam laws for another reason to send a link in the e-mail.</p>


问:
How to POST CSV with XMLHttpRequest
I would like to send a CSV file via POST in a XMLHttpRequest, but I am unsure of two things. First is there anything to distinguish a CSV file from a string split up by comma's? And what sort of Content-Type am I supposed to put in to the setRequestHeader?

答:
A CSV is, well, a string separated by commas.  Trying to separate if there is meaningful data in string as CSV as opposed to A CSV is, well, a string separated by commas is the job for a parser and part of the reason richer data formats (XML, JSON) are often used.

This gets even more fun when you have different flavors of CSV format (line endings, header row, jagged array, muli-line strings in a single field, how to handle a ", etc...).  All of these things make CSV difficult to work with as there is no standard.

The closet thing to a standard would be RFC 4180 (this also specifies the mime type as text/csv which is the other part of your question).

Title: How to POST CSV with XMLHttpRequest


答: Activation confirms the email is yours. It's not so much about being bogus or non-existent, as it is about being yours and they need it for an alternative/plan-b way of authentication, in first place.</p>

Title: Why do most sites require email activation

Body:
Most popular applications nowadays require account activation by email. I've never done it with apps that I've developed so am I missing some crucial security feature?

By email activation I mean when you register to a site they send you an email that contains a link that you have to click before your account gets activated.


问:
Is it right to persist a computed property to the DB?
I would like to know what do you think about persisting an "autocomputed property" to the database.
For example I have these properties

```<code>decimal Price {get;set;}
decimal Tax {get;set;}
decimal PriceWithTax {get {return Price + Tax;}}
</code>```

I think I need persist PriceWithTax. I can use this column, for example, in DB views.
But I feel that this solution can lead to hidden bugs.
What do you think?

Edit: Ok, I try to explain our real problem. We have a class "Order" which has collection of items - class ItemOrders. Class ItemOrder has property Price and class Order has property TotalPrice. And implementation of TotalPrice:

```<code>TotalPrice {get { return ItemOrders.Sum(i => i.Price); }}
</code>```

And we need sorting, grouping in the database. So its not a bussiness rule but a "data rule".

答:
The first question that you should ask yourself is whether this is a business rule or something purely related to data.

For example, taxes a person should pay every year depends on multiple complex business rules. Those rules are subject to frequent changes, so it is unwise to make a computed column containing such information. On the other hand, a day and a month of birth of the person extracted from the full birth date doesn't depend on business rules, and it's very improbable that it will change during the lifespan of the application; thus, creating a computed column for that looks fine.

In your example, it's difficult to say for sure without additional context, but I suppose that the price with tax is a business rule. What if there would be customers who shouldn't pay a tax? What if the tax would be different from case to case?

The second question to ask is whether storing the computed column would benefit your application. In my previous example with the birth date, it may make sense to store this additional information in a context where you should frequently search for persons based on their day and month of birth, independently of the year of birth.

In your case, if the price with tax is as simple as the addition of price and tax, why creating an additional column? How would it make either easier, or faster your application?


答:
Yes you are. By verification of the email address you are making sure that the account holder owns that address.

Later if someone else tries to sign up with the same address this won't be possible as you've already verified that the correct owner already has the account.

Basically it ensures people who are who they say they are. If you had a social networking site where people could add contacts by email address, this will limit what a fraudulent user could do by using the address of another person.


答:
Most web services want to be able to contact users, and use email for that. In particular, the user forgets their password, the fact that they can read an email at the address the service has on file authenticates them as the legitimate user, who is allowed to reset the password. Email can also be a way to inform you of important updates (a.k.a. spam you).

In order for the service to validate the email, it must ensure that the user who is creating the account does have access to that email. This is both to protect the user against a typo in the email address, and to protect the site's ability to spam.

You don't have to require an email to subscribe to your service. However, when users forget their credentials, they'll be unhappy. Email password resets are the norm.

If the user chooses a disposable email address, it's a deliberate choice from them not to have exclusive control over the account. Some sites that want to spam try to reject such addresses, although it is of course futile (big sites like Gmail also let you create disposable addresses, after all).


答:
Computed columns add nothing to your data, and they break the DRY principal - you are repeating your data.

If you want to use the computed column for your views, AFAIK, most databases today support computed columns in your views (meaning - you define the column in your view as Tax + Price).

The only reason I can think of that would require you to save computed data is if the computation is non-trivial and time/cpu/memory consuming, in which case the computation on insert will free you from making the computation over and over again.

Another reason might be so you can create an index on the computed column for easy lookup. Most databases today though also support some kind of function based indexes, which will be a superior solution.

Title: Is it right to persist a computed property to the DB?

Body:
I would like to know what do you think about persisting an "autocomputed property" to the database.
For example I have these properties

```<code>decimal Price {get;set;}
decimal Tax {get;set;}
decimal PriceWithTax {get {return Price + Tax;}}
</code>```

I think I need persist PriceWithTax. I can use this column, for example, in DB views.
But I feel that this solution can lead to hidden bugs.
What do you think?

Edit: Ok, I try to explain our real problem. We have a class "Order" which has collection of items - class ItemOrders. Class ItemOrder has property Price and class Order has property TotalPrice. And implementation of TotalPrice:

```<code>TotalPrice {get { return ItemOrders.Sum(i => i.Price); }}
</code>```

And we need sorting, grouping in the database. So its not a bussiness rule but a "data rule".


问:
Should a large company standardize engineering tools across all teams internationally?
I've been thinking a lot about the problem of standardization across large companies.  For me, the answer is pretty clear: do not arbitrarily standardize on tools just because that's what everyone else does.  It's counter-productive.

I'm thinking about source control, bug tracking and testing tools primarily.  It seems so obvious to me that I can't really think of many good counter-arguments.  Some I've thought of :

Easier sharing between teams

Easier for IT to manage

(1) is definitely true if different teams are working on the same codebase/product.  But in cases where one team doesn't work directly with the code I don't think is a valid reason.

(2) is just kind of pisses me off if that's really an argument.  The limitations placed on programmers, I believe, is more burdensome than any extra work piled on to IT.  But I'm not an IT guy.

I'm hoping for some better arguments than those.

答:
You should rather think that your development process should make it easy to use any available tool for your project.

The reasons for this are simple:

You need to be able to build and test your software automatically outside an IDE.

You may have a facility in IDE X you need but cannot use if you are tied to IDE Y.  Examples:  GUI editor in NetBeans.  Swing debugger in JDeveloper.

Procedures captured in files (so they can be automated) can be checked in to a source repository, and checked out again elsewhere.  This means more precise knowledge is captured.

Building and testing can happen on multiple platforms allowing for easy testing of platform compatability.

By thinking like this, you are best insured against vendors closing products, or other natural disasters.


问:
Testing non-central features
Only 1% of our clients use particular features or scenarios. Do you think we should spend as much time testing these features and scenarios as we spend on our central features and scenarios, or we should test mostly the "happy path" for these features?

UPDATE

Maybe I should have asked the question in a different way. Do you think a company needs to spend more time testing the central features of its products? For example I assume the Windows 8 team tested the the boot sequence more in depth than they tested the driver for a very uncommon graphics card. The cost of a bug in a boot sequence is huge since no one would be able to use the OS. But a bug in a driver of an obscure GPU will hurt only a small client base.

答:
Let's pretend I published an encyclopedia. Only this one is special. I went and replaced the definition for sarsaparilla with a picture of a monkey-butt.

Most of my users will be happy that they can look up popular things like monkeys and butts, but the one person who desperately needed to know what a sarsaparilla was would find themselves in a bit of a pickle.

So it depends. Are you okay with your program potentially crashing on 1% of your user base when they misclick something? Are they likely to do so? If they are, you might want to spend some time in that section of the program.

Title: Testing non-central features


答:
You should test all features of your application. You don't want to present any failures to your clients just because you don't have the inclination to test as thoroughly as you should.

That said, there is a very practical purpose for not testing those features: time. If these are features that haven't been touched by the new code (either directly or by supporting code), it is highly unlikely, but not impossible that they will be broken with a new compilation.

UPDATE

Let me be very clear, I am always going to recommend testing everything in an application. Management may not allow me the time necessary to fully test. If that is the case, then I would put more emphasis on testing the most used components of the system.

Title: Testing non-central features

Body:
Only 1% of our clients use particular features or scenarios. Do you think we should spend as much time testing these features and scenarios as we spend on our central features and scenarios, or we should test mostly the "happy path" for these features?

UPDATE

Maybe I should have asked the question in a different way. Do you think a company needs to spend more time testing the central features of its products? For example I assume the Windows 8 team tested the the boot sequence more in depth than they tested the driver for a very uncommon graphics card. The cost of a bug in a boot sequence is huge since no one would be able to use the OS. But a bug in a driver of an obscure GPU will hurt only a small client base.


问:
Role of Quality Assurance (Not testing!) in Agile
From Wikipedia:

Quality Assurance refers to administrative and procedural activities
implemented in a quality system so that requirements and goals for a
product, service or activity will be fulfilled.[1] It is the
systematic measurement, comparison with a standard, monitoring of
processes and an associated feedback loop that confers error
prevention.[2] This can be contrasted with quality control, which is
focused on process output.

You can read the following article to understand the difference between QA and testing (quality control): Link

What I'd like to understand is whether there is a place for QA engineer who is not a tester in Agile/Scrum. Agile is deliberately short on processes and documentation (its even in the manifesto), and that's what QA specialize in. So should there even be a QA engineer in Agile?

I myself am not a QA engineer (I am a test lead), but I have a friend who is, so that's why I am asking.

答:
I'm not sure that I understand the difference (or how it is relevant) between "testing" and "quality assurance". In the end to do QA you must do testing.

QA, as you define it, sounds a bit against the Agile manifesto's value of Working software over comprehensive documentation. Do you really need all that process documenting? Are you trying to squeeze an existing process into an Agile one. If you really need the documentation you should add it to your definition of done.

Agile methodologies prefer "generalizing specialists" who do vertical slices. This means any team member should be able to complete any story. Developers should test, QA should test and everyone should work to build quality in.

Agile teams use retrospectives as the point where they improve their process.

In teams that I've worked in everyone is responsible for quality. Typically there is a lag between something testable in an iteration/sprint being ready for a tester. With this time, you can get your QA activities done just in time for testing of stories. Be careful for this introducing scope creep within an iteration.

Many Agile processes are not prescriptive about that level of detail and it is left to the team to decide. Would a self organising team ask for a QA specialist?

ContentLicense: CC BY-SA 4.0


问:
What is the real difference between a minified and uncompressed file? What are the benefits of each?
I often see something like "uncompressed for development / minified for production" when reading blogs or downloading frameworks. What is the real difference between the two, and if minified is "good for production" why is it not recommended for development?</p>

答:
For different concerns.

For development you want to be able to view source and see all the details.  You don't care about size much as it's often local anyway.  When problems arise you want to be able to view the browser source of the actual css to see if there's any issues but this means keeping whitespace, formatting duplication, etc.
When you want to try a small change in the browser itself its much easier to do in the browser source with unminified code.

For production you care more about the size, download speeds and number of http requests for real users so compressing and minifying is appropriate there.   You might also want to obscure your files more from casual browsing.


答:
Minification isn't compression in the classic sense. It refers to the removal of whitespace and other nonessential characters like comments so that the code is still valid but as compact as possible. It's effectively a form of code obfuscation.

As such, it is not recommended for development because it makes the code much less readable and much harder to edit.

Compressed code would have to be uncompressed first before execution. Minified code is still valid code in all respects and can be run immediately.

Minification happens anyway as part of the compilation process in most languages. By pre-minifying the files, you simply speed up the download and parsing process by a certain amount.

Title: What is the real difference between a minified and uncompressed file? What are the benefits of each?

Body: I often see something like "uncompressed for development / minified for production" when reading blogs or downloading frameworks. What is the real difference between the two, and if minified is "good for production" why is it not recommended for development?</p>


答:
I think that there is indeed a role for quality assurance in an agile organization, especially one that has to adhere to a quality management system such as ISO9001, ISO13485 for the medical device industry or AS9000 in the aerospace industry,  for the purposes of meeting customer requirements, regulations, or corporate mandates.

There are two important things to keep in mind. First, quality assurance is not just interested in the quality of the final product, but all intermediate work products. You can view software development as a black box that takes in some form of customer requirements and outputs software (and perhaps support for that software). However, during that process, the development team does things - transformations of those requirements into something usable to design and build software, creation of designs, prototypes and simulations, production code, test code, supporting scripts, manual procedures (for testing, deployment...), user documentation, and more - any of which can have defects which would impact downstream activities that rely on that information. Quality assurance is concerned with not only how defects are found in these work products, but how those defects are prevented. Second, quality assurance is also interested in process quality - how well we are doing the work. Although the obvious thing here is the quality of the work products, it's also how long it takes to do the work (among other things).

Two of the tenets of Agile are to favor "individuals and interactions over processes and tools" and to favor "working software over comprehensive documentation". However, neither of these say that you shouldn't have a process or that you don't need any kind of documentation. If you're trying to implement a quality management system, it's likely that you are required to have some level of documentation that can be compared to the work that is actually done during auditing. I think that these Agile ideals are better expressed in a few of the concepts of lean software development - eliminate waste by removing delays in the process, reducing bureaucracy, and improving internal communication, amplify learning, and empowering the team to make decisions about how to best achieve the goals.

In addition to being responsible for internal auditing, a QA organization can also help support agility or lean software development by being responsible for helping to detect waste, find delays, and ensure that what is documented gives each development team sufficient leeway to implement process requirements (levied by customers, regulatory agencies, corporate mandates, etc.) in the best way for the individual project but still having some level of consistency across the organization to allow for people to transition between projects without having to relearn an entire new set of processes and methods on top of the technical learning curve.

However, based on some of your comments, it seems like the direction that this is going in is not Agile or lean. It's a fine line to walk on how detailed to make your process documentation. If it's too vague and gives each project team room to implement anything that they wanted to, leading to wide variations in process implementations (and therefore process quality and likely product quality) across the organization. If it's too specific and detailed, it's likely to add bureaucracy and comprehensive documentation would get in the way of interactions and working software. There needs to be balance between agility and the ability to obtain information about process quality (through measurements and some level of consistency).

See also:

Can Agile and ISO 9001 interact well?

How do you validate need for documentation?

Title: Role of Quality Assurance (Not testing!) in Agile

Body:
From Wikipedia:

Quality Assurance refers to administrative and procedural activities
implemented in a quality system so that requirements and goals for a
product, service or activity will be fulfilled.[1] It is the
systematic measurement, comparison with a standard, monitoring of
processes and an associated feedback loop that confers error
prevention.[2] This can be contrasted with quality control, which is
focused on process output.

You can read the following article to understand the difference between QA and testing (quality control): Link

What I'd like to understand is whether there is a place for QA engineer who is not a tester in Agile/Scrum. Agile is deliberately short on processes and documentation (its even in the manifesto), and that's what QA specialize in. So should there even be a QA engineer in Agile?

I myself am not a QA engineer (I am a test lead), but I have a friend who is, so that's why I am asking.


答:
Minification, as the name suggests, aims at creating the smallest (as number of symbols) possible version of a program that is still in the form of a source code. Minification is not only an obfuscation, but it usually "minifies" parsing (and as a whole - compilation) time: https://stackoverflow.com/a/1181459/880114.

Compression (specifically in the web, gzip is usually used) is simply archiving files in smaller size so requests for their transfer can take a lesser time. Still there is a decompression on the client side and this might not always be the way to go.


答:
It comes down to cost/benefit.

Except for trivial programs, it is usually not physically possible to test all the permutations of possible inputs for program correctness.  (If you could, you could replace your code with a simple look up table).

Since you can't test everything, you (or management) need to prioritize.  I'd probably do something like:

priority = odds of hitting it * the pain you'll see if the failure
  occurs

"happy path" - 80% of your customers use these features every day.  A failure here will be noticed by a lot of people, very quickly.  High
  chance of hitting it, not sure about the pain (depends on the
  feature).

Catastrophic failures - low chance of hitting it, but extreme pain if hit

odd ball features - low chance of hitting it, and hopefully low pain if hit.

Your "1% feature" could fall anywhere in the priority scheme.  If this is your biggest customer and they represent 1/2 the income of the company, their 1% bug might have a higher priority than all 500 other customers combined.

Title: Testing non-central features


问:
What's the most absurd myth about programming issues?
To put it another way... What is the most commonly held and frustrating misunderstanding about programming, you have encountered?

Which widespread and longstanding myths/misconceptions do you find hard for programmers to dispel/correct.

Please, explain why this is a myth.

答:
Programming is just like assembly line work. You are working on a product for a certain time (maybe with coworkers) and finally you ship it. Just like building a house of bricks.

Contra: Programming contains a lot creativity and planning. It is art. Like the mason, also a programmer knows the difference between shaping a brick and planning a whole cathedral.

OwnerDisplayName: Josua Schmid


问:
How important is it to clean up someone else's code when faced with a tight deadline?
(I'm talking about HTML / CSS code (not programming languages) but I think we also face the same issue as with programmers.)

I am the senior front-end designer in a team and I often have to re-work my juniors' output in tight deadlines.

I am faced with 2 problems:

Their coding style is a bit of a mess.

The aesthetics are not good.

Their coding style, I find, is a mixed bag with no proper convention / standard. I am torn between cleaning up the code or just dealing with their code (even copying how they do things).

I do find it frustrating to follow their coding style as I feel I might learn bad habits. But then, that is the fastest way of meeting the deadline.

For those with much more experience, which is more effective? Should I save the clean-up for later? Or clean-up along the way as I make the changes?

(I don't want to sound arrogant though but such is the reality. It will take them more years to write better code. I know, I wrote messy code when I was starting.)

答:
In order to help curb the problem in the future, develop an internal Coding Standards and Practices document that all employees must follow.

For the current batch, clean up the code according to the S&P document as you refactor the code but only when you refactor.

OwnerDisplayName: anon

LastEditorDisplayName: anon


答:
The short answer is: no. When times are hard, sometimes you just have to put your head down and take the aesthetic bullet. ;)

A more pragmatic answer is to time-box it. Budget an hour to run through and clean up one specific aspect of the code. Then check it in and do some real work. But be honest with yourself about keeping it constrained.

Sometimes, though, a little bit of clean up makes the work go faster. Even some quick search-and-replace type changes make everything a lot more accessible.

Be wary of style wars. Especially in a tight-deadline situation, if you're going to undo some stylistic preferences that the other programmer will just re-do, then again you're better of waiting until you have time to really work out how you want to address those stylistic issues cooperatively. (Which means some give and take.)

But there's a judgment value in the answer. I would say "moderately" important. Clean code really can make the work go faster, and the code quality is, after all, part of the deliverable. I don't think I can touch code (even my own) without spending some time on cleanup. But make sure that fussing with style and format, and style wars, don't become more important than getting the code to production.

Title: How important is it to clean up someone else's code when faced with a tight deadline?

Body:
(I'm talking about HTML / CSS code (not programming languages) but I think we also face the same issue as with programmers.)

I am the senior front-end designer in a team and I often have to re-work my juniors' output in tight deadlines.

I am faced with 2 problems:

Their coding style is a bit of a mess.

The aesthetics are not good.

Their coding style, I find, is a mixed bag with no proper convention / standard. I am torn between cleaning up the code or just dealing with their code (even copying how they do things).

I do find it frustrating to follow their coding style as I feel I might learn bad habits. But then, that is the fastest way of meeting the deadline.

For those with much more experience, which is more effective? Should I save the clean-up for later? Or clean-up along the way as I make the changes?

(I don't want to sound arrogant though but such is the reality. It will take them more years to write better code. I know, I wrote messy code when I was starting.)


答:
I have said this before and will say it again "working code is more valuable than pretty code".

If you change code the chances are high that you will change its behavior, if this is tested code then you have just invalidated all the testing effort, and will need to repeat the tests.

By all means encourage your juniors to write clean understandable code, but if you are going to re-write everything they write then you are wasting your employers money several times over. They have to pay for your juniors, then pay for you to do what they have already paid your juniors to do, and then pay for you once more to do the job they actually hired you for.


答:
I'm fairly inexperienced with programming. As a student, however, I often commit to peer review and partnerships on projects. If there's ample time to finish a project, I'll go ahead and clean up a team member's code for clarity and readability. More often than not, I'll find it difficult to even sift through the first 100 lines or so. In these cases, I'm more than willing to extend a hand to assist with teaching a fellow programmer better habits and coding. If there just isn't enough time, I simply copy/paste, and work my projects into the big picture dealing with their poor interfaces. Afterwards, I'm sure to offer plenty of advice on coding technique. When it comes to peer review, constructive criticism (regardless of how unwelcome) only benefits both he/she and myself in the long run. If I collaborate with the same individuals in the future, I can rest assured knowing they had some informative guidelines pressed into their minds, and only hope that the next go round runs much smoother.

Overall, if you have the time to spare, take it to teach your newcomers how to conduct their work so that everyone is beneficial. Take a minute and teach them what has worked for you, and what hasn't. If you don't have the time, bare with their work for now and be sure to get back to them when you have the chance. Let them know that there are better ways of doing things, especially if you'll be working with them in the future.


答: Depends on the overall culture. If tight deadlines are sporadic, accept you will have to do cleanup later. If they are constant, than you are structurally building up technical debt and you should take up the problem with management. If they dont address your concerns, better start looking for other job opportunity as company culture would most likely meet with darwinian principles soon.</p>


问:
Is it a good practice to choose the highest warning level in C++ programming?
I used to select the default warning level in C++ programming. For example, in VS, the default warning level is Level3 (/W3) and No (/WX-) (don't treat warnings as errors).

I am wondering is it a good practice to choose highest warning level in C++ programming, e.g. EnableAllWarnings (/Wall) with Yes (/WX) (treat warnings as errors)?

Any advice will be much appreciated.

答:
The compilers are usually very good in spotting mistakes, therefore I always enable all possible warnings and errors (-Wall and -Wextra for gcc), and turn warnings into errors. This way nothing gets through.

Also, I try to write as standard compliant code as possible (-pedantic for gcc), meaning I try to avoid compiler extensions.

Sometimes it is not possible, because of an external library. Then you are forced to put required settings to make it compile. Unfortunately this usually goes with lots of warnings.


答:
Generally speaking, yes, it is a good idea to raise warnings to the highest level possible.

You may get some warnings that are overly picky or that just don't worry you in which case you can explicitly pragma them away in specific locations. At least then you are making a specific and explicit decision to ignore the warning.

Warnings as errors is more contentious in my experience. Some people think it is very important to have turned on. I personally keep this turned off to allow a little more flexibility while I am writing code, but then I trust myself not to ignore warnings, and certainly never commit code containing warnings. I have in the past however turned this on to catch programmers who were not quite as conscientious as they should have been.

Title: Is it a good practice to choose the highest warning level in C++ programming?

Body:
I used to select the default warning level in C++ programming. For example, in VS, the default warning level is Level3 (/W3) and No (/WX-) (don't treat warnings as errors).

I am wondering is it a good practice to choose highest warning level in C++ programming, e.g. EnableAllWarnings (/Wall) with Yes (/WX) (treat warnings as errors)?

Any advice will be much appreciated.


答:
I believe you are looking at the problem the wrong way - you are missing a great opportunity of teaching the juniors how to write better code.

If you habitually re-write their code, you might give your juniors the impression that you don't value their work, which will lower their morale, and not help them code better the next time.

A better approach, I believe, is to add to your team's development process a code-review task. It doesn't have to be about every piece of committed code, and it doesn't (I would argue that it shouldn't) have to be conducted only by you - whenever a member of your team finishes a big enough task he should pair with one (or more) of his team-mates, explain the code to them, and receive constructive opinion and criticism about his design, coding-style, possible bugs and security issues, etc.

When the code-reviewing team-mate is you they will learn from your expertise much more then when you simply re-write their code (they get a chance to hear the reason the code should be changed), and might take less offense.

Giving them a chance to also conduct code-reviews will further enhance their abilities - seeing how other people write code and why - and will raise their self-esteem.

They will also learn a lot if you give them a chance to review your code. You might learn something too - so don't do it just for show!


答:
I would be interested to know at what point in your process you are finding this problem?

Strictly speaking, in this magical ideal world which none of us inhabit, all code promoted or deployed should be perfect. It isn't so sometimes you have to be pragmatic.

However, if you have a code review process, it should be highlighting this before testing. If you're constantly up against deadlines, are the problems the estimations for delivery meaning that a key component of any development process - ie - code review - is getting strangled?

Your juniors are never going to learn to sit back and absorb better ways of doing things if you don't take the time to make it a part of their dev process to learn. It sounds to me like you are not doing that.


问:
Is there any issue if ternary operator is used in super() method
I am using a ternary operator in super () method of my code. While the code went for review, I got a reply that

Strictly don’t use ternary operators in the Super() calls.

Is there any problem if we use ternary operators in the super() method?

答:
I don't think the main problem is with the ternary operator.

My suggestion would be to overload the constructor like this:

```<code>public class TestClass: SuperClass {

    public TestClass() {
        this( "No Name" );
    }

    public TestClass(String name) {
        super( name );
    }

}
</code>```

Body:
I don't think the main problem is with the ternary operator.

My suggestion would be to overload the constructor like this:

public class TestClass: SuperClass {

    public TestClass() {
        this( "No Name" );
    }

    public TestClass(String name) {
        super( name );
    }

}


问:
How can a route finding program avoid obstacles/locations when calculating routes?
Do you delete nodes from the underlying graph representation? Or is there a more elegant way to do it?</p>

答:
Most route finding algorithms allow to assign arbitrary costs to edges (connection between nodes). The algorithm will then not select the shortest route (measured in edge-count) but the least costly route. In a road navigation application there isn't really a way around that, because large roads should usually be preferred over small roads which means that they would have a lower cost per km.

When you deem a certain road connection temporarily unsuitable (like a traffic jam), you can temporarily add an additional cost to that edge. The route finding algorithm will then not use that road unless there is no reasonable other path. To make it completely impossible to use a road (when it's blocked), you can either assign a cost of infinity to it or make the algorithm skip blocked edges altogether.


问:
Should I use the new C++11 'auto' feature, especially in loops?
What are the pros/cons to using the auto keyword, especially in for loops?

```<code>for(std::vector<T>::iterator it = x.begin(); it != x.end(); it++ )
{
   it->something();
}

for(std::map<T>::iterator it = x.begin(); it != x.end(); it++ )
{
   it->second->something();
}

for(auto it = x.begin(); it != x.end(); it++ )
{
   it->??
}
</code>```

Seems like if you don't know whether you have an iterator for a map or a vector you wouldn't know whether to use first or second or just directly access properties of the object, no?

This reminds me of the C# debate on whether to use the keyword var.  The impression I'm getting so far is that in the C++ world people are ready to adopt the auto keyword with less of a fight than var in the C# world.   For me my first instinct is that I like to know the type of the variable so I can know what operations I can expect to perform on it.

答:
PRO

Your code :

```<code>for(std::vector<T>::iterator it = x.begin(); it != x.end(); i++)
</code>```

is not going to compile, because of the template dependent name.

This is the correct syntax :

```<code>for( typename std::vector<T>::iterator it = x.begin(); it != x.end(); i++)
</code>```

Now look how long the type declaration is. This tells why the auto keyword was introduced. This :

```<code>for( auto it = x.begin(); it != x.end(); i++)
</code>```

is more concise. So, this is a pro.

CON

You have to be a bit careful. With the keyword auto, you get the type you declared.

For example :

```<code>std::vector< int > v{ 1, 2, 3, 4 };
for ( auto it : v )
{
  ++ it;   // ops modifying copies of vector's elements
}
</code>```

vs

```<code>std::vector< int > v{ 1, 2, 3, 4 };
for ( auto & it : v )   // mind the reference
{
  ++ it;   // ok, vector's elements modified
}
</code>```

To conclude : yes, you should it, but not overuse it. Some people tend to use it too much, and put auto everywhere, like in next example :

```<code>auto i = 0;
</code>```

vs

```<code>int i = 0;
</code>```

Body:
PRO

Your code :

for(std::vector<T>::iterator it = x.begin(); it != x.end(); i++)

is not going to compile, because of the template dependent name.

This is the correct syntax :

for( typename std::vector<T>::iterator it = x.begin(); it != x.end(); i++)

Now look how long the type declaration is. This tells why the auto keyword was introduced. This :

for( auto it = x.begin(); it != x.end(); i++)

is more concise. So, this is a pro.

CON

You have to be a bit careful. With the keyword auto, you get the type you declared.

For example :

std::vector< int > v{ 1, 2, 3, 4 };
for ( auto it : v )
{
  ++ it;   // ops modifying copies of vector's elements
}

vs

std::vector< int > v{ 1, 2, 3, 4 };
for ( auto & it : v )   // mind the reference
{
  ++ it;   // ok, vector's elements modified
}

To conclude : yes, you should it, but not overuse it. Some people tend to use it too much, and put auto everywhere, like in next example :

auto i = 0;

vs

int i = 0;


答:
When fixing code, and having deadline, I use normally two rules:

The code is awful but it's possible to find an issue in reasonable
  time and fix it

I fix a problem and leave the rest intact.

The code is so messy that it's really hard to find an issue there.
  Fixing something causes breaks immediately something else. It would be
  probably faster to write that code from scratch than fixing it.

Then I have no other choice than rewrite/refactor until the code will be clean enough to localize and fix the bug.

The borderline case is:

The code is messy and really bad. It's still possible to fix a bug in
  reasonable time, but the code structure will make it really hard to
  maintain. Any new feature is very likely to introduce new bugs or
  cause significant performance decrease.

In that case, the code is to be fixed, but only when new features are to be implemented, on during the idle time, never in bug-fixing time in face of deadline!


答:
I suggest using a review tool. If you have a Git based repository you can use the Gerrit review tool. After a few rejected commits, the team will learn the standards you want to follow and future commits will not require any additional work from you.

Commits will wait for your acceptance. If you see any lines which should be rewritten, you can write comments and your teammates can fix the code on their own based on your requirements. It's really a good way to learn team members coding standards.