问:
Is it okay to fake part of the class under test?
Suppose I have a class (forgive the contrived example and the bad design of it):

```<code>class MyProfit
{
  public decimal GetNewYorkRevenue();
  public decimal GetNewYorkExpenses();
  public decimal GetNewYorkProfit();

  public decimal GetMiamiRevenue();
  public decimal GetMiamiExpenses();
  public decimal GetMiamiProfit();

  public bool BothCitiesProfitable();

}
</code>```

(Note the GetxxxRevenue() and GetxxxExpenses() methods have dependencies which are stubbed out)

Now I'm unit testing BothCitiesProfitable() which depends on GetNewYorkProfit() and GetMiamiProfit().  Is it okay to stub GetNewYorkProfit() and GetMiamiProfit()?

It seems like if I don't then I'm simultaneously testing GetNewYorkProfit() and GetMiamiProfit() along with BothCitiesProfitable(). I'd have to make sure I setup the stubbing for GetxxxRevenue() and GetxxxExpenses() so that the GetxxxProfit() methods return the correct values.

So far I've only seen example of stubbing dependencies on external classes not internal methods.

And if it is okay, is there a particular pattern I should use to do this?

UPDATE

I'm concerned that we might be missing the core issue and that is probably the fault of my poor example.  The fundamental question is: if a method in a class has a dependency on another publicly exposed method in that same class is it okay (or even recommended) to stub that other method out?

Maybe I am missing something, but I'm not sure splitting up the class always makes sense.  Perhaps another minimally better example would be:

```<code>class Person
{
 public string FirstName()
 public string LastName()
 public string FullName()
}
</code>```

where full name is defined as:

```<code>public string FullName()
{
  return FirstName() + " " + LastName();
}
</code>```

Is it okay to stub FirstName() and LastName() when testing FullName()?

答:
You should break up the class under question.

Each class should do some simple task. If you task is too complicated to test, then the task the class does is too big.

Ignoring the goofiness of this design:

```<code>class NewYork
{
    decimal GetRevenue();
    decimal GetExpenses();
    decimal GetProfit();
}

class Miami
{
    decimal GetRevenue();
    decimal GetExpenses();
    decimal GetProfit();
}

class MyProfit
{
     MyProfit(NewYork new_york, Miami miami);
     boolean bothProfitable();
}
</code>```

UPDATE

The problem with stubbing methods in a class is that you are violating the encapsulation. Your test should be checking to see whether or not the external behaviour of the object matches the specifications. Whatever happens inside the object is none of its business.

The fact that FullName uses FirstName and LastName is an implementation detail. Nothing outside of the class should care that that is true. By mocking the public methods in order to test the object, you are making an assumption about that object is implemented.

At some point in the future, that assumption may cease to be correct. Perhaps all of the name logic will be relocated to a Name object which Person simply calls. Perhaps FullName will directly access member variables first_name and last_name rather then calling FirstName and LastName.

The second question is why you feel the need to do so. After all your person class could be tested something like:

```<code>Person person = new Person("John", "Doe");
Test.AssertEquals(person.FullName(), "John Doe");
</code>```

You shouldn't feel the need stub anything for this example. If you do then you are stub-happy and well... stop it!  There is no benefit to mocking the methods there because you've got control over what is in them anyways.

The only case where it would seem to make sense for the methods FullName uses to be mocked is if somehow FirstName() and LastName() were non-trivial operations. Maybe you are writing one of those random name generators, or FirstName and LastName query the database for answer, or something. But if that's what happening it suggest that object is doing something which doesn't belong in the Person class.

Putting it another way, mocking the methods is taking the object and breaking into two pieces. One piece is being mocked while the other piece is being tested. What you are doing is essentially an ad-hoc breaking up of the object. If that's the case, just break up the object already.

If your class is simple, you shouldn't feel the need to mock out pieces of it during a test. If your class is complex enough that you feel the need to mock, then you should break the class up into simpler pieces.

UPDATE AGAIN

The way I see it, an object has external and internal behavior. External behavior includes returns values calls into other objects etc. Obviously, anything in that category should be tested. (otherwise what would you test?) But internal behavior shouldn't really be tested.

Now the internal behavior is tested, because it is what results in the external behavior. But I don't write tests directly on the internal behavior, only indirectly through the external behavior.

If I want to test something, I figure it should be moved so that it becomes external behavior. That's why I think if you want to mock something, you should split the object so that the thing you want to mock is now in the external behavior of the objects in question.

But, what difference does it make? If FirstName() and LastName() are members of another object does it really change the issue of FullName()? If we decide that it is neccessary to mock FirstName and LastName does is actually help for them to be on another object?

I think if you use your mocking approach, then you create a seam in the object. You have functions like FirstName() and LastName() which directly communicate with an external data source. You also have FullName() which does not. But since they are all in the same class that is not apparent. Some pieces aren't supposed to directly access the data source and other are. Your code will be clearer if just break out those two groups.

EDIT

Let's take a step back and ask: why do we mock objects when we test?

Make the tests run consistently (avoid accessing things which change from run to run)

