问:
What is the best practice for refactoring a static method in order to make it more testable?
Let's say you have a static method that looks something like this:

```<code>public static bool Foo()
{
    var bar = new Bar();
    //do some stuff here
}
</code>```

This method as it stands can be a real headache to unit test.

What is the best practice to refactor this so that it can be testable, without turning it into an instance method or changing the method signature?

答:
It really depends on what Bar is.  If it's something simple then your Foo method is already testable, you just need to specify your expectations and invoke it statically, e.g.:

```<code>Assert.IsTrue( FooContainer.Foo() );
</code>```

But if Bar encapsulates, say, your database access layer, then you can't test Foo without a real database, which is why (thanks @ysolik), static methods are death to testability.  Or, in the words of Michael Feathers, "don't hide a TUF in a TUC" (TUF stands for a test-unfriendly feature, TUC stands for a test-unfriendly construct).  If Bar is indeed test-unfriendly, then sorry, it doesn't work well without making Foo an instance method.  You would need to redesign your code first:

```<code>public class FooContainer {
    public bool Foo() {
        var bar = new Bar();
        //...
    }
}
</code>```

When Foo is no longer static, you can invoke it on an instance of FooContainer:

```<code>var container = new FooContainer();
Assert.IsTrue( container.Foo() );
</code>```

The next step is to extract an interface from Bar (let's call it IBar) and inject it into FooContainer:

```<code>public class FooContainer {
    private readonly IBar m_bar;
    public FooContainer( IBar bar ) { m_bar = bar; }
    public bool Foo() {
        // don't create another Bar, use m_bar
    }
}
</code>```

Now you can mock/stub IBar with your favourite isolation framework and test your FooContainer code in isolation from its dependencies.

Body:
It really depends on what Bar is.  If it's something simple then your Foo method is already testable, you just need to specify your expectations and invoke it statically, e.g.:

Assert.IsTrue( FooContainer.Foo() );

But if Bar encapsulates, say, your database access layer, then you can't test Foo without a real database, which is why (thanks @ysolik), static methods are death to testability.  Or, in the words of Michael Feathers, "don't hide a TUF in a TUC" (TUF stands for a test-unfriendly feature, TUC stands for a test-unfriendly construct).  If Bar is indeed test-unfriendly, then sorry, it doesn't work well without making Foo an instance method.  You would need to redesign your code first:

public class FooContainer {
    public bool Foo() {
        var bar = new Bar();
        //...
    }
}

When Foo is no longer static, you can invoke it on an instance of FooContainer:

var container = new FooContainer();
Assert.IsTrue( container.Foo() );

The next step is to extract an interface from Bar (let's call it IBar) and inject it into FooContainer:

public class FooContainer {
    private readonly IBar m_bar;
    public FooContainer( IBar bar ) { m_bar = bar; }
    public bool Foo() {
        // don't create another Bar, use m_bar
    }
}

Now you can mock/stub IBar with your favourite isolation framework and test your FooContainer code in isolation from its dependencies.


问:
What is Domain Driven Development in practical terms?
I heard about Domain Driven Development from a developer in the area.  He talked it up like it was just about the silver bullet to changing requirements.

I read the wiki.  Still not too clear.  What is "3D" in practical terms?  Is it really that amazing that now UML class diagramming is just obsolete?

答:
A high-level description could be -

Model your classes to mirror the data structures and behaviour of your problem domain.

This allows you to map changes in your problem domain directly to changes in your code, so it should be more easy to update as your problem domain evolves.

ContentLicense: CC BY-SA 3.0


问:
Split git repository in two?
Nearly a year ago, I started writing a game. I was just messing around at the time, but now I'm pretty serious about it. As it's grown, it's taken on the responsibilities of a major game engine, as well as the game logic of an actual game.

At some point, I decided I should break the two out, and designate a game and a game engine. I kept this separation in mind as I programmed for about the last 8 months, but I never did actually separate them.

Now I intend to finally move forward with this. The project is all in one git repository right now. I'd like to have two repositories; one for the game engine and one for the game.

I have two concrete questions:

1) How should I split the repository? I'd like to maintain history, so I'm thinking I'll just cp the repository, prune the two, and then check in. Any better ideas?

2) Should I add my game assets (models, images, sounds..) to the game repository, or should I make a third for these? I have quite a few assets, and many are subject to change, so it could create an inconvenient amount of noise.

答: As for #1, git provides some tools to make that very easy to do. For #2, I'd suggest a 2nd repository to keep your history clean and useful. You can pull it into your main working tree easily with git submodules.</p>

Title: Split git repository in two?


问:
Multithreaded file copy
There is an utility we have which is used to upload files (and perform other operations on the file) to a network shared location.

The file size tends to vary from a few mb to 500 mb.

A suggestion has come up that we should maybe support multi-threading when uploading the files to the shared location - not required to do it in byte chunks - each thread should pick one file and try to upload.

I am not so sure if multithreading can speed up IO operations like this. Is my hunch valid?

If indeed we are required to build this functionality I was wondering what would be a good design approach for the copy file engine?

Would it make sense to use a tool like robocopy (I read the newer versions support multithreading)?

Edit: Apologies for the delay and missing some vital information.

This utility is built using C# (.Net 2.0) and any future update also has to be using .Net (framework version is not a constraint). The utility is installed on the machines of the users (around 20 all on WinXP). The target share is on Win2k3 server.

Edit 2: have decided to run some tests with a simple application implementing the file upload through TPL. Post this analysis we will decide whether to go ahead or not. Thanks everyone for the help extended.

答:
If you are talking about one big file, multithreading won't really help. You're going to be I/O bound, so using a single thread won't slow THAT upload down.

What you do may to worry about, though, is resource contention (assuming you are writing the server, too). If you're handling the upload in the thread that also accepts and processes new requests, other requests will be waiting. As long as you defer back to the selector queue after reading a chunk from the socket and writing it to disk, though, you should be fine.

Title: Multithreaded file copy


答:
This depends on what the limiting factor is, doesnt it?  If the bottleneck is the utility program, then sure, running more than one copy or using more threads will speed things up.  If the network is the limiting factor, then adding multiple instances of the utility isnt going to help since you still will be stuck moving at most X bytes per second.  In fact it might hurt because you have the additional overhead of a second copy of the app.  Same with disk-IO.  You can only copy as fast as either machine can read from and write to disk.  If thats already max'd out, adding copies isnt going to help.

What you need to do is test to see what the bottleneck is, and go from there.

Title: Multithreaded file copy

Body:
There is an utility we have which is used to upload files (and perform other operations on the file) to a network shared location.

The file size tends to vary from a few mb to 500 mb.

A suggestion has come up that we should maybe support multi-threading when uploading the files to the shared location - not required to do it in byte chunks - each thread should pick one file and try to upload.

I am not so sure if multithreading can speed up IO operations like this. Is my hunch valid?

If indeed we are required to build this functionality I was wondering what would be a good design approach for the copy file engine?

Would it make sense to use a tool like robocopy (I read the newer versions support multithreading)?

Edit: Apologies for the delay and missing some vital information.

This utility is built using C# (.Net 2.0) and any future update also has to be using .Net (framework version is not a constraint). The utility is installed on the machines of the users (around 20 all on WinXP). The target share is on Win2k3 server.

Edit 2: have decided to run some tests with a simple application implementing the file upload through TPL. Post this analysis we will decide whether to go ahead or not. Thanks everyone for the help extended.


问:
How would you rank these programming skills in order of learning them?
As a general purpose programmer, what should you learn first and what should you learn later on?

Here are some skills I wonder about...

SQL

Regular Expressions

Multi-threading / Concurrency

Functional Programming

Graphics

The mastery of your mother programming language's syntax/semantics/featureset

The mastery of your base class framework libraries

Version Control System

Unit Testing

XML

Do you know other important ones? Please specify them...

On which skills should I focus first?

答:
In this modern world of ours most of the stuff that once was required (and will still be smacked as the only true knowledge by the old ones) is not required so much today. You don't need to know how the hardware work to still be an excellent programmer.

In my own opinion the only things you need to know in these modern days of ours are:

OO programming.

Unit testing.

Basic Big O notations (when should you use an ArrayList and when should you use an LinkedList?).

Communication. Communication is always needed. Either with the consumer, client or your programmer pals.

Refactoring/Removing smelly code.

Anything else will follow up afterward with practice and experience.

Except Unit testing of the things you mention I would only consider SQL, Concurrency and Version Control System to be useful more than maybe a few times in your career. The rest are just general stuff which can be useful at times though aren't needed.

OwnerDisplayName: mumtaz

LastEditorDisplayName: mumtaz

ContentLicense: CC BY-SA 2.5


答:
While copying a lot of smaller files, multithreading can help because there tend to be gaps in the data transfer while the program is searching directories for the next file, opening it and getting the data.

Multithreading will also help when the client and server both have parallel data storage such as RAID or SSD: anything that performs better with higher queue depth numbers.

Other than that, it will often slow things down. For example, making a single hard drive read or write two files at the same time will force it to repeatedly seek from file 1 to file 2.

Title: Multithreaded file copy


问:
Using a standard localised data cryptography pattern with an ASP.NET MVC3 application
I have been given the task to rejuvenate an existing Human Resources application from Access into ASP.NET. This is a strictly internal application, and I have no problems developing it within our standard ASP.NET environment. It does however pose it's own problems, both technically and ethically.