Avoid accessing expensive resources (don't hit third party services, etc.)

Simplify the system under test

Make it easier to test all possible scenarios (i.e. things like simulating failure, etc)

Avoid depending on the details of other pieces of code so that changes in those other pieces of code won't break this test.

Now, I think reasons 1-4 don't apply to this scenario. Mocking the external source when testing fullname takes care of all of those reasons for mocking. The only piece not handled that is the simplicity, but it seems the object is simple enough that's not a concern.

I think your concern is reason number 5. The concern is that at some point in future changing the implementation of FirstName and LastName will break the test. In the future FirstName and LastName may get the names from a different location or source. But FullName will probably always be FirstName() + " " + LastName(). That's why you want to test FullName by mocking FirstName and LastName.

What you have then, is some subset of the person object which is more likely to change then the others. The rest of the object uses this subset. That subset currently fetches its data using one source, but may fetch that data in a completely different way at a later date. But to me it sounds like that subset is a distinct object trying to get out.

It seems to me that if you mock the object's method you are splitting the object up. But you are doing so in a an ad-hoc manner. Your code does not make it clear that there are two distinct pieces inside your Person object. So simply split that object in your actual code, so that it is clear from reading your code what is going on. Pick the actual split of the object that makes sense and don't try to split the object up differently for each test.

I suspect you may object to splitting up your object, but why?

EDIT

I was wrong.

You should split objects rather then introducing ad-hoc splits by mocking individual methods. However, I was overly focused on the one method of splitting objects. However, OO provides multiple methods of splitting an object.

What I'd propose:

```<code>class PersonBase
{
      abstract sring FirstName();
      abstract string LastName();

      string FullName()
      {
            return FirstName() + " " + LastName();
      }
 }

 class Person extends PersonBase
 {
      string FirstName();
      string LastName();
 }

 class FakePerson extends PersonBase
 {
      void setFirstName(string);
      void setLastName(string);
      string getFirstName();
      string getLastName();
 }
</code>```

Maybe that is what you were doing all along. But I don't think this method will have the problems I saw with mocking methods because we've clearly delineated which side each method is on. And by using inheritance, we avoid the awkwardness that would arise if we used an additional wrapper object.

This does introduce some complexity, and for only a couple of utility functions I'd probably just test them by mocking the underlying 3rd party source. Sure, they are at increased danger of breaking but its not worth rearranging. If you've got a complex enough object that you need to split it, then I think something like this is a good idea.

Body:
You should break up the class under question.

Each class should do some simple task. If you task is too complicated to test, then the task the class does is too big.

Ignoring the goofiness of this design:

class NewYork
{
    decimal GetRevenue();
    decimal GetExpenses();
    decimal GetProfit();
}

class Miami
{
    decimal GetRevenue();
    decimal GetExpenses();
    decimal GetProfit();
}

class MyProfit
{
     MyProfit(NewYork new_york, Miami miami);
     boolean bothProfitable();
}

UPDATE

The problem with stubbing methods in a class is that you are violating the encapsulation. Your test should be checking to see whether or not the external behaviour of the object matches the specifications. Whatever happens inside the object is none of its business.

The fact that FullName uses FirstName and LastName is an implementation detail. Nothing outside of the class should care that that is true. By mocking the public methods in order to test the object, you are making an assumption about that object is implemented.

At some point in the future, that assumption may cease to be correct. Perhaps all of the name logic will be relocated to a Name object which Person simply calls. Perhaps FullName will directly access member variables first_name and last_name rather then calling FirstName and LastName.

The second question is why you feel the need to do so. After all your person class could be tested something like:

Person person = new Person("John", "Doe");
Test.AssertEquals(person.FullName(), "John Doe");

You shouldn't feel the need stub anything for this example. If you do then you are stub-happy and well... stop it!  There is no benefit to mocking the methods there because you've got control over what is in them anyways.

The only case where it would seem to make sense for the methods FullName uses to be mocked is if somehow FirstName() and LastName() were non-trivial operations. Maybe you are writing one of those random name generators, or FirstName and LastName query the database for answer, or something. But if that's what happening it suggest that object is doing something which doesn't belong in the Person class.

Putting it another way, mocking the methods is taking the object and breaking into two pieces. One piece is being mocked while the other piece is being tested. What you are doing is essentially an ad-hoc breaking up of the object. If that's the case, just break up the object already.

If your class is simple, you shouldn't feel the need to mock out pieces of it during a test. If your class is complex enough that you feel the need to mock, then you should break the class up into simpler pieces.

UPDATE AGAIN

The way I see it, an object has external and internal behavior. External behavior includes returns values calls into other objects etc. Obviously, anything in that category should be tested. (otherwise what would you test?) But internal behavior shouldn't really be tested.

Now the internal behavior is tested, because it is what results in the external behavior. But I don't write tests directly on the internal behavior, only indirectly through the external behavior.

If I want to test something, I figure it should be moved so that it becomes external behavior. That's why I think if you want to mock something, you should split the object so that the thing you want to mock is now in the external behavior of the objects in question.

But, what difference does it make? If FirstName() and LastName() are members of another object does it really change the issue of FullName()? If we decide that it is neccessary to mock FirstName and LastName does is actually help for them to be on another object?

I think if you use your mocking approach, then you create a seam in the object. You have functions like FirstName() and LastName() which directly communicate with an external data source. You also have FullName() which does not. But since they are all in the same class that is not apparent. Some pieces aren't supposed to directly access the data source and other are. Your code will be clearer if just break out those two groups.

EDIT

Let's take a step back and ask: why do we mock objects when we test?

Make the tests run consistently (avoid accessing things which change from run to run)

Avoid accessing expensive resources (don't hit third party services, etc.)

Simplify the system under test

Make it easier to test all possible scenarios (i.e. things like simulating failure, etc)

Avoid depending on the details of other pieces of code so that changes in those other pieces of code won't break this test.

Now, I think reasons 1-4 don't apply to this scenario. Mocking the external source when testing fullname takes care of all of those reasons for mocking. The only piece not handled that is the simplicity, but it seems the object is simple enough that's not a concern.

I think your concern is reason number 5. The concern is that at some point in future changing the implementation of FirstName and LastName will break the test. In the future FirstName and LastName may get the names from a different location or source. But FullName will probably always be FirstName() + " " + LastName(). That's why you want to test FullName by mocking FirstName and LastName.

What you have then, is some subset of the person object which is more likely to change then the others. The rest of the object uses this subset. That subset currently fetches its data using one source, but may fetch that data in a completely different way at a later date. But to me it sounds like that subset is a distinct object trying to get out.

It seems to me that if you mock the object's method you are splitting the object up. But you are doing so in a an ad-hoc manner. Your code does not make it clear that there are two distinct pieces inside your Person object. So simply split that object in your actual code, so that it is clear from reading your code what is going on. Pick the actual split of the object that makes sense and don't try to split the object up differently for each test.

I suspect you may object to splitting up your object, but why?

EDIT

I was wrong.

You should split objects rather then introducing ad-hoc splits by mocking individual methods. However, I was overly focused on the one method of splitting objects. However, OO provides multiple methods of splitting an object.

What I'd propose:

class PersonBase
{
      abstract sring FirstName();
      abstract string LastName();

      string FullName()
      {
            return FirstName() + " " + LastName();
      }
 }

 class Person extends PersonBase
 {
      string FirstName();
      string LastName();
 }

 class FakePerson extends PersonBase
 {
      void setFirstName(string);
      void setLastName(string);
      string getFirstName();
      string getLastName();
 }

Maybe that is what you were doing all along. But I don't think this method will have the problems I saw with mocking methods because we've clearly delineated which side each method is on. And by using inheritance, we avoid the awkwardness that would arise if we used an additional wrapper object.

This does introduce some complexity, and for only a couple of utility functions I'd probably just test them by mocking the underlying 3rd party source. Sure, they are at increased danger of breaking but its not worth rearranging. If you've got a complex enough object that you need to split it, then I think something like this is a good idea.


答:
While I agree with Winston Ewert's answer, sometimes it's just not possible to break up a class, whether due to time constraints, the API already being used elsewhere, or what have you.

In a case like that, instead of mocking methods, I'd write my tests to cover the class incrementally. Test the getExpenses() and getRevenue() methods, then test the getProfit() methods, then test the shared methods. It's true that you'll have more than one test covering a particular method, but since you wrote passing tests to cover the methods individually, you can be sure that your outputs are reliable given a tested input.

ContentLicense: CC BY-SA 3.0

Title: Is it okay to fake part of the class under test?

Body:
Suppose I have a class (forgive the contrived example and the bad design of it):

```<code>class MyProfit
{
  public decimal GetNewYorkRevenue();
  public decimal GetNewYorkExpenses();
  public decimal GetNewYorkProfit();

  public decimal GetMiamiRevenue();
  public decimal GetMiamiExpenses();
  public decimal GetMiamiProfit();

  public bool BothCitiesProfitable();

}
</code>```

(Note the GetxxxRevenue() and GetxxxExpenses() methods have dependencies which are stubbed out)

Now I'm unit testing BothCitiesProfitable() which depends on GetNewYorkProfit() and GetMiamiProfit().  Is it okay to stub GetNewYorkProfit() and GetMiamiProfit()?

It seems like if I don't then I'm simultaneously testing GetNewYorkProfit() and GetMiamiProfit() along with BothCitiesProfitable(). I'd have to make sure I setup the stubbing for GetxxxRevenue() and GetxxxExpenses() so that the GetxxxProfit() methods return the correct values.

So far I've only seen example of stubbing dependencies on external classes not internal methods.

And if it is okay, is there a particular pattern I should use to do this?

UPDATE

I'm concerned that we might be missing the core issue and that is probably the fault of my poor example.  The fundamental question is: if a method in a class has a dependency on another publicly exposed method in that same class is it okay (or even recommended) to stub that other method out?

Maybe I am missing something, but I'm not sure splitting up the class always makes sense.  Perhaps another minimally better example would be:

```<code>class Person
{
 public string FirstName()
 public string LastName()
 public string FullName()
}
</code>```

where full name is defined as:

```<code>public string FullName()
{
  return FirstName() + " " + LastName();
}
</code>```

Is it okay to stub FirstName() and LastName() when testing FullName()?


问:
Could spending time on Programmers.SE or Stack Overflow be substitute of good programming books for a non-beginner?
Could spending time (and actively participating) on Programmers.SE and Stack Overflow help me improve my programming skills any close to what spending time on reading a book like Code Complete 2 (which would otherwise be next in my reading list) will help.

Ok, may be the answer to this question for someone who is beginning with programming might be a straight no, but I'd like to add that this question I'm asking in context when the person is familiar with programming languages but wants to improve his programming skills. I was reading this question on SO and also this book has been recommended by many others (including Jeff and Joel).

To be more specific, I'd also add that even though I do programming in C, Java, Python,etc but still I'm not happy with my coding skills and reading the review of CC2 I realized I still need to improve a lot.

So, basically I want to know what's the best way for me to improve programming skills -  spend more time on here/SO or continue with CC2 and may be come here as and when time permits.

答:
Critical Thought and Analysis is still required

In and of itself no.  The only way to make source of learning (book, blog, website, SO, etc) is by comprehension, analysis and then repeated incorporation.  What people write on forums like this are not gospel and should not be treated as such no matter how many up votes it has.  You need do your own analysis on the opinion rendered to figure out if it's:

A good idea

An idea applicable for your situation

The Best Way to Improve

Is often specific to the person.  Some people get better through reading and research.  Some people get better through play and experimentation.  Other can even get better through self reflection and analysis.  Try whatever appeals to you and see if it works better for you.


问:
What are the disadvantages of test-first programming?
It's all the rage nowadays. "Everyone" recommends it. That in and of itself makes me suspicious.

What are some disadvantages you have found when doing test-first (test-driven) development? I'm looking for personal experiences from knowledgeable practitioners--I can read the hypothetical musings of a hundred wannabes elsewhere on the internet.

I ask not because I am looking to hate TDD, but because it is my job to improve software development process, and the more we can learn about the problems people encounter, the better chance we have of improving the process.

答:
I started doing TDD in early August 2009 and convinced my whole company to switch to it in September/October 2009. Presently, the whole dev team is fully converted, and committing untested code to the repo is considered a Bad Thing and thrown upon. It has been working great for us, and I cannot imagine switching back to cowboy coding.

However, there are two problems that are pretty noticeable.

The test suite has to be maintained

When you are serious about TDD, you'll end up writing lots of tests. Moreover, it takes some time and experience to realize what is the right granularity of tests (overdoing is almost as bad as underdoing it). These tests are also code, and they are susceptible to bitrot. This means that you have to maintain them as everything else: update it when you upgrade libraries they depend on, refactor from time to time... When you make big changes in your code, a lot of tests will suddenly become out of date or even plain wrong. If you are lucky, you can simply delete them, but a lot of times you'll end up extracting the useful bits and adapting them to the new architecture.

Testing abstractions leak from time to time

We are using Django, which has a pretty great testing framework. However, sometimes it makes assumptions that are slightly at odds with reality. For example, some middleware may break the tests. Or, some tests make assumptions about a caching backend. Also, if you are using a "real" db (not SQLite3), then preparing the db for the tests will take a lot of time. Sure, you can (and should) use SQLite3 and an in-memory db for tests you do locally, but some code will just behave differently depending on the database you use. Setting up a continuous integration server that runs in a realistic setup is a must.

(Some people will tell you that you should mock all the stuff like the database, or your tests aren't "pure," but that's only ideology speaking. If you make errors in your mocking code (and believe me, you will), your testsuite will be worthless.)

This all said, the problems I described start being noticeable only when you are quite advanced with TDD... When you are just starting with TDD (or working on smaller projects) test refactoring won't be an issue.

ContentLicense: CC BY-SA 2.5


问:
Using third-party libraries - always use a wrapper?
Most projects I am involved with use several open-source components. As a general principle, is it a good idea always to avoid binding all components of the code to the third-party libraries and instead go via an encapsulating wrapper to avoid the pain of change?

As an example, most of our PHP projects directly use log4php as a logging framework, i.e. they instantiate via \Logger::getLogger(), they use ->info() or ->warn() methods, etc. In the future, however, a hypothetical logging framework may appear which is better in some way. As it stands, all the projects which closely couple to the log4php method signatures would have to change, in dozens of places, in order to fit the new signatures. This would obviously have a wide impact on the codebase and any change is a potential problem.

To future-proof new codebases from this kind of scenario, I often consider (and sometimes implement) a wrapper class to encapsulate the logging functionality and make it easier, though not foolproof, to alter the way in which logging works in future with minimal change; the code calls the wrapper, the wrapper passes the call to the logging framework du jour.

Bearing in mind that there are more complicated examples with other libraries, am I over-engineering or is this a wise precaution in most cases?

EDIT: More considerations - using dependency injection and test doubles practically requires that we abstract out most APIs anyway ("I want to check my code executes and updates its state, but not write a log comment/access a real database"). Isn't this a decider?

答:
I think that wrapping third-party libraries today in case something better comes along tomorrow is a very wasteful violation of YAGNI.  If you are repeatedly calling third-party code in a manner peculiar to your application, you will (should) refactor those calls into a wrapping class to eliminate the repetition.   Otherwise you are fully using the library API and any wrapper would look just like the library itself.

Now suppose a new library appears with superior performance or whatever.  In the first case, you just rewrite the wrapper for the new API.  No problem.

In the second case, you create a wrapper adapting the old interface to drive the new library.  A little more work, but no problem, and no more work than you would have done if you had written the wrapper earlier.


问:
What do we mean by "design" in software engineering?
I have been reading about different software methodologies. Every method has a design phase to some extent, more or less.

I am not clear on what we mean by designing. Does this mean writing your thought process on how you will code the feature? Or it is a way of saying what you have coded or will code.

The point where I am most confused is the real world, which is different than putting your thoughts in documents and saying it will work. Since coding details can change, I don't know what's the point of having a design before coding.

答:
In my experience, Design is one of the most important phases of software development, specially for big projects.

Wikipedia defines Software design as:

Software design is a process of problem solving and planning for a
  software solution. After the purpose and specifications of software
  are determined, software developers will design or employ designers to
  develop a plan for a solution. It includes low-level component and
  algorithm implementation issues as well as the architectural view.

I've seen running into so many problems even when the path appeared to be straightforward and clear enough in the beginning. Spending time on design saves your time by allowing you to foresee the problems that you might run into during the development.


答:
The intention of a software design is to fail cheap and fail early. When designing your implementation, you're forced to think things through early. It's always cheaper to find out you've made a mistake in a design phase than to realize that 80% of the work that you've done without a design has to be re-factored due to a design flaw.

Others with more experience than you will also have a chance to review your design prior to you spending time implementing your solution and point out any high level errors in your system.

It also gives those who step into your shoes in the future the ability to read something documented rather than try to figure out your code.

Of course, depending on the granularity of your design, some things may change, but the overall architecture stays the same roughly 90% of the time (with enough experience).

Title: What do we mean by "design" in software engineering?

Body:
I have been reading about different software methodologies. Every method has a design phase to some extent, more or less.

I am not clear on what we mean by designing. Does this mean writing your thought process on how you will code the feature? Or it is a way of saying what you have coded or will code.

The point where I am most confused is the real world, which is different than putting your thoughts in documents and saying it will work. Since coding details can change, I don't know what's the point of having a design before coding.


答:
To be reductionist, and to take it as easy as possible but practical, you can think of analysis, design, and implementation as a chain of actions.

Analysis is when you just think about the overall aspects of your project, without thinking about specific platform. For example, should you use web services, or not. Do you want to force your users to use their email instead of user name or not? How long the trial period should be? What payment gateways should you support? These are analysis questions, which have nothing to do with platform and programming language.

When you took your decisions, the next step is to design, or to choose a programming language, a platform (Linux, Windows, iOS), to choose design patterns, to think about the overall architecture of your project (like creating Data Access Layer, etc.). Design is not abstract from platform.

Then you give your design documents to developers, and they should turn those documents into real code.


答:
Software design is a stage in a software methodology that when executed results a clear definition of how the problem at hand may be best solved.
The design may go into different iterations till its complete.
The design may cover different aspects of the system such as Solution Architecture, Application Structure, Database Design, Integration techniques, etc.
The input of the design is the planning and analysis deliverable.
Software design is not only applied at the system in general but also to each individual component of the system.
Software design should be the step proceeding program coding.
Different methodologies place design activities in different phases and call them different names.

One meaning of software design can be found in (among lots of others)
http://en.wikipedia.org/wiki/Software_design

http://c2.com/cgi/wiki?WhatIsSoftwareDesign

I find this cartoon very nice:
http://www.cs.utexas.edu/users/dsb/CS378/


问:
Is IronScheme complete enough or stable enough to be worth learning?
IronScheme is mentioned on Wikipedia as a successor to a failed project called IronLisp, bringing Lisp to CLR and .NET, the way Clojure does for the JVM.

Does anyone have experience with this language? It looks fairly complete (99%) but I'm not sure how to judge whether it's worth my time to fiddle with getting it set up or not. By stable or complete, I mean using it for actual projects rather than just fiddling with tools and Project Euler style problems.

答:
Note: I don't have experience with IronScheme, so take my thoughts with a grain of salt (although it's directed more at using bleeding edge technology).

While I'll toy with new technology with trivial problems (personal tools, Project Euler, etc), I'm very wary of employing said technology in full blown projects. Why?

Community support: Who will you get assistance from when you hit a wall?

Unforeseen issues: If the technology hasn't been thoroughly battle tested, do I really want to dig into the library code to try and figure out a solution to a problem that may not have been encountered? If I wanted to be a part of the commit team, then perhaps, but 99% of the time I just want to get my project done and not have to worry about the plumbing.

Team commitment: What happens if whoever's heading up the project pulls the plug? My projects are then orphaned, unless I want to take over the project, to which I have the same issues as the previous point.

So, while learning new tech is always a good thing (the worst thing that can happen is that you learn how someone else would implement something - which is good), it can really bit you in the arse if you adopt it early for full projects.

ContentLicense: CC BY-SA 4.0


问:
How do I decide whether to take a job I can't add to my portfolio?
I am a programmer, but since I've barely been in the professional market my portfolio is rather bare right now. Still, I started to try and get my website up and running to help a little bit, but what I really need right now is to be able to expand my portfolio.

I have a guy who wants me to do a lot of work for him, and I'm willing to do it for a while at least until I can get better jobs. The real problem, though, is that he does not want me to tell anyone that I have been doing work for him. Naturally, since I'm both trying to earn money and expand my portfolio, this poses a problem.

Part of the reason for doing an app was to prove I can develop one, but if I can't tell anyone I wrote it, then developing the app seems pointless; I may be better off creating my own app and eating the loss in profit because I can get more work out of it, but of course, at the same time, I'm in desperate need of cash. I do have jobs lined up elsewhere, but I'll likely get paid faster through doing this specific job.

What factors should I be looking at when making a decision on whether or not I should take a job for money that I can't talk about?

答:
Draft a line/section for your resume that describes the work you are currently doing without giving any specific details about the employer. Something like "Developing iPhone apps", "Constructing and maintaining a retail web site using javascript", etc.

If you want to be on the safe side send it to your current employer for comments but do not allow him to stall or veto the description altogether.


问:
How to manage a Closed Source High-Risk Project?
I am currently planning to develop a J2EE website and wish to bring in 1 developer and 1 web designer to assist me. The project is a financial app within a niche market.

I plan to keep the source closed. However, I fear that my would-be employees could easily copy the codebase and use it or sell it to a third party. The app development will take 4-6 months, perhaps more, and I may bring in additional employees after the app goes live.

But how do I keep the source to myself. Are there techniques companies use to guard their source?

I foresee disabling USB drives and DVD writers on my development machines, but uploading data or attaching the code in email would still be possible.

My question is incomplete. But programmers who have been in my situation, please advice. How should I go about this? Building a team, maintaining code-secrecy,etc.

I am looking forward to sign a secrecy contract with the employees if needed too.
(Please add relevant tags)

Update

Thank you for all the answers. I certainly won't be disabling all USB ports and DVD writers now. But I think I should be logging activity(How exactly should I do that?) I am wary of scalpers who would join and then run off with the existing code. I haven't met any, but I have been advised to be wary of them. I would include a secrecy clause, but given this is a startup with almost no funding and in a highly competitive business niche with bigger players in the field, I doubt I would be able to detect or pursue any scalpers.

How do I hire people I trust, when I don't know them personally. Their resume will be helpful but otherwise trust will develop only with time.

But finally even if they do run away with the code, it is service that matters after the sale is made. So I am not really worried for the long term.

答:
You need to trust your developers.

Virtually all professional developers won't steal your source. It's understood that if you work for somebody else, it's the employer that owns the code that you write. Developers might copy code for reference purposes, but it's highly unlikely they will offer it for sale to anyone else. If they did offer it for sale to a new employer then the likely outcome is them being shown the door and possibly even arrested (as Bob Murphy points out in his comment). Getting caught isn't worth the risk.

More importantly, distrust breeds distrust. Disabling USB ports and DVD writers will engender a feeling of distrust which will, paradoxically, make it more likely that the developers will copy the code.

By all means add a secrecy clause to your contract, but it's probably unnecessary to highlight it as the most important part of the contract.


问:
How to pay more attention to detail as a developer?
Are there any resources for paying more attention to detail as a software developer?  (Especially edge cases, or small mistakes in code, details in the problem description, ramifications for certain changes to a large system)

Some thoughts so far:

- Books of some sort?

- Exercise of some sort (e.g. just solving math problems [a professor during undergrad mentioned math teaches attention to detail..although another said assembly language programming teaches detail too...])?

- Some type of method for decomposing problems/thinking to force attention to every detail?

- Some way of noting down details so as not to forget them later

Example of what I mean:

Someone said a good question to ask a prospective developer how many 7's between 0 and 100.  I did it quickly and thought of 10, forgetting about 70-76 and 78-79.  Basically it's just a lack of attention to detail....

Another example is during a white board coding interview, there was an easy problem and while the initial version was correct, as we kept making it more efficient I kept making more and more small mistakes.  Once pointed out it was easy for me to fix them, but it was embarrassing to have them pointed out by the interviewer instead of me finding the issues on my own and fixing them.

Another example is just compiling code.  Initially I would write code, go over it once before compiling and catch many errors.  Now I miss many more errors and the compiler (or interpreter) finds many more errors than it used to.

Also I noticed that when I first came out of undergrad it was much easier to hang onto tons of detail where as now even the detail that I initially knew I seem to forget more as time goes on.  Which is why in addition to paying attention to detail for new problems I could also use resources for keeping track of detail from older problems without having to rely on memory.

答:
It sounds to me like you are letting yourself get distracted and, as Caleb suggests, want to go too fast.

What has helped me from forgetting details is not to hop-skip-and-jump from one part of a bigger problem to another part of it. Finish one part at a time. Write "todo comments" in your code when you think of something that needs to be done elsewhere and isn't related directly to what you are doing now.

I now tend to write "todo's" even for the part I am working on at any time. It helps ensure that I remember to implement whatever crossed my mind and to keep the intrusion on my thought process of that little detail to an absolute minimum.


答:
Offer three different prices:

Product is publicly credited to you (in the credits or wherever)

Product is white label permitting the customer to use it without crediting you

Product is white label and you may not even tell anybody, that you created it

By working on a project, that doesn't advertise you as the author, you're effectively losing money and time (which comes down to money in business), that you need to invest into advertisement of your services and acquisition of new customers.

The same applies yet again, if you're not even allowed to use the product as a reference for your skills and expertise.

Decide, what difference that makes to you and how that translates into money and then give your customer a choice.

Title: How do I decide whether to take a job I can't add to my portfolio?

Body:
I am a programmer, but since I've barely been in the professional market my portfolio is rather bare right now. Still, I started to try and get my website up and running to help a little bit, but what I really need right now is to be able to expand my portfolio.

I have a guy who wants me to do a lot of work for him, and I'm willing to do it for a while at least until I can get better jobs. The real problem, though, is that he does not want me to tell anyone that I have been doing work for him. Naturally, since I'm both trying to earn money and expand my portfolio, this poses a problem.

Part of the reason for doing an app was to prove I can develop one, but if I can't tell anyone I wrote it, then developing the app seems pointless; I may be better off creating my own app and eating the loss in profit because I can get more work out of it, but of course, at the same time, I'm in desperate need of cash. I do have jobs lined up elsewhere, but I'll likely get paid faster through doing this specific job.

What factors should I be looking at when making a decision on whether or not I should take a job for money that I can't talk about?


答:
The basic reason to write a wrapper around a third-party library is so that you can exchange that third-party library without changing the code that uses it.  You can't avoid coupling to something, so the argument goes that it is better to couple to an API you've written.

Whether this is worth the effort is a different story.  That debate will likely continue for a long time.

For small projects, where the likelihood that such a change will be necessary is low, it is probably unnecessary effort.  For larger projects, that flexibility may very well outweigh the extra effort to wrap the library.  However, it is difficult to know whether that is the case beforehand.

Another way to look at it is that basic principle of abstracting what is likely to change.  So, if the third-party library is well established and unlikely to be changed, it may be fine not to wrap it.  However, if the third-party library is relatively new there is a greater chance that it will need to be replaced.  That said, development of established libraries has been abandoned plenty of times.  So, this is not an easy question to answer.

Title: Using third-party libraries - always use a wrapper?

Body:
Most projects I am involved with use several open-source components. As a general principle, is it a good idea always to avoid binding all components of the code to the third-party libraries and instead go via an encapsulating wrapper to avoid the pain of change?

As an example, most of our PHP projects directly use log4php as a logging framework, i.e. they instantiate via \Logger::getLogger(), they use ->info() or ->warn() methods, etc. In the future, however, a hypothetical logging framework may appear which is better in some way. As it stands, all the projects which closely couple to the log4php method signatures would have to change, in dozens of places, in order to fit the new signatures. This would obviously have a wide impact on the codebase and any change is a potential problem.

To future-proof new codebases from this kind of scenario, I often consider (and sometimes implement) a wrapper class to encapsulate the logging functionality and make it easier, though not foolproof, to alter the way in which logging works in future with minimal change; the code calls the wrapper, the wrapper passes the call to the logging framework du jour.

Bearing in mind that there are more complicated examples with other libraries, am I over-engineering or is this a wise precaution in most cases?

EDIT: More considerations - using dependency injection and test doubles practically requires that we abstract out most APIs anyway ("I want to check my code executes and updates its state, but not write a log comment/access a real database"). Isn't this a decider?


答: Learning java became much, much easier when I found out about stackoverflow. I didnt ask questions, but I started to read the java questions daily. Much of the questions were out of my understanding, but every now and then someone asked the beginner question. Many people ask about concepts they read from a book but dont understand, and get much more understandable answer here, often with small code sample to further explain the problem.</p>

Title: Could spending time on Programmers.SE or Stack Overflow be substitute of good programming books for a non-beginner?

Body:
Could spending time (and actively participating) on Programmers.SE and Stack Overflow help me improve my programming skills any close to what spending time on reading a book like Code Complete 2 (which would otherwise be next in my reading list) will help.

Ok, may be the answer to this question for someone who is beginning with programming might be a straight no, but I'd like to add that this question I'm asking in context when the person is familiar with programming languages but wants to improve his programming skills. I was reading this question on SO and also this book has been recommended by many others (including Jeff and Joel).

To be more specific, I'd also add that even though I do programming in C, Java, Python,etc but still I'm not happy with my coding skills and reading the review of CC2 I realized I still need to improve a lot.

So, basically I want to know what's the best way for me to improve programming skills -  spend more time on here/SO or continue with CC2 and may be come here as and when time permits.


问:
Should "tags" be an nvarchar field in a table, or should it be another table altogether?
I am building a photography website and I want to allow the user to put tags on their photos for easy searching. But I'm not sure if I want the tags to be an nvarchar field in each picture and then search for that tag in each record or if I want to pull them out and create a separate table of tags and tagIds and then have a look up table, so I can just search the database for that ID.
So, what I'm asking is which would be better efficiency-wise and scalability-wise. The database will have over 3-4000 pictures.</p>

答:
Well, tags-content is actually an m-n-relationship. So normally (pun intended) you'd have one table containing the tags, one table containing the data (pictures for example) and one table for their relationship. Ideally you'll put an index on the tags in that table, because you're likely to search by tag more often.

To really scale out such models, you then might consider sharding. Or caching searches. Or maybe using a different database model optimized for that very use case. But I suppose 3-4000 pictures is not much data to worry about.


答:
I am the author of IronScheme. I am not really sure how to answer your question, but will try :)