The HR app will be responsible for storing employee pay data, for which it is not appropriate for me, the developer, to see. It's essentially three fields (salary band, actual salary, comments) and four other values, calculated on the front end.

I'd like to go down the cryptography route to encrypt said data - cryptography with the .NET Framework is very easy to implement. I could "roll my own", which as we all know is a bad idea. However I was wondering if there was a more better way of doing it. Here are some points:

For starters, we're all attached to the same domain.

I've heard and read about Certificates, can they be useful here? (x.509?)

The data will be stored in our local SQL Server, obviously the actual data here needs to be encrypted, so can SQL Server help me? Certificates again?

If a user doesn't have the appropriate certificate, can the data just be null? (You can't see this matey!)

This encryption problem doesn't have any relevance to usage of the system via a permissions model.

答:
This sounds to me like the same issue as credit card data in pci certified systems. You as a developer and later on the sysadmins and support staff are not allowed to see the data. Read into how people solve that problem and it should solve yours.

It comes down (a really short description) to encrypting each row with different keys and an environmental key. The environmental key should be stored only on the app servers, the per row key can be stored in the database. This way, someone with only database access cannot decrypt it, someone with only appserver access cannot decrypt it. Make sure that production and dev/test are not using the same environmental keys. So, a production database going back to dev or test for debugging purposes cannot be decrypted because dev does not know the production key. Still you can reset all data in the database (production backup downloaded for testing) with a single sql update (point it to a single per row key and use the dev encryption key and set everything to the same encrypted value).

Have a tool to replace an environmental key (decrypt with old and encrypt with new the data) if it might be compromised (or an employee having access to it leaves.)

There are a lot of sites with information on these pci standards and implementations. Read into it and you will certainly get an implementation which would work for your problem.

The encryption algorithm itself does not really matter, as long it is a known strong method


答:
Well, first of all, I don't think that the Wikipedia article you refer to is very good, mostly because it references a bunch of things that are only ancillary to Domain Driven Design and does little to enlighten anyone about the practice.

But, as someone who has taken Domain Driven Design to heart (which usually goes by DDD, rather than 3D, for what it's worth), I always felt the fundamentals of DDD are obvious, if you read so much as the first chapter of Eric Evans' book. But it is a set of patterns and practices, so it's not that easy to give a 3 sentence summary of what it is and what the advantages are without going into some detail. Which details resonate with any one person might be very different, too; it's probable that 10 years ago I wouldn't have seen the point at all, myself.

DDD is not a silver bullet. When done sensibly, it's about taking a craftsman-like approach to building software, and recognizing the need to reduce the cognitive friction between development teams and the businesses they are building software for. One of the most important practices is to have a layer in which the domain vocabulary used by the software team and the business team matches as closely as possible. You build this layer iteratively as you come to understand the business problem that you are trying to solve. When business logic is sensibly encoded in this layer, isolated from all the convoluted dependencies that enterprise applications typically have by factoring interactions with those systems out to interfaces, the language used in the actual domain layer eventually becomes fairly concise, obvious, and readable. When you review your model with the business, they will often correct you, and you gradually refactor to a deeper understanding of the domain.

Considering the shape that I've seen most enterprise software in, in practice, DDD may sound like a silver bullet, because most enterprise software has such poor separation of concerns that it's nearly untestable, and the software team lives in great fear of change because they have no idea what the side effects of ostensibly even trivial code changes might be, whereas a properly factored domain layer will be independently testable and verifiable. But actually, DDD acknowledges that systems rarely exist in isolation. DDD includes coping patterns for legacy systems (Anti-corruption layer, bounded contexts, to name a couple).

If you practice object-oriented design, including the discipline of loose coupling, and you practice unit testing fairly religiously, and you mercilessly refactor code, and you work with domain experts while building your system, essentially you'll end up with a result that's basically what advocates of domain driven design are talking about.

There are a few specific patterns described in Evans's book that apply mostly to enterprise software development, and some that are fairly universal principles, but essentially, DDD is a pragmatic approach to software development that can, over time, reduce the buildup of technical debt, and make your customers happier because you are able to speak the same language with each other, and deliver better-working solutions because of the advantages of understanding each other better.

Title: What is Domain Driven Development in practical terms?

Body:
I heard about Domain Driven Development from a developer in the area.  He talked it up like it was just about the silver bullet to changing requirements.

I read the wiki.  Still not too clear.  What is "3D" in practical terms?  Is it really that amazing that now UML class diagramming is just obsolete?


问:
Patterns for Continuous Integration and DVCS
We currently use Subversion and TeamCity, we're going to move to using Mercurial (specifically Kiln as we're FogBugz users).

Obviously this will result in changes - hopefully improvements - in our development patterns (all two of us!) but the one issue I'm stuggling with is how to structure things so that we still enjoy the benefits of continuous integration/our CI server (that there are and will remain benefits is a given, discussion of which is outside the scope of this question).

With SVN we are committing to a limited number of central repositories - effectively one per project (more or less one Visual Studio Solution) so its easy to trigger a build and to get the reassurance that all the files have been commited and that there are no stray dependencies etc, etc. But if we're going to take proper advantge of mercurial we're going to want to have more repository instances - where I'd expect changes to generally flow towards a definitive "live" repo. The problem I'm struggling with is that the live repo seems to me to be too "late" to trigger my CI builds OTOH one CI build per project per developer is probably excessive (and causes other issues). I think that live is too late because - given that we can/should have more repos (clones of) - live should be deployable whereas we want to be building/running tests (unit at commit, integration at scheduled intervals) on development commits.

I'm fishing a bit but that's because one of the things that a central subversion repo gives one (me, with our setup!) is a lot of clarity about what to build when.

n.b. I'm not asking about the mechanics of using mercurial with continuous integration - I have that working a treat for a personal project, its patterns and structures and working practice/workflow that I'm trying to get my head round.

答:
First, having multiple builds per project in TeamCity is really the way to go. The nature of the platform makes it really easy -- the copy button is there for a reason.

In any case, when we were on SVN, we typically ran 2 builds for each project -- one pointed at the main development line (in our case the trunk) and one pointed at our release branch. We carried this build setup over to HG while following a branching model similar to this one . Only real challenge has been finding a new funk schwea about build numbers now that we could no longer use current SVN revision number.

We try and encourage folks to push relatively often, especially when there is lots of work going on at once and we wanted faster feedback cycles. Just because it is a DCVS doesn't mean you have to only push once a day or something.


问:
Protecting an Application with Logins
I am a beginner Java programmer, and have tried searching for a way to set up a login system with my Java application.  The application is a game and I do not want people to just upload it to a website so others can down load and play. I want to use something like MySQL to make the user authenticate in order to use the application. Does anyone have a suggestion?</p>

答:
In short, don't bother.

A runnable JAR can easily be decompiled back into the Java and the login page would be circumvented rather easily. If the game is in the form of a Java applet, once a person gets past the login page, the applet can be downloaded and distributed with no problem.

Developers have been trying for years to stop people from using their software. To the best of my knowledge, there is no way to stop people from stealing your software. While you can go the route of security through obscurity, this is not in fact security, only a deterrent to the less experienced.

You may be able to stop some people from playing without your permission, but the client will need the code, even if in a binary form, to play the game, and therefore can access and modify it at will.

Title: Protecting an Application with Logins


问:
Is having your own website important?
How necessary or important is it? I try to keep a running list of blogs or sites to follow, but a lot of the time I pull up someone's profile and notice there isn't anything there. Is it really important?

I understand are different levels of programming (from C/C++ system programmers to Rails and even Haskell and J) and not everyone works in a language easily worked with for web based applications. Not everything is web-centric, however with the advent of many popular and sometimes free services I don't think it's unreasonable to expect a majority of programmers to have a personal site.

答:
Important in what sense?

Important to you, to be able to put your thoughts into writing and maybe get some feedback from other developers? Then sure, why not? There's no reason to not have your own website.

Important for your career, to have something to show for yourself? Perhaps, depending on your specific type of work. If you do web development, it would be nice to have a place of your own to show off what you know. If you do freelance work, it would be a great way to advertise yourself.

Important for the programming community? Not likely. Developer blogs are a dime a dozen and chances are yours won't be particularly noteworthy. I don't think anyone should feel compelled to have something to say. But again, no reason not to do so if you want.

It all comes down to why you think you should have a website in the first place. Plenty of developers do, and plenty don't. It's a matter of your own interests and motivations. If it's important to you, for whatever reason, then go for it. Otherwise, don't bother.

Title: Is having your own website important?


问:
Boss is skeptical of using a version control system for new project, should I anyway?
See https://softwareengineering.stackexchange.com/questions/109817/superior-refusing-to-use-subversion

My question is similar, but here are the main differences in my scenario:

We are starting a new project from scratch, using PHP and web tech. There would be no down time in development as we would be adopting it from the beginning, if I have my way.

My dev team consists of me, and my boss. We are the "IT" Department of a relatively small firm.

The web app will replace a legacy application with absolutely no source control. Due to variations in geographical legal requirements, the decision was made (before I was hired) to fork the app into 7 completely separate directories for each version. Different developers did different things in different places at different times after that. Patching changes across them, well, I think it could be done better, I guess that's why I'm posting.

My boss's proposal, directly pasted from an email:

Updates should be submitted as packages in the SUBMISSIONS folder.  The package should contain all relevant files as well as an
  ‘UPDATE.NFO’ file that contains a description of the update, a list of
  all new files included (with descriptions), and a list of all modified
  files with modification details.

Update packages should focus on an individual element and not stray from its intended purpose.  Code should be designed to be modular and
  reusable whenever possible.

All submitted packages should be installed in each developer’s test environment soon after submission.  Each developer is to review the
  new addition and voice any concerns over its installation to the
  production environment.  A standard package update should be held for
  a minimum of 3 business days for this review process before being
  loaded into the production environment.  High priority updates/fixes
  can skip this requirement.

The reason source control was invented is to make all that automatic, right? I suggested subversion, because that's what I used in college. Boss doesn't like subversion because "It makes a mess of the code" (i.e. uses binary magic and is not plainly readable). We did try it one time, but I think trying to use it on windows made weird lower/uppercase errors and we couldn't check out our files. I don't know whether it's only subversion, or all source control products that are objectionable.

So, what kind of argument should I make to my boss? Or is he right, and there could be a danger of losing all our work from some weird bug?

Or am I wrong altogether? Is source control really necessary in my situation? This is our main business-critical software we're talking about, so it will end up huge no doubt. But there's only 2 developers (now).

Additionally, If I can't convince him, would there be any point to me using it only for myself? I am speaking as someone with very limited experience actually using svn; all I really know is checkout and commit. What are the features of source control (may include other products than svn) that would aid in my individual development effort?

Please no "get another job" comments. That's not helpful to the debate.

答:
Yes, using source control, even if only for you, is totally worthwhile. Git, for instance, works really well for a standalone developer and allows you to do things such as branch and merge (with the lowest possible cost) and version your changes as you go.

SVN - or any version control system, really - allows you to do this too, but merging is a bit more problematic.

ContentLicense: CC BY-SA 4.0


答:
If I can't convince him, would there be any point to me using it only for myself?

Yes.  There's benefit to using it just for yourself.  You get change history so you can see what's different.

No.  There's no benefit because your boss has doomed your project to lots of pointless rework because they fouled things up.

Title: Boss is skeptical of using a version control system for new project, should I anyway?

Body:
See https://softwareengineering.stackexchange.com/questions/109817/superior-refusing-to-use-subversion

My question is similar, but here are the main differences in my scenario:

We are starting a new project from scratch, using PHP and web tech. There would be no down time in development as we would be adopting it from the beginning, if I have my way.

My dev team consists of me, and my boss. We are the "IT" Department of a relatively small firm.

The web app will replace a legacy application with absolutely no source control. Due to variations in geographical legal requirements, the decision was made (before I was hired) to fork the app into 7 completely separate directories for each version. Different developers did different things in different places at different times after that. Patching changes across them, well, I think it could be done better, I guess that's why I'm posting.

My boss's proposal, directly pasted from an email:

Updates should be submitted as packages in the SUBMISSIONS folder.  The package should contain all relevant files as well as an
  ‘UPDATE.NFO’ file that contains a description of the update, a list of
  all new files included (with descriptions), and a list of all modified
  files with modification details.

Update packages should focus on an individual element and not stray from its intended purpose.  Code should be designed to be modular and
  reusable whenever possible.

All submitted packages should be installed in each developer’s test environment soon after submission.  Each developer is to review the
  new addition and voice any concerns over its installation to the
  production environment.  A standard package update should be held for
  a minimum of 3 business days for this review process before being
  loaded into the production environment.  High priority updates/fixes
  can skip this requirement.

The reason source control was invented is to make all that automatic, right? I suggested subversion, because that's what I used in college. Boss doesn't like subversion because "It makes a mess of the code" (i.e. uses binary magic and is not plainly readable). We did try it one time, but I think trying to use it on windows made weird lower/uppercase errors and we couldn't check out our files. I don't know whether it's only subversion, or all source control products that are objectionable.

So, what kind of argument should I make to my boss? Or is he right, and there could be a danger of losing all our work from some weird bug?

Or am I wrong altogether? Is source control really necessary in my situation? This is our main business-critical software we're talking about, so it will end up huge no doubt. But there's only 2 developers (now).

Additionally, If I can't convince him, would there be any point to me using it only for myself? I am speaking as someone with very limited experience actually using svn; all I really know is checkout and commit. What are the features of source control (may include other products than svn) that would aid in my individual development effort?

Please no "get another job" comments. That's not helpful to the debate.


问:
Do companies care about university grades when hiring programmers?
Most of my fellow students that I've talked to claim that aiming for good grades is useless as the companies don't care about them when hiring programmers. To them, it's enough to have simply attended courses which may be important, and that's that.

Is this true? Are university grades useless when leaving campus, or do employers ask to see them for an interview?

答:
Grades do NOT matter at ALL!!!!  I graduated in May 2011 with a 3.8 GPA. I received an Honors Degree.  Worked my ass off to get those grades. I didn't miss ANY school at all.

My friend, got through with C's and D's.  Slept in when he wanted, skipped when he wanted. We both graduated, and he got a job because he knew someone in the field.  That is the only reason!!!!! We both applied for the job, I have NEVER heard from them.

Unless you are in the medical field, enjoy your college time.  Don't stress over getting the good grades, it is totally over-rated!!!  I have been applying for jobs since graduating in May 2011.  I have applied for jobs all over the United States.

I strongly suggest working on getting experience in the field, either by apprenticeship or internship : get to know someone in the field.


答:
Benefits of source control go far beyond allowing multiple developers to work on a single piece of code. Eric Sink, the founder of SourceGear, lists a few compelling reasons to use source control as a sole developer:

```<code>- It's an undo mechanism.
- It's a historical archive.
- It's a reference point for diff.
- It's a backup.
- It's a journal of my progress.
- It's a server.
</code>```

Eric also happens to have a very nice beginners' Source Control How-to. There is a free online Mercurial tutorial available by Joel Spolsky, Mercurial is a popular distributed version control system.

As the next step I suggest you to start using source control locally on your machine, as a sole developer. Very soon your boss will notice that you're capable of sheer magic, like telling within minutes, if not seconds, how far back a super-critical bug goes and then you would tell him precisely which customer accounts were affected and need fixing before all hell breaks loose. Or being able to undo any changes CEO disapproves of super fast.

And finally before you try to convince your boss you may want to delve into the topic of objection-handling. It's 101 of sales.

If unsuccessful - move on as soon as practically possible, not much point in wasting your time tilting at windmills.

Body:
Benefits of source control go far beyond allowing multiple developers to work on a single piece of code. Eric Sink, the founder of SourceGear, lists a few compelling reasons to use source control as a sole developer:

- It's an undo mechanism.
- It's a historical archive.
- It's a reference point for diff.
- It's a backup.
- It's a journal of my progress.
- It's a server.

Eric also happens to have a very nice beginners' Source Control How-to. There is a free online Mercurial tutorial available by Joel Spolsky, Mercurial is a popular distributed version control system.

As the next step I suggest you to start using source control locally on your machine, as a sole developer. Very soon your boss will notice that you're capable of sheer magic, like telling within minutes, if not seconds, how far back a super-critical bug goes and then you would tell him precisely which customer accounts were affected and need fixing before all hell breaks loose. Or being able to undo any changes CEO disapproves of super fast.

And finally before you try to convince your boss you may want to delve into the topic of objection-handling. It's 101 of sales.

If unsuccessful - move on as soon as practically possible, not much point in wasting your time tilting at windmills.


答:
Don't ask him. Don't tell him. Show him.

Install svn, or git, or whatever you like on some extra machine. Practice using it yourself until you feel comfortable not just using it, but explaining it. If you're going to make him comfortable with your new system, you'll need to be more than comfortable with it yourself. You'll need to be able to help him recover easily when he screws up a merge or checks something into the wrong place.

When you're ready, show him exactly what you're talking about. Show him that it doesn't "make a mess" of anything. Point out that it doesn't just let you retrieve any previous version of your code with ease, it also makes it possible to know exactly what changed between any two versions.

Point out that if anything ever happens to the server (serious bug, virus, hacker, disk crash...) you'll both look like heros if you can instantly reconstruct the necessary version. Point out, too, that you'll look twice as good if you're able to produce any version on demand. Search your old e-mail and compile a list of problems you've had over the past year that you could have avoided with version control.

Give him a cheat sheet that will make it easy for him to use your version control system.

Finally, suggest some options but leave the decision to him. Should you set up your own server, or use one of the many hosted services? Should you use svn, git, or something else? Should you migrate all seven projects to the system, or try it with one or two at first?


问:
Rapid code analysis
I have recently moved to a new company where I am tasked (in part) with evaluating the existing code base and coming up with patterns and best practices.

Our development team consists of around 20 programmers and we have nearly as many projects. The bulk of the code is done in C# and SQL (Stored Procedures).

Naturally, I speak to the guys writing the code. However, I need to read through the code to get a proper idea of what is going on. Given the number of projects, it is extremely time consuming; especially if you're simply trying to get a high-level view of what is going on.

At the moment I am using Enterprise Architect to import the classes into class diagrams. This helps to an extent, but I guess something like a flow diagram would be more useful.

I suspect team leads deal with this same problem often when it comes to code reviews. How do you manage to speed-up code analysis?