IronScheme firstly tries to implement Scheme (R6RS specifically), with the secondary objective being CLR interoperability.

Compared to Clojure (focusing on the their bad points), IronScheme won't:

give you CLR runtime exceptions; IronScheme uses Scheme's exception handling

give you 'infinite' stacktraces; IronScheme is properly tail recursive

be hard to setup; just extract to directory and go

take long to start up; IronScheme (when ngen'd) only takes 0.1 seconds to start the REPL

be ambiguous; IronScheme implements a standardized specification

Unfortunately where Clojure wins is:

Documentation

Frameworks and libraries

User community

This is worrying for IronScheme, as the last 3 mentioned is very much a chicken-egg scenario. Personally, I tend to only create libraries when I need them, and with a very tiny user community, there is not much contribution from users besides bug reports. I would love a bigger user community.

As for support, I normally help users as fast I can. This evidence can be seen from my response times on the IronScheme discussion boards. Also, bugs are normally fixed as soon as they have been identified.

As for stability, the codebase is pretty mature, and currently only bug fixes and optimizations are the only code additions.

As for usability, if you are familiar with the .NET framework, you can do pretty much anything with IronScheme as you can with any other .NET language; it may be harder or easier depending on how much you are willing to abstract into more Scheme-like idioms. Things are very easy to write in IronScheme; for example my entire MVC framework is barely 400 lines of Scheme code, thanks to tapping into ASP.NET (I certainly do not like re-inventing the wheel).

Feel free to ask for clarifications if the answer is not enough. Demian does make good points in terms of maintainability too.

Regards

leppie

Title: Is IronScheme complete enough or stable enough to be worth learning?

Body:
IronScheme is mentioned on Wikipedia as a successor to a failed project called IronLisp, bringing Lisp to CLR and .NET, the way Clojure does for the JVM.

Does anyone have experience with this language? It looks fairly complete (99%) but I'm not sure how to judge whether it's worth my time to fiddle with getting it set up or not. By stable or complete, I mean using it for actual projects rather than just fiddling with tools and Project Euler style problems.


问:
How do you determine a 'release' date when a team uses Scrum
I'm new to scrum and using it on a project.  We do 3 week iterations, estimate at the beginning and then collect the points at the end.

We have a product back log (probably not complete).

If you do estimates of the entire back log at the beginning aren't those estimate just 'wild ass guesses'

So when do you estimate the back log and how do you determine a release date?

ep

答:
In theory each sprint should result in product increment which can be released. The question is what does release mean in this case? It definitely means that the released increment should be available for some group of users to use it and get feedback but it doesn't have to mean that you have to do a big production release every two or three weeks.

Example:

Suppose that you are building some larger application which should be available publicly. Product owner has some idea what is minimal feature set needed to make the application available publicly - without these features users will not like the application and it will hurt company reputation. So the first public release is based on availability  of these mandatory features. During development of this first public release team can still release every sprint but those incremental releases will be available only internally within company and company employees will use the application all the time during the development.

The public release date can be defined as well. Team's progress and velocity will show if the team is able to deliver necessary features to that release date early in development stage. It will allow product owner and management reacting on this soon enough if it will be obvious that requested feature set is much bigger than deliverable feature set.


答:
I've heard it said that no idea on its own is worth more than $20 (and that's Canadian dollars!) The idea only has value if it is executed well. Even if it comes to them stealing the code and trying to make a go of it themselves, odds are you have a better idea of what the next steps are, and more contacts with prospective buyers of the software.

You should definitely only hire people you trust, but even if they steal your code and try to sell it, they are unlikely to get very far.

Title: How to manage a Closed Source High-Risk Project?

Body:
I am currently planning to develop a J2EE website and wish to bring in 1 developer and 1 web designer to assist me. The project is a financial app within a niche market.

I plan to keep the source closed. However, I fear that my would-be employees could easily copy the codebase and use it or sell it to a third party. The app development will take 4-6 months, perhaps more, and I may bring in additional employees after the app goes live.

But how do I keep the source to myself. Are there techniques companies use to guard their source?

I foresee disabling USB drives and DVD writers on my development machines, but uploading data or attaching the code in email would still be possible.

My question is incomplete. But programmers who have been in my situation, please advice. How should I go about this? Building a team, maintaining code-secrecy,etc.

I am looking forward to sign a secrecy contract with the employees if needed too.
(Please add relevant tags)

Update

Thank you for all the answers. I certainly won't be disabling all USB ports and DVD writers now. But I think I should be logging activity(How exactly should I do that?) I am wary of scalpers who would join and then run off with the existing code. I haven't met any, but I have been advised to be wary of them. I would include a secrecy clause, but given this is a startup with almost no funding and in a highly competitive business niche with bigger players in the field, I doubt I would be able to detect or pursue any scalpers.