答:
Disclose: I am part of the NDepend team.

Did you try the tool NDepend ? The tool provides many features to do fast code analysis on a .NET code base, like default and custom code rules or dependency graph and dependency matrix. This should help you to quickly pinpoint the bulk of code quality and code architecture flaws.

Concerning code reviews, NDepend proposes the code diff feature. Basically you can compare 2 different snapshots of your code base and explore diff easily. This way you can focus your code reviews on code that has been added or changed since your last stable version.

For example below I am listing NUnit methods added or changed between v2.5.3 and v2.5.8. By right-clicking a changed method you can see the source code diff, or decompile both older and newer versions of the method with Reflector and see the decompiled results diff.

OwnerDisplayName: Andre

Title: Rapid code analysis

OwnerDisplayName: Patrick Smacchia - NDepend dev


答:
Before I would have only said that Websites and Blogs only serve a valuable purpose if you work as a Web Developer or anything where having a Portfolio on-line would generate you business. But after much thought it became clear I was being extremely naive.
Blogs as people have already said are a great way to track work you do and different things you may learn and great for looking back and reminiscing, but recently I discovered a much more valuable use. During your time working in technology I can guarantee you will be asked to demonstrate or convey your passion for technology and when that time comes you will hit out with the every cliché in the book, BUT if you keep a blog you can add it to your CV for employers to look at and use it when asked that all important question.
It wasn't until recently when I worked for a world leading investment bank I discovered a sophisticated blog can be the difference between you and the next guy.

Even sites such as this , and giving consistent advice can help display a strong passion for tech.

So in my honest opinion it cant do no harm and if anything will personally benefit you, and can benefit you career wise aswell.

Title: Is having your own website important?

Body:
How necessary or important is it? I try to keep a running list of blogs or sites to follow, but a lot of the time I pull up someone's profile and notice there isn't anything there. Is it really important?

I understand are different levels of programming (from C/C++ system programmers to Rails and even Haskell and J) and not everyone works in a language easily worked with for web based applications. Not everything is web-centric, however with the advent of many popular and sometimes free services I don't think it's unreasonable to expect a majority of programmers to have a personal site.

OwnerDisplayName: Chris McGuire


问:
Why isn't rich code formatting more common?
I was reading Code Complete and in the chapter on layout and style, he was predicting that code editors would use some sort of rich text formatting. That means, instead of code looking like this

```<code>Procedure ResolveCollisions
{ Performs a posteriori collision resolution through spatial partitioning algoritm }
(
  CurrentMap : SpriteContext,
  PotentialColliders: SpriteList
)
var Collider  : Sprite,
    Collidee  : Sprite,
    Collision : SpriteCollision
begin
  DoStuff();
end.
</code>```

it could look something like this:

Procedure ResolveCollisions

Performs a posteriori collision resolution through spatial partitioning algorithm

Parameters

CurrentMap : SpriteContext

PotentialColliders : SpriteList

Local Variables

Collider  : Sprite

Collidee  : Sprite

Collision : SpriteCollision

```<code>    DoStuff();
</code>```

I've seen syntax coloring and highlighting and even parentheses coloring, but nothing that looked like this in actual code. I was wondering if this sort of thing actually ever existed, or perhaps if it was decided that it didn't have enough benefit or that it was an entirely bad idea.

Have any of you seen richly-formatted code like this before, or know if the idea was ever considered and eventually rejected?

答:
Simple reason: editor/tool independence.

Once you make you code "rich" -- it will be tied to the editor that you used -- or to the ones that can understand rich code. All other editors that can't handle richness will show gibberish.

In the same vein, rich code wouldn't play well with diff tools. For example, if you just changed  some formatting, the diff will show a difference, but it is not even a difference you're least concerned with.

And what about version control? How would you tell it to ignore all the changes in formatting and see files as modified only when there is some "real" change.

Finally I guess, the whole point of rich code is readability -- and for that, I think better (and more) comments, logical identifier names, and consistent indentation will suffice.

In essence, the programming text is best handled as a plain text -- what you're seeing is what the reality. (which is also in line with the Pythonic idea of explicit better than implicit)


问:
Single statement if block - braces or no?
Which is better/more generally accepted?

This:

```<code>if(condition)
{
  statement;
}
</code>```

Or:

```<code>if(condition)
  statement;
</code>```

I tend to prefer the first one, because I think it makes it easier to tell what actually belongs in the if block, it saves others from adding the braces later (or creating a bug by forgetting to), and it makes all your if statements uniform instead of some with braces and some without.  The second one, however, is still syntactically correct and definitely more compact.  I'm curious to see which is more generally preferred by others though.

答:
I prefer braces for consistency, but not wasting too much white space (so more readably formatted code is in my limited field of view).  So I write this for short enough lines:</p>

```<code>If (cond) { statement; }
</code>```

Body:
I prefer braces for consistency, but not wasting too much white space (so more readably formatted code is in my limited field of view).  So I write this for short enough lines:

If (cond) { statement; }


答:
There is no technical reason that you couldn't. If text editors can do syntax highlighting, they could just as easily change other aspects of the display to highlight code.

However, it's one thing to have whatever is being typed change colors as the editor figures out what you are typing. Having the text suddenly change sizes and jump around while you are typing would get really obnoxious.

However, for a 'static' code display, you could easily beautify source code. For example take any halfway decent source->html converter, and add whatever font sizes and styles you like to the stylesheets, and you'll have rich formatted code.

Title: Why isn't rich code formatting more common?

Body:
I was reading Code Complete and in the chapter on layout and style, he was predicting that code editors would use some sort of rich text formatting. That means, instead of code looking like this

```<code>Procedure ResolveCollisions
{ Performs a posteriori collision resolution through spatial partitioning algoritm }
(
  CurrentMap : SpriteContext,
  PotentialColliders: SpriteList
)
var Collider  : Sprite,
    Collidee  : Sprite,
    Collision : SpriteCollision
begin
  DoStuff();
end.
</code>```

it could look something like this:

Procedure ResolveCollisions

Performs a posteriori collision resolution through spatial partitioning algorithm

Parameters

CurrentMap : SpriteContext

PotentialColliders : SpriteList

Local Variables

Collider  : Sprite

Collidee  : Sprite

Collision : SpriteCollision

```<code>    DoStuff();
</code>```

I've seen syntax coloring and highlighting and even parentheses coloring, but nothing that looked like this in actual code. I was wondering if this sort of thing actually ever existed, or perhaps if it was decided that it didn't have enough benefit or that it was an entirely bad idea.

Have any of you seen richly-formatted code like this before, or know if the idea was ever considered and eventually rejected?


答: Perhaps richly formatted code has not caught on because it is not such a cool idea. Personally, I do not like what I see in the example you provided. I use syntax coloring and highlighting, but that formatting is too wrought, and it deviates too much from the way I am used to seeing and writing code.</p>


问:
Should exceptions of a subclass extend the superclass exceptions or my own namespace?
Our library extends another (third-party) library. When we create a child class in our library, and want to throw exceptions, should those exceptions extend the exceptions from the parent class or from our own namespace? For example:

```<code>class Their_Exception extends Exception {}

class Their_Class {}
class Their_Class_Exception extends Their_Exception {}

class Our_Exception extends Exception {}

class Our_Class extends Their_Class {}
class Our_Class_Exception extends Our_Exception {} // or Their_Class_Exception?
</code>```

Is it better to keep our code as above, and extend our own namespace? The way I see it, we have these alternatives:

Extend our own namespace, catching all possible Their_Class_Exception cases and throwing Our_Class_Exceptions in their place (complete encapsulation, but in general could be a fair amount of rework)

Extend our own namespace, but only don't catch Their_Class_Exception in Our_Class (probably means multiple catch blocks)

Extend Their_Class_Exception (seems counter-intuitive, could introduce bugs by catching the wrong exceptions)

Which of these is the best approach in general, in order to keep our code consistent?

答:
The Liskov Substitution Principle requires that overriding methods in derived classes throw only the exceptions that are thrown by the base class method.

```<code>class Base {
    void f() throws AnException { ... ; }
}

class Derived extends Base {
    void f() throws AnException /* ok */,
                    AnotherException /* LSP violation, not allowed in Java */ {
        ...;
    }
}

class Victim {
    void g(Base b) {
        try {
             b.f();
        }
        catch (AnException e) { ... }
    }
}

Derived d = new Derived();
Victim v = new Victim();
v.g(d); // oops, AnotherException unexpectedly thrown
</code>```

This is ok:

```<code>class AnotherException extends AnException { ... }
class Derived extends Base {
    void f() throws AnException {
        ...
        throw new AnotherException();
    }
}
</code>```

Body:
The Liskov Substitution Principle requires that overriding methods in derived classes throw only the exceptions that are thrown by the base class method.

class Base {
    void f() throws AnException { ... ; }
}

class Derived extends Base {
    void f() throws AnException /* ok */,
                    AnotherException /* LSP violation, not allowed in Java */ {
        ...;
    }
}

class Victim {
    void g(Base b) {
        try {
             b.f();
        }
        catch (AnException e) { ... }
    }
}

Derived d = new Derived();
Victim v = new Victim();
v.g(d); // oops, AnotherException unexpectedly thrown

This is ok:

class AnotherException extends AnException { ... }
class Derived extends Base {
    void f() throws AnException {
        ...
        throw new AnotherException();
    }
}


答:
I myself do not have a website but I have many friends who do. A lot of them use it to show what their web development skills are like , post their resume online, show their projects from inside and outside of class, and host a blog. None of them have all of those, but they all have at least one.

Having a copy of your resume online allows employers whom you haven't contacted to get in contact with you about a job. This is useful because it makes you aware of opportunities you would not otherwise be aware of.

Hosting a blog allows you to express your views in an open forum so they can see your opinions on software development.  This way they can give you more specific questions or see you express yourself in writing (possibly advantageous if you have problems thinking on your feet or have a speech impediment).

Showing your web development skills and past projects is good because it lets them see your code.  This might not seem like much, but it will show them where your strengths lie and they might think your a better fit.  It will also let them see that you have actually worked on some of the things you claim.

In conclusion, it is not necessarily important in the sense of necessary, but it could make the difference in getting or not getting a job later.

Title: Is having your own website important?


答:
How multithreading will not help:

Multiple threads simultaneously reading from the client's disk or simultaneously sending stuff over the network will not help at all, since there most likely exists only one path of communication between the client and the server, the client is most likely reading files off from a single hard-drive, and the files are most likely being written on a single hard-drive on the server. (Even if the server has RAID, it will make some difference, but not much.) On the contrary, as it has already been pointed out, performance will probably degrade, because there will be constant seeking between the files that are being read in parallel on the client, and constant seeking between the files that are being written in parallel on the server. Also, the files may end up being stored badly fragmented on the server.

How multithreading will help:

However, multithreading can help in a different way: with just two threads on the client, the file I/O can be desynchronized from the network I/O. This means that the client can be simultaneously transmitting a chunk of a file while reading the next chunk from its disk.  (The server is already capable of simultaneously writing a chunk of a file on the disk while receiving the next chunk from the network.)  This would greatly speed up the transfer process, because the client would tend to keep either the network channel or the disk channel (whichever is slower) saturated, instead of accessing each one at a time, intermittently. I would guess that every specialized file copy utility out there should be smart enough to do that, but I may be wrong, so if "Robocopy" advertises that they do multi-threaded copy, that's fine, go with that.

EDIT: I corrected the bit I had written about RAID.

EDIT: I corrected the bit about requiring two threads on the server.

I guess the most important thing here, (as it is almost everywhere,) is measurement.  You don't have any control over how these utilities work, so you will only know whether you are doing it the fastest way possible if you measure the throughput to see if it is near the advertised throughput of your disk or network (whichever is smaller.)

Title: Multithreaded file copy


答:
In addition to what has been said, consider:
- There must be a task on the client to create the chunks and another on the server to put them back together as 1 file again. This requires some work.

A good thing about small chunks is that you can re-send pieces of a file if the process fails instead of sending the big file all over.

Consider requesting a 'larger pipe' between your client and the server.

Consider Zipping the big file before sending it (I am not sure if this would help in case of multi media files since they are sometimes are already compressed).

Consider using a commercial file transfer utility such as:

DataExp

Title: Multithreaded file copy


问:
What does C++ do better than D?
I have recently been learning D and am starting to get some sort of familiarity with the language. I know what it offers, I don't yet know how to use everything, and I don't know much about D idioms and so on, but I am learning.

I like D. It is a nice language, being, in some sort of ways, a huge update to C, and done nicely. None of the features seem that "bolted on", but actually quite well thought-out and well-designed.

You will often hear that D is what C++ should have been (I leave the question whether or not that is true to each and everyone to decide themselves in order to avoid unnecessary flame wars). I have also heard from several C++ programmers that they enjoy D much more than C++.

Myself, while I know C, I can not say that I know C++. I would like to hear from someone knowing both C++ and D if they think there is something that C++ does better than D as a language (meaning not the usual "it has more third-party libraries" or "there are more resources" or "more jobs requiring C++ than D exists").

D was designed by some very skilled C++ programmers (Walter Bright and Andrei Alexandrescu, with the help of the D community) to fix many of the issues that C++ had, but was there something that actually didn't get better after all? Something he missed? Something you think wasn't a better solution?

Also, note that I am talking about D 2.0, not D 1.0.

答:
RAII and stack memory usage

D 2.0 doesn't allow RAII to happen on the stack because it removed the value of the scope keyword in allocating class instances on the stack.

You can't do value-type inheritance in D, so effectively that forces you to do a heap allocation for any form of RAII.

That is, unless you use emplace, but that's very painful to use, since you have to allocate the memory by hand. (I have yet to find it practical to use emplace in D.)

Title: What does C++ do better than D?


问:
Algorithm visualization app/plugin
I am looking for an application (or and IDE plugin) that I can use in order to test some algorithms I implement.

Mainly metaheuristics used in TSP, VRP and other optimization problems.

I have already checked some common suggestions on the net, but they're rather too simple for example Zest.

Any suggestions?

答:
See this site for exhaustive resources on algorithm visualization

http://wiki.algoviz.org/AlgovizWiki/

It also depends on what language/toolkits you want to use and plugin. Matplotlip, Gnuplot, Ciro graphics and many many tools can be used.

Title: Algorithm visualization app/plugin


问:
SQL Triggers and when or when not to use them.
When I was originally learning about SQL I was always told, only use triggers if you really need to and opt to use stored procedures instead if possible.

Now unfortunately at the time (a good few years ago) I wasn't as curious and caring about fundamentals as I am now so never did ask to the reason why.

What's the communities opinion in this? Is it just someone's personal preference, or should triggers be avoided (just like cursors) unless there is a good reason for them.

答:
The Wikipedia article on database triggers presents a good overview of what triggers are and when to use them in different databases.

The following discussion is based on SQL Server only.

Using triggers is quite valid when their use is justified. For example, they have good value in auditing (keeping history of data) without requiring explicit procedural code with every CRUD command on every table.

Triggers give you control just before data is changed and just after the data is changed. This allows for:

Auditing as mentioned before

Validation and business security checking if so is desired. Because of this type of control, you can do tasks such as column formatting before and after inserts into database.

I was always told, only use triggers if you really need to and opt to use stored procedures instead if possible.

May be the some of the reasons for this are:

Some functions that triggers used to do in the old days could now be performed in other ways such as updating totals and automatic calculation on a column.

You don't see where the trigger is invoked by examining code alone without knowing they exist. You see their effect when you see the data changes and it is sometimes puzzling to figure out why the change occurred unless you know there is a trigger or more acting on the table(s).

If you use several database controls such as CHECK, RI, Triggers on several table, your transaction detailed flow becomes complex to understand and maintain. You will need to know exactly what happens when. Again, you will need good documentation for this.

Some differences between triggers and non-trigger stored procedures are (amongst others):

A non-trigger stored procedure is like a program that has to be invoked explicitly either from code or from a scheduler or from a batch job, etc. to do its work, whereas a a trigger is a special type of stored procedure that fires as a response of an event rather than be directly executed by the user. The event may be a change of data in a data column for example.

Triggers have types. DDL Triggers and DML Triggers (of types: INSTEAD OF, For, and AFTER)

Non-Trigger Stored procedures can reference any type of object, however, to reference a view, you must use INSTEAD OF triggers.

In SQLServer, you can have any number on non-trigger stored procedures but only 1 INSTEAD OF trigger per table.


问:
Is there a canonical book on design patterns?
I am interested in learning design patterns and would like to know what are considered top tier books in learning this subject.

Is there a book out there that's the de-facto standard for describing best practices, design methodologies, and other helpful information on design patterns? What about that book makes it special?

答:
If you are interested specifically in design patterns in C++ try Modern C++ Design by Andrei Alexandrescu.

This book talks about how to implement commonly used patterns like Factory, Singleton, and Visitor in C++. It also talk about very C++-specific topics such as smart pointers, template metaprogramming, and policy-based class design.

Book author is an authoritative expert, known for his

pioneering work on policy-based design implemented via template metaprogramming. These ideas are articulated in his book Modern C++ Design and were first implemented in his programming library, Loki. He also implemented the "move constructors" concept in his MOJO library. He contributed to the C/C++ Users Journal under the byline "Generic<Programming>"...


问:
Advice for becoming a better programmer
I am a team lead dealing with a programmer with a Bachelors Degree in Computer Science. He has a couple of years of experience and is still a code monkey. Having observed him for quite a while, he is very slow in programming even fizzbuzz style questions. For other stuff, he tries to make do with just tinkering with code or asking for help on programming forums. For scripting stuff he get can barely get by with that, but for our core work of embedded systems using our proprietary toolchains, online forums are not of much help.
As a result, he is either very late or incomplete so that I am forced to assign his work to others.
When confronted he acknowledges all of this, but he does not know what practical steps he needs take on how to improve.

He gets stuck on issues for a long time, being unable to debug.
How to develop his skills to the next level, make him a  problem solver, develop architecture skills and understand large systems, debugging skills and make him plan his tasks? In all his annual performance reviews he has never rated high.
As his team-lead I have to develop a plan of action in consultation with him.

Asking him to quit is always the last option but I would like to avoid that.

答:
When I first got a job out of school I was intimidated by the freakish amount of code. Code which I did not understand to boot.. I am a master procrastinator and I was scared of breaking code as well.. After that went away I developed my skills far beyond what I had imagined.

This is because programming requires tacit knowledge. It's not something that can easily communicated or taught like in the case of an explicit fact. It takes a long time to develop a "know-how" of anything, programming is no exception. I would even say that's the especially in things like programming.

Maybe you can act like his job is on the line (and perhaps it really is). See if that kicks his noggin into gear. If not, then I'm afraid @Oded is right.

Title: Advice for becoming a better programmer


答:
While Oded may be right (it was my first reaction too), you could be dealing with someone who hasn't developed a structured approach to his/her craft yet.

If you have the leeway in your projects (and you should have as he is not contributing much by the sound of it anyway and his work is already mostly done by others), team him up with another developer.

Have them work in a pair-programming setup with the weaker developer at the "controls" of the pc and the stronger developer "helping". Make it obvious to both that it is the lesser developer's responsibility to implement/debug whatever you assign the pair and that the stronger developer is "only" there to help him improve and make him more independent. (And if it is a worry, make sure that the stronger developer doesn't have to worry about her/his annual review productivity goals...)

The stronger developer should not/never take over or go into lecturer mode. Instead (s)he should ask questions like: "what do you need to know to go on?", "what does this tell you?", "how can you find out if?", etc.

For the stronger developer choose one who likes to help others and is able to communicate clearly how (s)he goes about his/her tasks. Also pick one  whose skills are above his, but not too much otherwise the gap will just be too wide and both will get frustrated pretty quickly (albeit for different reasons).

Title: Advice for becoming a better programmer

Body:
I am a team lead dealing with a programmer with a Bachelors Degree in Computer Science. He has a couple of years of experience and is still a code monkey. Having observed him for quite a while, he is very slow in programming even fizzbuzz style questions. For other stuff, he tries to make do with just tinkering with code or asking for help on programming forums. For scripting stuff he get can barely get by with that, but for our core work of embedded systems using our proprietary toolchains, online forums are not of much help.
As a result, he is either very late or incomplete so that I am forced to assign his work to others.
When confronted he acknowledges all of this, but he does not know what practical steps he needs take on how to improve.

He gets stuck on issues for a long time, being unable to debug.
How to develop his skills to the next level, make him a  problem solver, develop architecture skills and understand large systems, debugging skills and make him plan his tasks? In all his annual performance reviews he has never rated high.
As his team-lead I have to develop a plan of action in consultation with him.

Asking him to quit is always the last option but I would like to avoid that.


问:
Multithreaded file copy
There is an utility we have which is used to upload files (and perform other operations on the file) to a network shared location.

The file size tends to vary from a few mb to 500 mb.

A suggestion has come up that we should maybe support multi-threading when uploading the files to the shared location - not required to do it in byte chunks - each thread should pick one file and try to upload.

I am not so sure if multithreading can speed up IO operations like this. Is my hunch valid?

If indeed we are required to build this functionality I was wondering what would be a good design approach for the copy file engine?

Would it make sense to use a tool like robocopy (I read the newer versions support multithreading)?

Edit: Apologies for the delay and missing some vital information.

This utility is built using C# (.Net 2.0) and any future update also has to be using .Net (framework version is not a constraint). The utility is installed on the machines of the users (around 20 all on WinXP). The target share is on Win2k3 server.

Edit 2: have decided to run some tests with a simple application implementing the file upload through TPL. Post this analysis we will decide whether to go ahead or not. Thanks everyone for the help extended.

答:
Doing what you suggest in a naive fashion will kill your throughput, the choke point is disk I/O and not getting files ready.

I'll suggest using one thread that receives files to work with and queues them up for the copy and then keeps a sequential copy going on anything in the queue; your supplier thread is responsible for getting the files read to queue up.  In this way you're not thrashing the file system on the shared drive(s) and you're not doing files one at a time with gaps to prepare the next, you're preparing and sending simultaneously.

Bonus is that there's only one synchronization point at the queue to worry about.

Title: Multithreaded file copy


答:
Instead of implementing parallel upload yourself, you might consider existing protocols and tools. E.g. the ftp protocol and the lftp tool (lftp can transfer several files in parallel).

So it is probably much easier and more robust to use lftp scripts or control lftp from your application instead of implementing everything from scratch.

Title: Multithreaded file copy


问:
Help in deciding on approach/language for Web App
I have a personal project - a web application I wanted to develop - but I'm confused on which route to take.

I know a little programming, and have dabbled in Pascal, C, HTML, CSS and Actionscript (for Flash). And I don't mind learning a new language (PHP, Python, mySQL, etc). I am not under any time constraint.

About the App:

The best I can describe it is as a kind of expert system (but not AI) that needs to find the best workflow for a process, given a set of initial and final parameters. E.g. a 'capsule' of data must pass through many 'tools' or 'environments' to reach a desired output - something like a very complicated car wash.

Let's say there are many tools that can be used at various stages in the process. I have estimated there are at least 500 tools as of now, and it is bound to grow in the future as newer tools are introduced. Existing tools will also have version updates.

Each tool, on average, has at least 100 properties that define the tool. Some of them have as high as 1000 unique properties. Some of these tools are linked to each other - e.g if one tool is selected, there are only n tools that can correspond to it for the next step in the process. I also have the problem of 'matching' the tools for analysis. E.g. Tool A might have only three fixed rpms - 100, 200 and 500, but Tool B might have rpms from 20 to 2000. I'm not sure how I can construct a database without spelling out each number, as in the example above.

The total number of tools needed for the process can be defined at the beginning, however, it will change as the application becomes more complex in the future. I plan to address every contingency in the process. The idea is - if the user inputs the initial parameters and the desired outcome (another set of parameters), the app must find the 'best' path - sort of like a decision tree. The best path can be the fastest, cheapest, etc. I would like the user to choose what is best for him/her.

Unfortunately, parameters might change, relationships might change (but not regularly) - the 'rules' I will be using might be revised for better accuracy in prediction.

I also need to track each user's path and solutions' for future reference (but no personal details except username and email address for logging in). Maybe when the app is up and running, I'd like to make it more democratic, with users contributing to refining the logic/rules involved.

If possible, I would also like the app to output a graphical flowchart at the end showing the workflow with all tools grouped in an easy to understand layout.

My questions:

Will the app be better served with a relational DB like mySQL or an Object database?

Can I make this work with PHP+AJAX+CSS+mySQL? Is there a better alternative? I would prefer free programs - I intend this app to be free, but I might want to make it commercial later on. I need this program to run on any device that can connect to the internet, including tablets and mobiles, etc.

Do I need a framework? CodeIgniter/Symfony vs Django/ZOPE?
Which is the best way for me to begin, considering I might have to scale it up in the future - with more data, more users simultaneously logged in, and more algorithms? I don't want to change solutions later on - if I have to invest time and effort I'd rather do it the right way from the beginning.

I would appreciate any help in getting started. Thank you in advance.

答:
Will the app be better served with a relational DB like mySQL or an Object database?

It doesn't matter.  They both work.

Since you need to get something finished pick one that you know.

Can I make this work with PHP+AJAX+CSS+mySQL?

Yes.  Since you need to get something finished pick one that you know.

Is there a better alternative?

Yes.  But you need to get something finished, so don't start learning new things now.

Do I need a framework? CodeIgniter/Symfony vs Django/ZOPE?

Yes.  You need a framework.

Which is the best way for me to begin,

Use tools you know.

I might have to scale it up in the future... I don't want to change solutions later on

Bad idea.

I'd rather do it the right way from the beginning.

You really can't.  In two years, the technology will have changed, and every choice you make today will have a better choice two years from now.

Just get something to work as soon as you can.  Get to market is more important than anything else.

You'll need to to technology refreshes anyway, so the very idea of "right the first time" doesn't apply.


问:
What is the best practice for refactoring a static method in order to make it more testable?
Let's say you have a static method that looks something like this:

```<code>public static bool Foo()
{
    var bar = new Bar();
    //do some stuff here
}
</code>```

This method as it stands can be a real headache to unit test.

What is the best practice to refactor this so that it can be testable, without turning it into an instance method or changing the method signature?

答:
What is the point of Foo? What does it do? What is a Bar?

It seems from your question that Bar is a class that introduces side effects, or Bar is a resource.

Either way, in both circumstances, without changing the method signature, you're hooped without going into the ill-fated world of pre-processor directives (#if test var bar = FakeBar(); // = bad).

If Bar is a class that introduces side effects: without injecting that dependency or returning whatever it affects, you're in trouble.

If it's a resource (Stream, DBConnection, etc.) then the only realistic options I can see is to:

Extract out an interface and take an
IBar as a parameter like @azheglov
says

Create a fake/stub that
inherits from Bar and pass that as a
parameter

What it boils down to is that you're likely going to have to change the method signature, reduce side-effects and not create hidden dependencies if you want to make it easier to test.


答:
I had a developer on my team who was intimidated by the new code and new language (this was in an environment where you opened a position for a general skill set and eventually got someone assigned, with no real interviewing or input into the selection). He would always procrastinate, and when I confronted him about it, he said something to the effect of "I'm still learning this, so every task is a pain in the ass".

My response: "I'm sorry, but the only way you get past that is to have a sore ass for a while."

As for your problem developer, I would agree with the other responders about hands-on mentoring. Having that second person there will help teach him the processes and ways to think, not to mention taking away the chance to let the mind (and web browser) wander.