How do I hire people I trust, when I don't know them personally. Their resume will be helpful but otherwise trust will develop only with time.

But finally even if they do run away with the code, it is service that matters after the sale is made. So I am not really worried for the long term.


答:
Case 1:
A user may enter 1 tag value per picture and Pictures are stored in database or using the file system.

Suggested design for case 1:
Store tags in a database table and use the file name as an Foreign Key.

Case 2:
A user may enter 1 or more tag values per picture and Pictures are stored in database or using the file system.

Suggested design for case 2:
Store tags in a database table
Store file names of pictures in another table.
Create a 3rd table (an intersection table) with at least 2 columns (first column is a foreign key of the picture file name, second column is a foreign key for the tag value)

Alternatively,

If your database has full text search, you will need only 1 table as in case 1.

In all cases, its a good idea to not store pictures in the database if the number of pictures does not violate your server's OS capacities.

I can explain further if you like.

Title: Should "tags" be an nvarchar field in a table, or should it be another table altogether?

Body:
I am building a photography website and I want to allow the user to put tags on their photos for easy searching. But I'm not sure if I want the tags to be an nvarchar field in each picture and then search for that tag in each record or if I want to pull them out and create a separate table of tags and tagIds and then have a look up table, so I can just search the database for that ID.
So, what I'm asking is which would be better efficiency-wise and scalability-wise. The database will have over 3-4000 pictures.</p>


答:
Warning: the question being tagged sql-server, my answer applies to Microsoft SQL Server only, and is not relevant for other SQL engines.

There are three approaches for storing tags:

One table: all the tags of a single photo would be stored in a single nvarchar field,

Two tables: one for pictures, another one for tags, every tag referencing a picture,

Three tables: one for pictures, another one for tags, and a third one for links between two other tables.

1. nvarchar field approach vs. other approaches

Don't. Really, don't use nvarchar to store comma-separated (or something-else-separated) tags of a single picture. Databases are done to structure your data, to store data in a logical way. By establishing a convention that several pieces of changeable information will be stored in a single field, you break this logic¹. To be a bit extreme, you can also store all your application data in a flat nvarchar(max), but the fact that you can doesn't mean that you must do it, since instead of using an existent database, you're creating your own, without bringing anything that the existent database does not provide.

Reason 1: complicated queries

If you consider adding features to your application later, storing all tags of a single photo in a single nvarchar is not extensible, and will hurt soon or later. Consider two scenarios:

You want to display on your home page the list of tags, ordered by the number of occurrences (so for example if 457 images are tagged sky and 432 images are tagged river, sky will appear before river). Can you do that easily? Can you do that with just one simple query?

You want simply to search for pictures containing a tag `river`. Let's say tags are separated by spaces.

```<code>select [PictureId] from [Picture] where [Tags] like '%river%'
</code>```

will give you the pictures with rivers, but also all the photos of Riverside, CA, United States. How sad!

```<code>select [PictureId] from [Picture] where [Tags] like '% river %'
</code>```

will give you some pictures with rivers, but some will be missing.

```<code>select [PictureId]
from [Picture]
where [Tags] like '% river %'
    or [Tags] like 'river %'
    or [Tags] like '% river'
</code>```

gives you the expected set, but now the query, which is expected to do something easy, really sucks.

Reason 2: blocking scenarios

Some scenarios will become impossible or nearly impossible. For example, what if you want to know which tag was assigned by which user? This is a reasonable requirement; after all, if the same user is assigning intentionally wrong tags to photos, you would certainly want to block this user.

Reason 3: waste of space

Not only do you have to write complicated queries, but you are also wasting the disk space by storing the same tags again and again.

Microsoft SQL Server is good at optimizing duplicate rows (see below), but this applies only to the same field. If one field is sky nature mountains and the other is river mountains nature, "nature" and "mountains" will waste 30 bytes instead of 15. Even worse, if one photo is tagged animal cat and another one, cat animal, Microsoft SQL Server will consider those fields different.

2. three-tables vs. two-tables approach

Some people may suggest a three table approach: one for pictures, another for tags and a third one which links tags to pictures.

It is a perfectly valid generic solution, and it's how teachers will tell you to do in college. The fact is, it's a bad solution in most cases when it comes to Microsoft SQL Server.

Microsoft SQL Server supports prefix and dictionary compression, which means that you can store the same value several times, without wasting more disk space. This can simplify a lot the three tables design without loosing neither the flexibility (which is lost in the one table scenario), nor the optimal disk space used by the data.

With two tables, most queries are easier and shorter to write. For example consider listing the first one thousand pictures with their respective tags. With three tables, you have to do two joins. With two tables, one join is enough.

Consider now a very simple scenario: you're displaying the picture with the list of tags. With the first approach, you have to search the TagPhoto table, then Tag table to get the exact tags. With the second one, you get them through a single select query with no joins at all (as noted by Catcall below, this does not apply if you use natural keys instead of surrogate keys).

Performance-wise, two tables approach was always a success for me when I profiled my applications on my hardware. This means what it means, and maybe in your case things will be completely different. In all cases, Microsoft SQL Server row compression is mature enough to be used, and was done especially for cases like that.

¹ In other words, a tag is a modifiable and "referenceable" entity: you can add a single tag, remove it, change it, affect it to another picture, etc. Since it is an entity like this, you have to store it in its own field in a database.

You can note that there are other entities which are stored combined in a same field. For example a comment of a user on a blog can be represented as a set of paragraphs, every paragraph being an entity. This is not the same thing. Paragraphs are not modifiable as separate entities: the user modifies the whole comment, not the paragraphs. Paragraphs are neither "referenceable", or at least they don't have to be: all paragraphs of a message must be displayed every time; all paragraphs of a message belong to the same user, have the same submission or last edit date, etc.

Body:
Warning: the question being tagged sql-server, my answer applies to Microsoft SQL Server only, and is not relevant for other SQL engines.

There are three approaches for storing tags:

One table: all the tags of a single photo would be stored in a single nvarchar field,

Two tables: one for pictures, another one for tags, every tag referencing a picture,

Three tables: one for pictures, another one for tags, and a third one for links between two other tables.

1. nvarchar field approach vs. other approaches

Don't. Really, don't use nvarchar to store comma-separated (or something-else-separated) tags of a single picture. Databases are done to structure your data, to store data in a logical way. By establishing a convention that several pieces of changeable information will be stored in a single field, you break this logic¹. To be a bit extreme, you can also store all your application data in a flat nvarchar(max), but the fact that you can doesn't mean that you must do it, since instead of using an existent database, you're creating your own, without bringing anything that the existent database does not provide.

Reason 1: complicated queries

If you consider adding features to your application later, storing all tags of a single photo in a single nvarchar is not extensible, and will hurt soon or later. Consider two scenarios:

You want to display on your home page the list of tags, ordered by the number of occurrences (so for example if 457 images are tagged sky and 432 images are tagged river, sky will appear before river). Can you do that easily? Can you do that with just one simple query?

You want simply to search for pictures containing a tag `river`. Let's say tags are separated by spaces.

select [PictureId] from [Picture] where [Tags] like '%river%'

will give you the pictures with rivers, but also all the photos of Riverside, CA, United States. How sad!

select [PictureId] from [Picture] where [Tags] like '% river %'

will give you some pictures with rivers, but some will be missing.

select [PictureId]
from [Picture]
where [Tags] like '% river %'
    or [Tags] like 'river %'
    or [Tags] like '% river'

gives you the expected set, but now the query, which is expected to do something easy, really sucks.

Reason 2: blocking scenarios

Some scenarios will become impossible or nearly impossible. For example, what if you want to know which tag was assigned by which user? This is a reasonable requirement; after all, if the same user is assigning intentionally wrong tags to photos, you would certainly want to block this user.

Reason 3: waste of space

Not only do you have to write complicated queries, but you are also wasting the disk space by storing the same tags again and again.

Microsoft SQL Server is good at optimizing duplicate rows (see below), but this applies only to the same field. If one field is sky nature mountains and the other is river mountains nature, "nature" and "mountains" will waste 30 bytes instead of 15. Even worse, if one photo is tagged animal cat and another one, cat animal, Microsoft SQL Server will consider those fields different.

2. three-tables vs. two-tables approach

Some people may suggest a three table approach: one for pictures, another for tags and a third one which links tags to pictures.

It is a perfectly valid generic solution, and it's how teachers will tell you to do in college. The fact is, it's a bad solution in most cases when it comes to Microsoft SQL Server.

Microsoft SQL Server supports prefix and dictionary compression, which means that you can store the same value several times, without wasting more disk space. This can simplify a lot the three tables design without loosing neither the flexibility (which is lost in the one table scenario), nor the optimal disk space used by the data.

With two tables, most queries are easier and shorter to write. For example consider listing the first one thousand pictures with their respective tags. With three tables, you have to do two joins. With two tables, one join is enough.

Consider now a very simple scenario: you're displaying the picture with the list of tags. With the first approach, you have to search the TagPhoto table, then Tag table to get the exact tags. With the second one, you get them through a single select query with no joins at all (as noted by Catcall below, this does not apply if you use natural keys instead of surrogate keys).

Performance-wise, two tables approach was always a success for me when I profiled my applications on my hardware. This means what it means, and maybe in your case things will be completely different. In all cases, Microsoft SQL Server row compression is mature enough to be used, and was done especially for cases like that.

¹ In other words, a tag is a modifiable and "referenceable" entity: you can add a single tag, remove it, change it, affect it to another picture, etc. Since it is an entity like this, you have to store it in its own field in a database.

You can note that there are other entities which are stored combined in a same field. For example a comment of a user on a blog can be represented as a set of paragraphs, every paragraph being an entity. This is not the same thing. Paragraphs are not modifiable as separate entities: the user modifies the whole comment, not the paragraphs. Paragraphs are neither "referenceable", or at least they don't have to be: all paragraphs of a message must be displayed every time; all paragraphs of a message belong to the same user, have the same submission or last edit date, etc.


问:
How to most effectively debug code?
Bugs creeping into code can be minimized, but not entirely eliminated as it is written - programmers are, although many would disagree, only humans.

When we do detect an error in our code, what can we do to weed it out? How should we approach it to make most effective use of our valuable time and enable us to spend less time trying to find it and more time coding? Also, what should we avoid when debugging?

Note here that we're not talking about preventing bugs; we're talking about what to do when bugs do appear. This is a wide field, I know, and may be highly dependent on language, platform and tools. If so, keep to encompassing answers such as mindsets and general methods.

答:
Write tests. Testing is not only great at preventing bugs (in my experience, TDD done right eliminates almost all trivial, stupid bugs), but also helps a lot in debugging. Testing forces your design to be rather modular, which makes isolating and replicating the problem a lot easier. Also, you control the environment, so there will be a lot less surprises. Moreover, once you get a failing test case, you can be reasonably sure that you've nailed the real reason of the behavior that is bothering you.

Learn how to use a debugger. print statements may work reasonably well at some level, but a debugger most of the time is very helpful (and once you know how to use it, it is a lot more comfortable than print statements).

Talk about someone about your problem, even if it's just a rubber duckie. Forcing yourself to express the problem you are working on in words really does miracles.

Give yourself a time limit. If for example after 45 minutes you feel you are going nowhere, just switch to other tasks for some time. When you get back to your bug, you'll hopefully be able to see other possible solutions that you wouldn't have have considered before.

Title: How to most effectively debug code?


答:
Two things that SO and P.SE offer that neither reading other books nor writing code yourself offer: testing and teaching. If you just blindly ask questions like

Why doesn't this compile

```<code>wall
of
code
100
lines
long
</code>```

And someone says "on line 33 that comma should be a semicolon" and you accept the answer and carry on, and continue to ask the same questions forever, then you might not improve.

BUT, if you're reading other people's questions, thinking about the answer, watching to see what answers come in and how they compare to what you know, that will really improve you. If you type some code into your favourite editor and try a few things to see what the answer is, that will really improve you. And when you start trying to explain concepts (and people are waiting there to "clarify" what you say) that will also improve you.

For about 30 years now I have discovered I never learn a topic so well as when, believing I know it, I set out to teach it. To me that is the major contribution of the *.se sites to my skills as a developer. Plus, when I just want to know a simple fact, it's my number one place to look (or occasionally ask).

Body:
Two things that SO and P.SE offer that neither reading other books nor writing code yourself offer: testing and teaching. If you just blindly ask questions like

Why doesn't this compile

wall
of
code
100
lines
long

And someone says "on line 33 that comma should be a semicolon" and you accept the answer and carry on, and continue to ask the same questions forever, then you might not improve.

BUT, if you're reading other people's questions, thinking about the answer, watching to see what answers come in and how they compare to what you know, that will really improve you. If you type some code into your favourite editor and try a few things to see what the answer is, that will really improve you. And when you start trying to explain concepts (and people are waiting there to "clarify" what you say) that will also improve you.

For about 30 years now I have discovered I never learn a topic so well as when, believing I know it, I set out to teach it. To me that is the major contribution of the *.se sites to my skills as a developer. Plus, when I just want to know a simple fact, it's my number one place to look (or occasionally ask).