Title: Advice for becoming a better programmer


答:
This is no different from any manager, dealing with someone who's not earning his keep. It sounds to me like you haven't even talked to him yet. Make that your first priority.

When you do talk to him, don't say what you've said here. It's all vague and accusatory.

Be prepared with a list of specific examples of what he has done wrong or where he has not performed. Be ready to help him. Find out what his concerns are and try to find training courses to solve the problems. As him if he'd be more comfortable pairing with more senior developers for a while. Review his code personally and offer constructive criticism.

Most importantly, do not give up on him yet.

If this doesn't work then you need to take a more aggressive approach. Find ways of measuring the problem. Be very specific about where he falls short of your expectations, given his pay grade, and where he needs to be in 6 months from now. Follow up with 1-to-1s at least once a month (you should do this anyway, for the whole team).

If he doesn't succeed in meeting the targets you've set for him in 6 months, then and only then should you suggest he either take a pay-cut or leave.

One more thing: involve your manager from the beginning. Tell him that you want his advice, not for him to get involved personally. But make sure he's aware of the situation and everything you do, so that you have backing should the employee turn on you, through HR or tribunals or whatever.

Title: Advice for becoming a better programmer


问:
If I want my client program to be free software, do I also have to release the server software as free software?
Imagine I've written a game and I want to make it free software. Am I also required to make the game server software free software because the game uses it to connect to other players to play against?

Imagine I've written a stock ticker and I wish to make it free software. Can I charge for the subscription to the stock information, even though the software serves little purpose without paying for such a subscription?

I'm also interested in revenue sources for free software that go beyond charging for distribution or support, that counteract one person purchasing your software and then distributing it themselves and undercutting your prices.

答:
No, there are no free-software licenses that I am aware of that would impose such a requirement. If there were you wouldn't be under any obligation to use that license but instead pick one of the others.

The client and server could even be under different licenses if you like.

Also, if no license suited your needs, you are free to create your own. As the copyright owner, you are free to release your software as you see fit.


问:
How did you pick up the art of solving real life problems? Can you shortcut the process?
I have been coding on and off on C and to a certain extent on C++ since my college days(2003), but I never had the opportunity to work with a hard core programming responsibility, which could have taught me "industrial grade coding" better and what is needed really for solving real life problems. To me it looks like I had been coding naive codes, like linked lists, binary trees, and Dijkstra and Kruskal's Algos all these years.

Additionally I feel my academic experience just taught me the language not how to solve real life problems.

So my question is:

So how did you pick up the art of solving real life problems?

Any way to shortcut the process?

Is it only my education or in general everyone coding education sucked big time?

How often do you have to learn "new things" to solve a problem in a better way.

For example, though I have used delegates and events for simple windows programming, but my current problem will require a much more in depth knowledge of it, plus I have forgotten most of it. So its as good as new for me. I will have to learn threaded programming and multicore programming. So it gives me an impression-"heck then what did I learn really all these years?"

答:
I believe that participating to some C++ free software project with a living community will increase your skills.

I also think that learning a very different language (like Ocaml, Haskell, Common Lisp, Scheme, Clojure, Smalltalk) will change your way of thinking (even on plain C++ code).

And first-class books or courses like SICP could learn you a lot.

You could even consider writing some GCC extensions in MELT which you would find useful. (MELT is a high-level domain specific language, with a Lispy look, to extend the GCC compiler; I am the main author of MELT)


答:
Talking with him is imperative.

Understanding if he cares about his programming work, if he is interested in it, what is his motivation? Is it a matter of lack of self-confidence??

If he is interested, perhaps buying him the SICP book might help him.

Title: Advice for becoming a better programmer


答:
If you really intend to maximize the potential power of C++ then just concentrate on its core applications. Decide for yourself whether you want to utilize your skills in IT or like to do some serious computer science work.Remember whatever you have studied and learnt about programming would never go wasted!Also decide whether you are into writing low-level or system software or aiming for application software. Programming micro-controllers and embedded devices another area where you can also look into.

Visit the inventor's website and check it for yourself, what applications would interest you to look into further either for enhancement or criticism. Here is the link that may interest and can benefit your existing skills further: http://www.stroustrup.com/applications.html

Also look into the potential advantages of other languages like Java for example and try to come up with an idea or product that can benefit you and others.

Title: How did you pick up the art of solving real life problems? Can you shortcut the process?

Body:
I have been coding on and off on C and to a certain extent on C++ since my college days(2003), but I never had the opportunity to work with a hard core programming responsibility, which could have taught me "industrial grade coding" better and what is needed really for solving real life problems. To me it looks like I had been coding naive codes, like linked lists, binary trees, and Dijkstra and Kruskal's Algos all these years.

Additionally I feel my academic experience just taught me the language not how to solve real life problems.

So my question is:

So how did you pick up the art of solving real life problems?

Any way to shortcut the process?

Is it only my education or in general everyone coding education sucked big time?

How often do you have to learn "new things" to solve a problem in a better way.

For example, though I have used delegates and events for simple windows programming, but my current problem will require a much more in depth knowledge of it, plus I have forgotten most of it. So its as good as new for me. I will have to learn threaded programming and multicore programming. So it gives me an impression-"heck then what did I learn really all these years?"


问:
Is it necessary to read every single byte to check if a copied file is identical to the original?
I recently learned of a program called Total Commander. It's a Windows Explorer replacement and has its own stuff to copy files. To check whether the files are identical, instead of calculation a CRC, it literally checks every single byte, one at a time, on both the original and the copy.

My question is: Is this necessary? Can CRC or any other such technique go wrong? Should you, as a programmer, try and implement this perfect but slow system, or is it too extreme?

答:
The only perfect way to check for identical files is byte for byte comparison. Another way to be a fair approximation is to calculate a hash such as MD5 for the files and compare those. It's possible there could be a hash collision but not very likely.

I would imagine the byte for byte comparison would be faster than computing the hash on both files at the time you are doing the comparison. However, if your application pre-calculates the hash and stores meta-data about your files, comparing hashes will be significantly faster.

CRC is probably not the way to go as it is just an error detection mechanism, not a hash. (or a poor hash with lots of possible collisions)


答:
To be 100% certain two files are identical, you really do need to check the bytes.

Why? Hash collisions, thats why! Depending on the algorithm used for hashing, collision might be more or less probable, but it is possible none the less. Following these steps:

Check file sizes

Check mime types

Check hash

Check a few random offsets and compare bits

Will give you a very high guarantee of certainty that the two files are the same, however there is a very (extremely) small chance that you have a collision on your hands. The choice of how far you want to go with your comparisons will be dictated by the situation.

Title: Is it necessary to read every single byte to check if a copied file is identical to the original?

Body:
I recently learned of a program called Total Commander. It's a Windows Explorer replacement and has its own stuff to copy files. To check whether the files are identical, instead of calculation a CRC, it literally checks every single byte, one at a time, on both the original and the copy.

My question is: Is this necessary? Can CRC or any other such technique go wrong? Should you, as a programmer, try and implement this perfect but slow system, or is it too extreme?


答: well, he needs to read a few programming books and watch tutorials online, i.e. stanford classes. He obviously doesn't have the required knowledge to do programming.</p>

Title: Advice for becoming a better programmer


答:
Here's another way to think about it.

If there is no possibility that two different files have the same CRC, then by extension it means that every file can be represented by a unique CRC.If the CRC was smaller than the original file then it would represent a form of lossless compression. If not, you would do just as well to compare the original files since you'd be comparing the same number of bytes.

In theory you could use lossless compression of both sides of the comparison to reduce the number of bytes necessary in the comparison, but it is a fools errand because you'd waste more cycles and have to read every byte of both files to do the compression. That is, to encode every byte (and it's order) in a lossless compression scheme you'd have to first read it in and plug it into the algorithm, right? Game over.

Here's an analogy:

If you wanted a way to quickly determine whether two printed documents were identical without comparing letter by letter, you could compare the count of letters on each line of the documents. If the counts all matched, the odds improve substantially that the documents are identical, however no one would argue that you could be certain that every letter was the same using this approach.


问:
Is it good design to require class users to use Rvalue reference?
I have the following situation where I have a base class and multiple polymorphics derived classes:

```<code>#include <iostream>

class Base {
    public:
        virtual void foo() = 0;
};

class Derived1 : public Base {
    public:
        virtual void foo() { std::cout << "Derived1::foo()\n"; };
};

class Derived2 : public Base {
    public:
        virtual void foo() { std::cout << "Derived2::foo()\n"; };
};
</code>```

I want to make sure that my class users will always use the base class to use object of this hierachy. I got the idea to provide a factory methods returning the good object. From now, the methods look like this:

```<code>Base&& make_base(int i) {
    if(i == 1)
        return Derived1();
    else
        return Derived2();
}
</code>```

The client can then use it that way:

```<code>int main() {
    Base&& b = make_base(1);
    b.foo();
}
</code>```

Assuming C++03 compatibility is not needed, is this considered a good practice?

Edit

AS DeadMG pointed out, my initial make_base has undefined behaviour. If I can't require clients to only use Base class, I wish I could recommend it. Is this kind of factory a better idea?