问:
Licensing for a commercial Blackberry app
I want to develop an app for a website (let's say stackoverflow.com in this case).

Will I violate the Blackberry terms of service if I list an (paid) app for a website which I do not own? What about the website I'm going to use?

答:
If you're actually developing an SO app and not just using it as an example, you can use the content so long as you attribute it to the original creator. All Stack Overflow content is licensed under CC-BY-SA.

This doesn't apply in general. Some sites will have similar licensing for their content, some will be entirely proprietary. You should always check with the site to find out the appropriate licensing or to work out a business arrangement for content access.

I can't find the legal terms for developers of Blackberry applications, but I think it's safe to assume you would not be allowed to publish an application to the Blackberry App World without having appropriate rights to use another site's content. I'm not sure what would happen if you just distributed the application yourself.

And as with all legal matters... if in doubt, talk to a lawyer.


问:
Test-driven development and improving white box testing skills
I am an entry level Java Programmer straight out of school.
I have good knowledge and experience with J2SE.
Can anyone advise me on how to improve or tune my skills towards being a Java white box tester? Wide range of inputs are welcome.

And what is Test Driven Development?

答:
The question specifically makes reference to "white box testing". This is where your tests have intimate knowledge of the internal structure of your code and assert behaviour at each step rather than just the input/output/side effect(black box testing). Whilst JUnit is excellent to do both, you need further additional frameworks in order to do this within the context of a unit test.

EasyMock and JMock are good frameworks to do this. I tend to favour JMock.

At the risk of starting a OT debate, you should think carefully about the implications of white box testing. White box tests are intimately tied to your code (obviously), and if not used carefully, mocking frameworks can cause your tests to be rather complicated, difficult to read and tend to be more brittle when refactoring.

I tend to stick to a mix of both. Black box tests wherever possible, and white box tests sparingly applied to riskier/more complicated code.

Of course the frameworks listed above can also be used in black box tests where the number of contributing (injected) classes is large and simple stubbing becomes unwieldly.

Regarding TDD - it's primarily a design enhancing approach to writing code, rather than simply a way of writing tests. The tests you have at the end are an important output, but moreover the approach is intended to enhance the design and stucture of your application.


答:
I'm dealing with this exact issue on a project I'm currently working on. But in my case the library is for graphics and thus I am able to restrict it's use to a small number of classes that deal with graphics, versus sprinkling it throughout the entire project. Thus it's pretty easy to switch APIs later if I need to; in the case of a logger the matter becomes a lot more complicated.

Thus I would say the decision has a lot to do with what exactly the 3rd-party library is doing and how much pain would be associated with changing it. If changing all the API calls would be easy regardless then it's probably not worth doing. If however changing the library later would be really hard then I would probably wrap it now.

Beyond that, other answers have covered the main question very well so I just want to focus on that last addition, about dependency injection and mock objects. It depends of course on how exactly your logging framework works, but in most cases no that wouldn't require a wrapper (although it will probably benefit from one). Just make the API for your mock object exactly the same as the 3rd-party library and then you can easily swap in the mock object for testing.

The main factor here is whether or not the 3rd-party library is even implemented through dependency injection (or a service locator or some such loosely coupled pattern). If the library functions are accessed through a singleton or static methods or something then you will need to wrap that in an object that you can work with in dependency injection.


问:
When should I be offloading work to a GPU instead of the CPU?
Newer systems such as OpenCL are being made so that we can run more and more code on our graphics processors, which makes sense, because we should be able to utilise as much of the power in our systems as possible.

However, with all of these new systems, it seems as if GPUs are better than CPUs in every way.  Because GPUs can do parallel calculation, multi-core GPUs actually seem like they'd be much better than multi-core CPUs; you'd be able to do many calculations at once and really improve speed.  Are there still certain cases where serial processing is still better, faster, and/or more efficient than parallel?

答: CPUs are still more versatile. For example, GPUs are more efficient than CPUs in single precision, but not in double precision. There is much more libraries for CPUs than for GPUs.</p>


答:
you'd be able to do many calculations at once and really improve speed.

improve speed? so what? Through last year I can recall only once or twice when it was needed. Most of the time I've been asked to modify or fix logic, to adjust for a different data source, to improve user interaction etc etc. The only speed customers were interested in these cases was speed of making a change. "Please release new feature in a month, or better yet - in two weeks".

Don't get me wrong - as a coder I enjoy squeezing CPU ticks thoroughly. It's just that this art is not typically in high demand.

Are there still certain cases where serial processing is still better, faster, and/or more efficient than parallel?

I would say there are plenty cases. Serial processing is simpler than parallel, which makes it more efficient in all cases when speed is not a critical requirement. Serial processing allows for easier implementation of complicated logic and user interface, it is easier to specify and test, to maintain and change.

As a rule, serial processing allows clearer expression of programmer's intent and easier reading of code. I would say it saves most precious and scarce resource - programmer's brain.

Title: When should I be offloading work to a GPU instead of the CPU?

Body:
Newer systems such as OpenCL are being made so that we can run more and more code on our graphics processors, which makes sense, because we should be able to utilise as much of the power in our systems as possible.

However, with all of these new systems, it seems as if GPUs are better than CPUs in every way.  Because GPUs can do parallel calculation, multi-core GPUs actually seem like they'd be much better than multi-core CPUs; you'd be able to do many calculations at once and really improve speed.  Are there still certain cases where serial processing is still better, faster, and/or more efficient than parallel?


问:
Difference between a defect and a bug in testing?
What is the difference between a defect and a bug? </p>

答:
The difference is that the term "bug" sound magical. As if a program can randomly have bugs in it after you're done programming. If it has random bugs then it means you didn't conform to the specifications and your program is in error.

A defect means an error where the program doesn't conform to the specifications. This is more severe and basically says, any error is a huge problem with the program and this means that the program is not fit to be released.

The difference is in the attitude of the programmers who use the terms. There are millions of programs that are released with bugs and people are fine with that because they accept for some reason that a bug is magical and random and that every program contains at least one bug. However, a programmer who uses the term "defect" may become uncomfortable with releasing a program with a defect because the term implies a greater severity.

The implications of preferring one term over the other affect us daily.


问:
Do the young minds need to learn the pointer concepts?
Why did the C master Dennis Ritchie introduce pointers in C? And why did the other programming languages like VB.NET or Java or C# eliminate them? I have found some points in Google, and I want to listen your comments too. Why are they eliminating pointer concepts in modern languages?

People say C is the basic language and pointers is the concept that makes C powerful and outstanding and makes C still to compete with more modern languages. Then why did they eliminate pointers in more modern languages?

Do you think knowledge of pointers is still important for new programmers? People are using VB.NET or Java these days, which supports more highly advanced features than C (and does not use any pointer concepts) and many people as I see now (my friends) choose these languages ignoring C as they support advanced features. I tell them to start with C. They say it's a waste to learn the concepts of pointers when you're doing the advanced things in VB.NET or Java which are not possible in C.

What do you think?

Updated:

The comments I read on Google are:

The earlier computers were too slow and not optimized.

Using pointers makes it possible to access an address directly and this saves time instead of making a copy of it in function calls.

Security is significantly worse using pointers, and that's why Java and C# did not include them.

These and some more what I found. I still need some valuable answers. That would be greatly appreciated.

答:
Definitely, we need to have a thorough concept of pointer, if you really want to be a good programmer. The reason for Pointer concept was a direct access to you value which becomes more efficient and effective with time constraints...

Also, Now a days, considering Mobile Applications, which have very limited Memory we need to use it very carefully so that its operation is very quick with user response... So for this purpose we need a direct reference to the value...

Consider Apple's devices, or Objective C language, which is totally working with pointer concept only. All the variable declared in Objective C is having Pointer. You need to go through Objective C's wiki


答:
From the IEEE Standard Glossary of Software Engineering Terminology, which is cited in the Software Engineering Body of Knowledge KA for Software Testing and Software Quality:

bug. See: error; fault.

error. (1) The difference between a computed, observed, or measured
  value or condition and the true, specified, or theoretically correct
  value or condition. For example, a difference of 30 meters between a
  computed result and the correct result. (2) An incorrect step,
  process, or data definition. For example, an incorrect instruction in
  a computer program. (3) An incorrect result. For example, a computed
  result of 12 when the correct result is 10. (4) A human action that
  produces an incorrect result. For example, an incorrect action on the
  part of a programmer or operator. Note: While all four definitions are
  commonly used, one distinction assigns definition 1 to the word
  “error,” definition 2 to the word “fault,” definition 3 to the word
  “failure,” and definition 4 to the word “mistake.” See a2so: dynamic
  error; fatal error; indigenous error; semantic error; syntactic error;
  static error; transient error.

failure. The inability of a system or component to perform its
  required functions within specified performance requirements. Note:
  The fault tolerance discipline distinguishes between a human action (a
  mistake), its manifestation (a hardware or software fault), the result
  of the fault (a failure), and the amount by which the result is
  incorrect (the error). See also: crash; dependent failure; exception;
  failure mode; failure rate; hard failure; incipient failure;
  independent failure; random failure; soft failure; stuck failure.

fault. (1) A defect in a hardware device or component; for example, a
  short circuit or broken wire. (2) An incorrect step, process, or data
  definition in a computer program. Note: This definition is used
  primarily by the fault tolerance discipline. In common usage, the
  terms “error” and “bug” are used to express this meaning. See also:
  data-sensitive fault; program sensitive fault; equivalent faults;
  fault masking; intermittent fault.

I think the definition of failure is the most relevant. Everything begins with a mistake, whether it's in the requirements, the design, the implementation, or the test case/procedure. If this mistake is manifested in software, it becomes a fault. A failure is caused by the existence of one or more faults in software.

I'm not to keen on the formal definition of error, though. I very much prefer the definition provided by dukeofgaming in his answer, however, the one in this answer is the IEEE standard definition of error.

Title: Difference between a defect and a bug in testing?

Body: What is the difference between a defect and a bug? </p>


问:
Ideal programming language learning sequence?
What do you think? What is the ideal programming language learning sequence which will cover most of the heavily used languages and paradigms today as well as help to grasp common programming basics, ideas and practices?
You can even suggest learning sequence for paradigms rather than languages.

N.B. : This is port of the question I asked in stackoverflow and was closed for being subjective and argumentative.

答:
First> What do you want to do when you start programming? Learning how to be a game programmer is different than learning how to be a database programmer for a business... is different than a web based programmer...

Second> Once you start learning, do you want to start high and work your way down? Or start low and work your way up. Learning C# (high level) is a lot different than learning Assembly (Low level).

A huge choice will depend on what you want to do. Programming a driver will most likely include C or Assembly... Programming a website will be PHP, C#, asp, etc... You won't touch assembly for web programming... etc...

As far as basics... once you decide WHAT you want to do, its easier to pick a language. I, for example, have become a data analyst at a hospital. So my "choice" of languages is SQL/TSQL/DB2 for data access, C# WPF for windows clients, Silverlight for web clients, some RPG/CL for IBM back-end logic...

Personally I stick to a good book + build your own program to practice the language. I like APress and loved reading C# 2008 Illustrated when learning C#. They have other books that encompass basics for the unlearned as well as the expert.


答:
What is Design?

There is some amount of variability what people will include under the heading of design.  I think for most it includes systems structure and code structure.

System structure is the macroscopic picture of what the full solution will look like.  You think through the solution and make a list of the things you know you will need. For example a common need is a datastore.  You then need to decide the specifics, what kind of datastore?  Does it need to be a database, what about a persistent datastructure, flat-files, etc. You weigh the needs of the project against the benefits of each implementation in order to choose the one that will best match your needs.

Code structure is also part of design.  The overall architecture, and possibly even suggestions on what kind of abstractions to either stay away from or use.  Should it be structured as one cohesive system.  Should it be broken up in independent components that need to work together.  How will the code structure run in a multi-process environment, is that even a concern?  Will it be multi-threaded instead, or do we need to worry about distributing work load across different servers?  These are the kind of questions you start with.

Why does it come before coding?

It doesn't always come before coding, or at least the final design doesn't.  Sometimes you have to prototype to figure out if a design will work.  Prototyping is essentially choosing a design you think may work because you can't or won't do the analysis during design phase. That doesn't mean all questions need to be answered up front.  Design can be an iterative process, as much as writing code.  You have to understand though, that certain decisions have a much broader impact, and trying to change them later will be extremely expensive.  Moving a monolithic program to a component or SOA would be extremely expensive.  Deciding to change languages is an extremely expensive decision.  When you make design decisions you should be aware of the cost associated with rethinking those decisions later on down the line.


答:
The IEEE Standard Glossary of Software Engineering Terminology (IEEE Std 610.12-1990) defines software design as "the process of defining the architecture, components, interfaces, and other characteristics of a system or component" and "the result of [that] process". In essence, it's the analyzing of requirements to produce some kind of description of what you intend to build. The end result of the design activities are the equivalent to blueprints for building a building.

Design is usually broken down into a high-level (or top-level) description of the system that identifies various components and how components interact with each other as well as a more detailed description of each of those components that allow them to be constructed.

When you are talking about design at any level, you begin to think of concepts such as distribution (what components will be performing what tasks and where those components will be located), concurrency and task processing, data and events, error handling and fault tolerance, data persistence, interaction and presentation. You also consider the quality attributes (such as maintainability, testability, portability, robustness) of the system and its components.

How you approach design depends on the process methodology that you are using to build the system. For example, in a sequential life cycle, you typically perform "big design up-front" (BDUF), where you take your requirements and produce a high-level design, finalize it (for the most part), use that to create detailed designs, and implement that design. In a more iterative and incremental methodologies, you need to consider all known requirements, but you focus your efforts on designing (and then implementing) a subset of those features at any given time, evolving your product and its design as your add additional features.

The specific approaches and techniques that you use to design your system will also vary depending on what the system is. For example, designing a website for your local little league team, designing a web application for Amazon, and designing software that controls aircraft are all very different in terms of formality and what you need to formally document. The specifics of inputs, activities, and outputs are part of the design activities should be specified in your project planning documentation (which again, might vary in formality from verbal agreements to a large specification, depending on the type of project).


答:
This is the objective truth:

Some languages support direct memory access (pointers), some don't. There are good reasons for each case.

As someone said here, back in the days of C, automatic memory management was not as elaborate as it is today. And people had been used to it, anyhow. The good programmers back then had a much deeper understanding of computer programs than of our generation (I'm 21). They've been using punch-cards and waiting days for some compile-time on the mainframe. They probably knew why every bit in their code existed.

The obvious advantage of languages like C is that they allow you to have finer control over your program. When do you actually need it, these days? Only when you're creating infrastructural applications, such as OS-related programs and runtime environments.
If you want to just develop good, fast, robust and reliable software, then automatic memory management is most often your better choice.

The fact is that direct memory access has mostly been abused over the course of software development history. People had been creating programs that leaked memory, and were actually slower because of redundant memory allocation (in C it's easy and common to extend the process' virtual memory space for every single allocation).

These days, virtual machines/runtimes do a much better job than 99% of the programmers at allocating and releasing memory. On top of that, they allow you extra flexibility in the flow you want your program to have, because you're (mostly) not occupied with freeing allocated memory at the right time and place.

As for knowledge. I think it's admirable that programmers know how the environments they program in are implemented. Not necessarily to the smallest details, but the big picture.

I think that knowing how pointers work (at the very least) is interesting. Same as knowing how polymorphism is implemented. Where your process gets its memory from, and how.
These are things that have always been of interest to me, personally. I can honestly say that they have made me a better programmer, but I can not say that they're an educational necessity for anyone who wants to become a good programmer. In either case, knowing more is often going to make you better at your work.

The way I see it, if all you're asked to do is create an application in Java or C# or something like that, then you need to focus on proper design and implementation techniques. Testable code, clean code, flexible code. In that order.

Because even if you don't know all the small details, someone who does will be able to change what you'll have created into something that simply performs better. And that's often not a difficult job, once you have a proper, clean and testable design in place (and that's usually most of the work).

If I were an interviewer looking to hire someone for a high-level-language application, those would be the things I'd be most interested in.

Low-level knowledge is a bonus. It's good for debugging and occasionally creating slightly better solutions. It makes you an interesting person, professionally. It grants you some respect in your workplace.

But in today's world, it's no sacred requirement.

Title: Do the young minds need to learn the pointer concepts?

Body:
Why did the C master Dennis Ritchie introduce pointers in C? And why did the other programming languages like VB.NET or Java or C# eliminate them? I have found some points in Google, and I want to listen your comments too. Why are they eliminating pointer concepts in modern languages?

People say C is the basic language and pointers is the concept that makes C powerful and outstanding and makes C still to compete with more modern languages. Then why did they eliminate pointers in more modern languages?

Do you think knowledge of pointers is still important for new programmers? People are using VB.NET or Java these days, which supports more highly advanced features than C (and does not use any pointer concepts) and many people as I see now (my friends) choose these languages ignoring C as they support advanced features. I tell them to start with C. They say it's a waste to learn the concepts of pointers when you're doing the advanced things in VB.NET or Java which are not possible in C.

What do you think?

Updated:

The comments I read on Google are:

The earlier computers were too slow and not optimized.

Using pointers makes it possible to access an address directly and this saves time instead of making a copy of it in function calls.

Security is significantly worse using pointers, and that's why Java and C# did not include them.

These and some more what I found. I still need some valuable answers. That would be greatly appreciated.


问:
What is a good workflow for a C++ beginner?
I desire to become a more serious scientific C++ programmer.  To do so, I need  a consistent style of workflow (e.g., coding, test-driven development, etc.).  My question is simply what sort of workflow should I consider?  I am familiar with Linux and Mac, and prefer to use free tools like cppcheck, valgrind, g++, etc.  I prefer to use Qt and Boost within my programming toolbox, and I am considering using mock objects (either Google Mock or MockPP) within the TDD.  I typically use vim for my editor.

In essence, should I do something like the following:

TDD -> code -> cppcheck -> valgrind -> debug -> repeat until ready to
  use profilers and coverage mechanisms

or, should I pursue some other way?  I realize that the opinions will vary, and I also understand that I must first take the time to think through the process and document my thoughts whether written or UML before coding, and I also know that I should document the code as I step write the code.

Any and all thoughts will be appreciated.

答:
Don't fall into the guess & check trap of c++ programming. That is, don't get into a tight edit -> compile (get error) -> edit loop w/o understanding what is going on.

If you don't understand why adding an ampersand or removing parenthesis fixes a compiler error/warning then take the time to understand.

Excellent resource for understanding the idiosyncrasy of c++: http://www.parashift.com/c++-faq/

OwnerDisplayName: Tim


答:
The simple answer is that a GPU works best when you need to do a fairly small, fairly simple computation on each of a very large number of items. To accomplish much this way, the computation for each item must be independent of the computations for the other items. If there's (normally) some dependency between one item and another, you generally need to figure out some way to break it before you're going to get much out of executing that code on the GPU. If the dependency can't be broken at all, or requires too much work to break, the code might execute faster on the CPU.

Most current CPUs also support quite a few types of operations that a current GPUs simply don't attempt to support at all (e.g., memory protection for multitasking).

Looking at it from a slightly different direction, CPUs have been (largely) designed to be reasonably convenient for programmers, and the hardware people have done their best (and a darned good best it is!) to create hardware that maintains that convenient model for the programmer, but still executes as quickly as possible.

GPUs come at things from rather the opposite direction: they're designed largely to be convenient for the hardware designer, and things like OpenCL have attempted to provide as reasonable of a programming model as possible given the constraints of the hardware.

Writing code to run on a GPU will typically take more time and effort (so it will cost more) than doing the same on the CPU. As such, doing so primarily makes sense when/if either:

The problem is so parallel that you can expect a large gain from minimal effort, or

The speed gain is so important that it justifies a lot of extra work.

There are some obvious possibilities for each -- but a huge number of applications clearly aren't even close to either one. I'd be quite surprised to see (for example) a CRUD application running on a GPU any time soon (and if it does, it'll probably happen because somebody set out with that exact goal in mind, not necessarily anything approaching an optimal cost/benefit ratio).

The reality is that for a lot of (I'm tempted to say "most") applications, a typical CPU is far more than fast enough, and programming convenience (leading to things like easier development of new features) is much more important than execution speed.