```<code>#include <type_traits>
#include <string>

template <class Derived>
Derived make_base() {
    static_assert(
        std::is_base_of<Base, Derived>::value,
        "'Derived' must derived from 'Base'.");

    // Default behaviour
    return Derived();
}

template <>
Derived2 make_base<Derived2>() {
    // Specialise behaviour
    // Do something
    return Derived2();
}

int main() {
    auto b = make_base<std::string>(); // Error, 'Derived' must derived from 'Base'.
    Base&& b1 = make_base<Derived1>(); // call make_base()
    Base&& b2 = make_base<Derived2>(); // call make_base<Derived2>()
    b1.foo();
    b2.foo();
}
</code>```

答:
No, this is undefined behaviour. The temporary variable will not have it's lifetime extended.

In addition, I'd say it's a code smell to not offer Derived1 and Derived2. Only accessing through the base class is something you only do if the derived class cannot be known at compile-time.


答:
My advice: keep it simple. Approach every task by splitting it up into pieces and then combine those pieces together in an designed way. (ie design the pieces knowing they'll be working together).

For daily work, real-life problems are always a mixture of all kinds of things. Having problem getting your app to work, maybe its the code... but maybe its the OS, or the DB, or the network. Understanding the basics of all those things means you'll have a much better chance of finding and fixing the problem than if you were totally clueless about how those parts work. This does not mean you have to be an expert, just than you have some understanding. Over time these little bits together will become a much greater resource to you than you expect.

(eg. a real life problem that affected a previous company big time. Performance was bad, there was nothing wrong with the code, but all the devs working on it assumed there was, in the end the problem was shown to be a lack of correct indexes on the DB. Another company had performance problems too, we implemented caches, scoured the code, and determined in the end that it was MTS. Replaced that section with a fixed-config DCOM connection and problem was fixed).

Education v real-life. Yup, it's like that for everyone. I used to see graduates come out and start work with an attitude that they knew everything because they'd written 10,000 line project in their final year and they knew all there was to know. Dump them in front of our 10,000,000 line project and they suddenly realised education was simply a stepping stone to get to the bottom rung of real life work. Don't stress about it, just learn what you need as you go along. FSM knows that everything you know today will be obsoleted by idiots in our 'beloved' industry soon enough anyway.


答:
The original book, "Design Patterns: Elements of Reusable Object Oriented Software" is a good resource for object oriented design patterns.

There exist many other books out there.  When I need a quick reference and my GoF book is out of reach, Wikipedia normally has enough information on those and other design patterns.

Those are the primary ones I use.  It should also be noted that people have started applying the pattern format to other things, such as project management.

If you want to know about antipatterns, which you should be aware of, there's two books, "AntiPatterns: Refactoring Software, Architectures, and Projects in Crisis" as well as "Refactoring: Improving the Design of Existing Code" which has a section on code smells, another name for antipatterns.

Title: Is there a canonical book on design patterns?

Body:
I am interested in learning design patterns and would like to know what are considered top tier books in learning this subject.

Is there a book out there that's the de-facto standard for describing best practices, design methodologies, and other helpful information on design patterns? What about that book makes it special?


答:
C++ is older than Java and C#, but is still quite good for serious apps, especially desktop apps.
If I can just pass along some of my experience (not limited to C++):

There are lots of 3rd-party packages, controls, etc. These are two-edged swords. They can give you a lot of features and standardization. The downside is they can upgrade you into trouble (on the theory that you can easily recode your app), or the vendor can go up in smoke.

OOP is also a two-edged sword. When you need it, it's great. OTOH if you just follow the "religion", you can end up with a monster albatross around your neck. People are encouraged to create more and more classes, intertwined with notifications, delegates, and every hot buzzword in the community. For any given application purpose, there are infinitely many possible code bases that will serve it. Some of them are optimal in maintainability, code size, performance, any dimension of importance. Many many more are wildly non-optimal, and the OOP "best practice", in my experience, leads people strongly toward the latter.

The people who build these tools are justifiably proud of their "power", and they expect that the users of the tools will share that respect and use them to best effect, i.e. sparingly.
Unfortunately, the mindset of a tool user can be just the opposite. They may think "This thing is so great, I can use it here, there, and everywhere. Then I'll be really productive."
See the disconnect?
That's how monstrosities get built.

(As an extreme, and amusing, example, I heard a story once of a Cobol programmer who wrote a SORT (of a dataset) inside an inner loop, because it was "only a single line of code, so it must be fast".)


问:
How big a problem is Qt's cross-platform GUI?
I read something on this site saying that Qt's GUI being a big problem that stop programmers from using Qt. They say the problem is that the GUI don't look native, and they are inconsistent. I personally use Windows, and from what I can see (some tree view and table view), they look pretty good and very native-looking too. So I want to know what they are like on Mac, and what are the differences? and what differences there should be between ordinary Mac apps and ordinary Windows apps in terms of user experience?</p>

答:
Mac applications with a GUI are usually written in Objective-C and use AppKit, which provides a UI that's consistent and similar in every application (e.g. short cuts are the same everywhere, the same controls have the same size, dialogs are the same, menu bar is similar in all applications). This means that if you know how to use TextEdit, you automatically know how to use TextMate, and if you know how to use iTunes, you can find your way easily in iPhoto and Aperture.

Qt breaks this consistency by using controls that aren't native. For example, using windows instead of sheets to show an error message, or by not using proxy icons in title bars of windows, or using them wrongly. I do use one Qt app on my Mac, which is the GUI front-end of Doxygen. It works but the first time I opened it I immediately saw it wasn't a so called "Cocoa" app (one that uses AppKit as described above).

If you want to have a cross-platform app with a GUI consistent on both platforms, rewrite the GUI for each platform. You can use the same back-end.

If you want to know the exact differences in UI between Windows and Mac OS X, refer to their human interface guidelines. (Mac OS X HIG and Windows UEIG.)

tl;dr: If you are writing a little tool like a front-end for a command line tool, or a simple calculator, go on and use Qt. If you're writing a big application like an IDE, a Terminal emulator or a Twitter client you should rewrite the GUI completely for every platform to be consistent.


答:
On windows it's native - themes and everything. Rather more native than a lot of the OS, which when you dig down into some of the management screens goes NT on you.

On Linux it is the native toolkit for KDE and looks ok on Gnome

Don't use Mac - but Mac users do get very touchy about everything looking exactly as Apple says it should

Title: How big a problem is Qt's cross-platform GUI?

Body: I read something on this site saying that Qt's GUI being a big problem that stop programmers from using Qt. They say the problem is that the GUI don't look native, and they are inconsistent. I personally use Windows, and from what I can see (some tree view and table view), they look pretty good and very native-looking too. So I want to know what they are like on Mac, and what are the differences? and what differences there should be between ordinary Mac apps and ordinary Windows apps in terms of user experience?</p>


答:
It all depends on where the limiting factor is.

Multithreading could help if there are round trip delays or other gaps in transmission, and the threads help fill in the gaps.

Multithreading could hurt if it has the effect of causing your disk to rattle back and forth, trying to keep all the threads supplied with data.

etc.

Title: Multithreaded file copy


答:
Lets say you need to push data to a 3rd party system in near real time. Your table contains 950 gigabytes of data, so it's too big to just simply push the entire table to the 3rd party app.

Instead you accumulate changes in a queue. Some external program will then periodically push out small batches of queued data.

The system has over 2000 stored procedures. You also know that tons of sql exists in the source code. To guarantee the queue is populated correctly you'll have to search through all the stored procs and code and hope you don't miss anything.

Instead you can put a trigger on the table to keep the queue updated. Guaranteed not to miss anything. One central location. Performance penalty? Not really because the hit of populating the queue cannot be avoided whether it's by trigger, or external.

In this scenario I would say not using a trigger is a bad design choice. If later you want to use a new method of pushing data (say the queue isn't working out) and the interface changes you are protected if you use the trigger. Triggers are often the best choice.  Don't listen to dogmatic anti-trigger fanboys.

Title: SQL Triggers and when or when not to use them.

Body:
When I was originally learning about SQL I was always told, only use triggers if you really need to and opt to use stored procedures instead if possible.

Now unfortunately at the time (a good few years ago) I wasn't as curious and caring about fundamentals as I am now so never did ask to the reason why.

What's the communities opinion in this? Is it just someone's personal preference, or should triggers be avoided (just like cursors) unless there is a good reason for them.


问:
How do I learn Python from zero to web development?
I am looking into learning Python for web development.

Assuming I already have some basic web development experience with Java (JSP/Servlets), I'm already familiar with web design (HTML, CSS, JS), basic programming concepts and that I am completely new to Python, how do I go about learning Python in a structured manner that will eventually lead me to web development with Python and Django?

I'm not in a hurry to make web applications in Python so I really want to learn it thoroughly so as not to leave any gaps in my knowledge of the technologies involving web development in Python. Are there any books, resource or techniques to help me in my endeavor? In what order should I do/read them?

UPDATE:

When I say learning in a structured manner, I mean starting out from the basics then learning the advanced stuff without leaving some of the important details/features that Python has to offer. I want to know how to apply the things that I already know in programming to Python.

答:
http://learnpythonthehardway.org

Is a free online book that contains a series of 52 lessons in python.

By starting on lesson 1 and working through to lesson 52 you should learn enough to consider yourself a competent programmer in the python language.

Each lesson has code examples that you should take the time to type in and RUN then ALTER to see how that changes the results.

It is that process by which most people that I know generally learn programming.