问:
Would md5 hashes allow detection of synced files?
We have to develop our own file management system in Java web application. We need to sync files between our main server and client severs and find out whether all the client server has all the latest version of files.

Our files are in pdf, doc and xls format they changes every now and then as and when it is required.

What we are thinking of using MD5 checksum to find out hashcode of files on Main server and store it in database. Same would be there in Client Servers database. After comparing records on database we would come to know whether client servers are synced or not.

Please suggest if there are any better ways to do the same.

答:
MD5 is said to by quite slow (see below). The MD5 digest is rather long. I'd recommend modification time, size and CRC-32 checksum for file comparisons. A discussion on CRC-32 is here. As its name implies, CRC-32 has 32-bit hash values. A Java implementation is available in java.util.zip.CRC32

Edit:
The speed advantage of CRC-32 vs. MD5 is smaller than I've expected. CRC-32 needs some 20% less time compared to MD5.

I used the following Java code to find the difference (and demo the usage of both methods):

```<code>import java.security.*;
import java.util.Random;
import java.util.zip.CRC32;

public class HashBench {

    @SuppressWarnings("unused")
    public static void main(String[] args) throws Exception {
        int noOfLoopIterations = 100 * 1000;
        int bytesInMessageBuffer = 100 * 1024;

        byte randomByteBuffer[] = new byte[bytesInMessageBuffer];
        byte md5Digest[];
        MessageDigest md5;
        CRC32 crc;
        long crcValue;
        long startTime;
        long elapsedTime;

        new Random().nextBytes(randomByteBuffer);

        //  MD5 benchmark
        o("Starting MD5 benchmark ...(" + bytesInMessageBuffer/1024 + "KByte messages)");
        md5 = MessageDigest.getInstance("MD5");

        startTime = System.nanoTime();
        for (int i = 1; i < noOfLoopIterations; i++)
        {
            md5Digest = md5.digest(randomByteBuffer);
        }
        showElapsed(noOfLoopIterations, startTime);

        //  CRC-32 benchmark
        o("Starting CRC-32 benchmark ... (" + bytesInMessageBuffer/1024 + "KByte messages)");
        crc = new CRC32();

        startTime = System.nanoTime();
        for (int i = 1; i < noOfLoopIterations; i++)
        {
            crc.reset();
            crc.update(randomByteBuffer);
            crcValue = crc.getValue();
        }
        showElapsed(noOfLoopIterations, startTime);

        o("Ciao!");
    }

    private static void showElapsed(int noOfLoopIterations, long startTime) {
        long elapsedTime;
        elapsedTime = System.nanoTime() - startTime;
        o("Elapsed time:    " + num(elapsedTime / 1000000000.0) + "s for " + String.format("%1$,.0f", 1.0 * noOfLoopIterations) + " loops");
        o("Time per digest: " + num(elapsedTime / (1000000.0 * noOfLoopIterations)) + "ms");
        o("");
    }

    private static void o(String s) {
        System.out.println(s);
    }

    private static String num(double x) {
        return String.format("%1$,.2f", x);
    }

}
</code>```

The result:

```<code>Starting MD5 benchmark ...(100KByte messages)
Elapsed time:    28,94s for 100.000 loops
Time per digest: 0,29ms

Starting CRC-32 benchmark ... (100KByte messages)
Elapsed time:    23,89s for 100.000 loops
Time per digest: 0,24ms
</code>```

To avoid the influence of disk caching and other external effects, I just fill a byte array with random values. The benchmark executes the hash/checksum calculation repeatedly.

Conclusion: Calculation speed is not a convincing reason in this case.

Body:
MD5 is said to by quite slow (see below). The MD5 digest is rather long. I'd recommend modification time, size and CRC-32 checksum for file comparisons. A discussion on CRC-32 is here. As its name implies, CRC-32 has 32-bit hash values. A Java implementation is available in java.util.zip.CRC32

Edit:
The speed advantage of CRC-32 vs. MD5 is smaller than I've expected. CRC-32 needs some 20% less time compared to MD5.

I used the following Java code to find the difference (and demo the usage of both methods):

import java.security.*;
import java.util.Random;
import java.util.zip.CRC32;

public class HashBench {

    @SuppressWarnings("unused")
    public static void main(String[] args) throws Exception {
        int noOfLoopIterations = 100 * 1000;
        int bytesInMessageBuffer = 100 * 1024;

        byte randomByteBuffer[] = new byte[bytesInMessageBuffer];
        byte md5Digest[];
        MessageDigest md5;
        CRC32 crc;
        long crcValue;
        long startTime;
        long elapsedTime;

        new Random().nextBytes(randomByteBuffer);

        //  MD5 benchmark
        o("Starting MD5 benchmark ...(" + bytesInMessageBuffer/1024 + "KByte messages)");
        md5 = MessageDigest.getInstance("MD5");

        startTime = System.nanoTime();
        for (int i = 1; i < noOfLoopIterations; i++)
        {
            md5Digest = md5.digest(randomByteBuffer);
        }
        showElapsed(noOfLoopIterations, startTime);

        //  CRC-32 benchmark
        o("Starting CRC-32 benchmark ... (" + bytesInMessageBuffer/1024 + "KByte messages)");
        crc = new CRC32();

        startTime = System.nanoTime();
        for (int i = 1; i < noOfLoopIterations; i++)
        {
            crc.reset();
            crc.update(randomByteBuffer);
            crcValue = crc.getValue();
        }
        showElapsed(noOfLoopIterations, startTime);

        o("Ciao!");
    }

    private static void showElapsed(int noOfLoopIterations, long startTime) {
        long elapsedTime;
        elapsedTime = System.nanoTime() - startTime;
        o("Elapsed time:    " + num(elapsedTime / 1000000000.0) + "s for " + String.format("%1$,.0f", 1.0 * noOfLoopIterations) + " loops");
        o("Time per digest: " + num(elapsedTime / (1000000.0 * noOfLoopIterations)) + "ms");
        o("");
    }

    private static void o(String s) {
        System.out.println(s);
    }

    private static String num(double x) {
        return String.format("%1$,.2f", x);
    }

}

The result:

Starting MD5 benchmark ...(100KByte messages)
Elapsed time:    28,94s for 100.000 loops
Time per digest: 0,29ms

Starting CRC-32 benchmark ... (100KByte messages)
Elapsed time:    23,89s for 100.000 loops
Time per digest: 0,24ms

To avoid the influence of disk caching and other external effects, I just fill a byte array with random values. The benchmark executes the hash/checksum calculation repeatedly.

Conclusion: Calculation speed is not a convincing reason in this case.


问:
We are spending more time implementing functional test than implementing the system itself, is this normal?
Basically, we have three main projects, two of them are web services, and the other is a web application. While I'm satisfied with covering as much as we can of our web services with functional tests (all three projects have their proper unit tests), functional tests for the web application are taking a lot of developer time to get implemented. By a lot I mean two times, or sometimes more, the time that takes to implement the functionality being tested with unit test included.

The manager policy is to test every single functionality we add, even if is not business critical (i.e a new C.R.U.D).

I do agree with testing all of the web services functionality, because is hard to manually test them, and also, this tests run fast and don't take to much to implement.

So, what's the value in spending more time writing functional test, than writing system code, unit test and fixing QA tikets? Is this normal? Shouldn't we be writing functional tests only for critical functionality and let QA to do regression tests over no critical functionality?

Note: we are not developing medical software or NASA software or nothing that critical.

答:
More than 2 times ... seems a bit much to me. You might want to analyze the reasons for this, they could include:

bad tool support for creation and maintenance of the tests

contracts of the web services is not sufficiently described in the design. Developers need to work out the contracts while testing, which is usually a time consuming alignment process.

Talk to your developers.

Assuming you are developing in sprints, having these functional tests if just part of the sprint. It ain't done without these tests. If you don't have it, your time for integration testing after the development phase might double.

ContentLicense: CC BY-SA 3.0


问:
Customer ask firmware source file
Recently my company was asked by a customer to develop a control board that includes firmware and PCB layout development. After finishing development the customer will buy the control boards at certain quantity every year.

We are now at making contract stage, the customer is wanting my company to handover both the firmware source files and PCB layout files after development. We could give the PCB layout files but we don't want to give the firmware files as we're worried about they may find third party to produce the control board so that we may have nothing.

However from the customer point of view, they are concerned that they could be in risk situation if my company for some reason stops selling the boards. Is there any "common" practice to make contract regarding this?

答: Because this is a rather common concern, it has a convenient solution called Source Code Escrow. The idea is to deposit and periodically update the sources with a third party which will hand them over to the customer of your company should your company go into bankruptcy.

Title: Customer ask firmware source file


答:
You're writing a contract.  Have the contract define the terms under which they could use the source.  Include a term that says that they're only able to take the source to a 3rd party on the condition that your company no longer exists and/or refuses to do business with them.  To give yourself extra protection, you could put the code in escrow and have a 3rd party hold it until they are allowed to have it.

This is not a technical problem - leave the details to the business and legal departments.

Title: Customer ask firmware source file

Body:
Recently my company was asked by a customer to develop a control board that includes firmware and PCB layout development. After finishing development the customer will buy the control boards at certain quantity every year.

We are now at making contract stage, the customer is wanting my company to handover both the firmware source files and PCB layout files after development. We could give the PCB layout files but we don't want to give the firmware files as we're worried about they may find third party to produce the control board so that we may have nothing.

However from the customer point of view, they are concerned that they could be in risk situation if my company for some reason stops selling the boards. Is there any "common" practice to make contract regarding this?


答:
The most common practice in this case is to place the software and PCB's in Escrow - essentially an independent third party hold copies of the files and releases when certain conditions are met (or not) by the various parties. The Escrow agreement defines the conditions that allow the release of the files (typically bankruptcy, contractual failure etc).

There are companies that specialise in this, however is is something that your lawyers can do on your behalf.  In more than one case where I have worked, the files have been encrypted and sent to the customer with each update. The encryption key has been lodged with the companies lawyers (in some cases ours, in some cases the customers), along with the agreement of when the key is to be released to the customer, and what the customer is allowed to do with those files. (e.g. - the customer can use the files to continue to manufacture products we cannot supply, but it does not own the IP, and therefore may not sell the source or use it for any other purpose. The advantage of this is there is you do not get a bill from the lawyers every time you release an updated product.

The Escrow agreement is critical and must be drawn up very carefully. It is not something that you should attempt without legal advice.

Title: Customer ask firmware source file


问:
Effective design analysis
Our team redesigns the legacy software and there are a lot of design meetings going on. Many of them are not very effective and lack good analysis of the proposed design. Is there any formal way to approach this kind of discussions to make them more effective? I heard about SWOT for example, but afaik it is used more for business projects.</p>

答:
I assume you want the redesigned system to behave exactly as the legacy system, at least in the beginning. This means to need to capture the concepts and behavior of the legacy system first as precisely as possible. I recommend to create architecture models and use them for discussion. Everybody should have a good understanding of the modeling language you use.

Check out the methology we use at SAP, which is described in this 2 part blog post:
http://scn.sap.com/people/bernhard.groene/blog/2008/01/09/how-to-communicate-architecture-technical-architecture-modeling-at-sap-part-1

Don't just create the block diagrams. I recommend to start with entity relationship diagrams and also add definitions of the entities. Then do the block diagrams - don't forget to describe protocol boundaries, and add a short description what each block does. Last thing should be one of the dynamic diagrams. Use the other diagram types if you need them.

What you will experience are lively discussions e.g. if there is a 1:* or a 1:0..* relationship between two entities. Or you will learn a lot about dependencies between components if you try to figure out where you could establish a system boundary, and so on.

If you use vague powerpoint drawings of your legacy system architecture, everybody will have his own interpretation and understanding, so you won't make much progress, and your new system will probably not reflect the legacy system.

Title: Effective design analysis


问:
Velocity does not plateau over time, why?
I have plotted my team burn up chart and its velocity per iteration. To me it looks really bad (velocity fluctuates a lot). What should I be looking for to diagnose the root cause of this behaviour?

答:
Additional potential cause: during the later sprints, you are paying off technical debt from earlier sprints.

E.g. you have a management demo after sprint 3 and need to show happy-day scenario. To make it, you do the coding without error handling, without translation support, without unit testing. This is a valid decision, you just need to be aware of the consequences.

So later you add all the nice stuff like excation handling framework, translation support, unit test framework and so on. Your existing coding from the 1st 3 sprints does not have use that yet, so it needs to be updated. This effort slows down the value creation during the later sprints.


答:
Mihai Danila's suggestion of applying Fowler's StranglerApplication in replacing your Legacy app is in my opinion the best way of replacing any legacy system.

I blogged a couple of years ago about never rewrite a system. Basically like Strangler Application it is about inserting transparent facades in front of  interfaces to the existing systems and incrementally grow this redirection to the new system until the old system is no longer in use.

And never re-writing the whole system in one go as that will take way too long with no business benefit at least for a long while. You need to deliver business value along with replacing the existing legacy system.

As for the actual estimations, you can apply many time consuming in-detail estimations using various techniques. However they will all just be as wrong as a finger in the air estimate, just now wrong in detail as Dan North would say.

Create a few prioritised epics of what features are needed initially, break them up further if needed but not much more. Then just throw some order of magnitude estimates of various parts of the job (t-shirt sizes, 1-10-100 story points or days or whatever). Applying StranglerApplication and then deliver some tracer bullet stories to get a much better idea of the actual complexity and viability of the project.

Title: Effective design analysis

Body: Our team redesigns the legacy software and there are a lot of design meetings going on. Many of them are not very effective and lack good analysis of the proposed design. Is there any formal way to approach this kind of discussions to make them more effective? I heard about SWOT for example, but afaik it is used more for business projects.</p>


答:
Functional tests are very important. Yes, they take time to write but if you are writing the right functional tests, they will be more than worth it.

There are a few good reasons to do automated functional tests on an application.

When a new feature is added to your web site, it let's you know right away if changes made for that new feature break any other functionality on your site.

It's documented knowledge of how the application runs and works together to achieve the business requirements.

When it's time to update a 3rd party library, you can update it and run your functional test suite to see if anything breaks. Instead of having to go through every page yourself, you can have a computer do it for you and give you a list of all the tests that broke.

Load testing! You can simulate thousands of simultaneous users all hitting your site at once and you can see where your site slows down and buckles under the pressure. You can see how your web site behaves long before you get a late night call that the site has crashed.

Functional testing takes time to do manually. Yes, it takes long to write the cases, but if you had to sit down with a binder with 500 pages of tests that you had to complete before you could ship the product you'd wish you had the automated tests!

Testing documents get out of date fast. When a new feature is added, you have to make sure to update the master testing document. If someone skips some tests you all of a
sudden get bugs creeping into pages that are "done and tested". I
currently work in an environment like that, and I can assure you,
it's a nightmare.

In the end, yes it takes time to write these cases, but you should take pride in writing them. It's your way of proving, beyond a shadow of a doubt that your code works and it works with all the other features out there. When QA comes to you and says there is a bug, you fix it, and then add it to your test suite to show that it's fixed and make sure it never happens again.

It is your safety net. When someone goes in and hijacks a stored proc and makes a small change so it'll work with their code, you'll catch that it has broken 3 other features in the process. You'll catch it that night and not the night before the deadline!

As for writing functional tests for only system critical functions. That won't give you the whole picture and it'll allow bugs to sneak through. All it take is for one little feature to be added that isn't system critical, but interacts indirectly with a system critical function and you have the potential to have a bug introduced.

Title: We are spending more time implementing functional test than implementing the system itself, is this normal?

Body:
Basically, we have three main projects, two of them are web services, and the other is a web application. While I'm satisfied with covering as much as we can of our web services with functional tests (all three projects have their proper unit tests), functional tests for the web application are taking a lot of developer time to get implemented. By a lot I mean two times, or sometimes more, the time that takes to implement the functionality being tested with unit test included.

The manager policy is to test every single functionality we add, even if is not business critical (i.e a new C.R.U.D).

I do agree with testing all of the web services functionality, because is hard to manually test them, and also, this tests run fast and don't take to much to implement.

So, what's the value in spending more time writing functional test, than writing system code, unit test and fixing QA tikets? Is this normal? Shouldn't we be writing functional tests only for critical functionality and let QA to do regression tests over no critical functionality?

Note: we are not developing medical software or NASA software or nothing that critical.


答:
You're misusing velocity as an indicator of performance, as though some number of story points accepted is a "good" sprint and anything less than that is a "bad" sprint.

Velocity (which is a terribly misnamed concept) should be used as a forward-looking tool for estimating how many features to which the team can commit in the next sprint, i.e. velocity should be used for capacity planning.

http://jimhighsmith.com/velocity-is-killing-agility/

Here's a salient quote from the article: "The problem is the weight given to velocity and turning it into a productivity measure."

There may be a problem in what looks to be significant variance in your velocity. This doesn't mean that the team is doing anything wrong, but the effect is that the team's capacity for future sprints cannot be predicted very well. Unfortunately, that's not a question that any of us can answer for you. You need to dig into the subject via retrospective. What's really happening?

In any event, the most critical measure is missing from your graph. How well did the team do at delivering the value to which they committed? Does velocity fluctuate because they exceed their commitment in some sprints but not others, does it fluctuate because they're not finishing stories, or does it fluctuate because the commitments also fluctuate?

Title: Velocity does not plateau over time, why?

Body: I have plotted my team burn up chart and its velocity per iteration. To me it looks really bad (velocity fluctuates a lot). What should I be looking for to diagnose the root cause of this behaviour?


问:
Can my GitHub and SourceForge account share the same repository?
I like that SourceForge can also let people browse your code using Git. But, before I even set up the project on SourceForge, I had a GitHub repository for it. Now that I have created my SourceForge project, I had the option to use the Git tool. The problem is when I did that it made a new repository from scratch. I would like to use my existing GitHub repository with my SourceForge project, is this possible? As of now the SourceForge project has no files, so the button on the summary page, instead of a 'Download' button, is a 'Browse Code' button, but the button leads to an empty repository.

So I am wondering, is it possible to link my SourceForge project with my existing GitHub repository, or do I need to remove the 'Code' tool from my SourceForge project and just leave a link to my repository in the description?

答:
You can't, as far as I can tell.

Sourceforge has its own GIT Server and GitHub maintains it's own.

You could manually push to each repo everytime you push.

```<code>git remote add origin SF_URL
git push origin master
git remote rm origin
git remote add origin GITHUB_URL
git push origin master
</code>```

You could use some automatic syncing like this post suggests.

http://www.pragmatic-source.com/en/opensource/tips/automatic-synchronization-2-git-repositories

P.S. You should be able to automate this with Batch / Shell script. This way you can run one script and wait till it is done. This could get complicated when you have confliting changes and you have to merge though !!

Body:
You can't, as far as I can tell.

Sourceforge has its own GIT Server and GitHub maintains it's own.

You could manually push to each repo everytime you push.

git remote add origin SF_URL
git push origin master
git remote rm origin
git remote add origin GITHUB_URL
git push origin master

You could use some automatic syncing like this post suggests.

http://www.pragmatic-source.com/en/opensource/tips/automatic-synchronization-2-git-repositories

P.S. You should be able to automate this with Batch / Shell script. This way you can run one script and wait till it is done. This could get complicated when you have confliting changes and you have to merge though !!


答:
Certainly, simply set up a new remote in your local Git repository, that points to your Sourceforge repository. Suppose you have named this repository sf. Then simply:

```<code>git push sf master
</code>```

will push the master branch to the sf remote. If you've set everything up correctly, a copy of your repository will be pushed into Sourceforge. You may then push any other, or all, branches/tags as desired.

Title: Can my GitHub and SourceForge account share the same repository?

Body:
I like that SourceForge can also let people browse your code using Git. But, before I even set up the project on SourceForge, I had a GitHub repository for it. Now that I have created my SourceForge project, I had the option to use the Git tool. The problem is when I did that it made a new repository from scratch. I would like to use my existing GitHub repository with my SourceForge project, is this possible? As of now the SourceForge project has no files, so the button on the summary page, instead of a 'Download' button, is a 'Browse Code' button, but the button leads to an empty repository.

So I am wondering, is it possible to link my SourceForge project with my existing GitHub repository, or do I need to remove the 'Code' tool from my SourceForge project and just leave a link to my repository in the description?

Body:
Certainly, simply set up a new remote in your local Git repository, that points to your Sourceforge repository. Suppose you have named this repository sf. Then simply:

git push sf master

will push the master branch to the sf remote. If you've set everything up correctly, a copy of your repository will be pushed into Sourceforge. You may then push any other, or all, branches/tags as desired.


问:
Whats the greatest most impressive programing feat you ever witnessed?
Everyone knows of the old adage that the best programmers can be orders of magnitude better than the average. I've personally seen good code and programmers, but never something so absurd. So the questions is, what is the most impressive feat of programming you ever witnessed or heard of?

You can define impressive by:

The scope of the task at hand e.g. John single handedly developed the framework for his company, a work comparable in scope to what the other 200 employed were doing combined.

Speed e.g. Stu programmed an entire real time multi-tasking app OS on an weekened including  its own C compiler and shell command line tools

Complexity e.g. Jane rearchitected our entire 10 millon LOC app to work in a cluster of servers. And she did it in an afternoon.

Quality e.g. Charles's code had a rate of defects per LOC 100 times lesser than the company average. Furthermore he code was clean and understandable by all.

Obviously, the more of these characteristics combined, and the more extreme each of them, the more impressive is the feat.

So, let me have it. What's the most absurd feat you can recount? Please provide as much detail as possible and try to avoid urban legends or exaggerations. Post only what you can actually vouch for.

Bonus questions:

Was the herculean task a one-of, or did the individual regularly amazed people?

How do you explain such impressive performance?

How was the programmer recognized for such awesome work?

答:
I remember it very clearly; on highschool a classmate has coded a full-blown driving videogame in a couple of lessons with QB45 (Quick Basic 4.5).
Vertical scrolling, stopwatch, lifes, levels; I was totally shocked. </p>

ContentLicense: CC BY-SA 2.5


答:
Is spending more time implementing functional test than implementing the system itself normal?

Absolutely.  Writing really good tests is likely to take the majority of the time in many (good) shops.

So a 2-1 ratio is fine.  Less experienced developers themselves often don't take all the time for tests into account.


答:
AS the other posters pointed out, you can add more than one remote then simple push to each.

There is a better way, though, of creating merged remotes to where when you push to your remote, it actually pushes to two remotes.  This answer has the detailed steps: https://stackoverflow.com/a/3195446/525703


问:
What is the most egregious waste of money you have seen, and what did you do about it?
Often we as programmers see large organisations wasting huge sums of money on bloated and inefficient solutions to problems. This pains me greatly because I like organisations to benefit from best of breed solutions. However, my abilities as a programmer are limited when it comes to influencing the key decision makers and often my perspective on the matter is constrained to my own little technical world.

So, my question is this. After encountering an egregious waste of money on some software and/or hardware that really got your goat, what did you do about it to get it fixed or were you doomed to bite the bullet and mutter forever under your breath? I'm interested in hearing your overall experiences and especially what lessons you learned about how to tackle this sort of thing in the future. Let's not name names, the experience of how to tackle the problem is more important than the actual offending product.

答:
I've seen too many examples to name a favourite, but I've noticed a few general trends in my main field, web-development:

Vanity Websites. These are websites that serve no useful purpose to anyone outside the small organisation that commissions them and are built around an obsessive compulsion with logos, photos of themselves and self-indulgent waffle. The worst part is these are usually public-sector funded and commissioned by people who have no clue about the web. (For instance, once had a NHS hospital trust who wanted to develop a mini-version of Facebook for their own staff intranet).

Paid for is Best. The mindset that insists that
paid-for software must intrinsically
be better than open-source. After
all, it's paid for, right? I've seen
so many clients insist on making
stupid choices simply because they
work in a culture that automatically
discounts anything open-source as a
matter of policy.

Design by committee. This is where a huge group of people have a "brainstorm" and then try to incorporate every crack-pot idea there is into the design, inevitably resulting in a ill-thought through mess that compromises on everything in favour of trying to please everyone (and by everyone they mean the committee making the decisions, not the people having to use the application).

Consultants. This is where you pay a middle-man (who knows neither business practices nor software development) to get in the way and cream-off money by protracting the development process with confusing techno-babble and business-speak.


问:
Why does automated testing keep failing in my company?
We have tried to introduce developer automated testing several times at my company. Our QA team uses Selenium to automate UI tests, but I always wanted to introduce unit tests and integration tests. In the past, each time we tried it, everyone got excited for the first month or two. Then, several months in, people simply stop doing it.

A few observations and questions:

Does automated testing actually work? Most of of my colleagues who used to work at other companies have tried and failed to implement an automated testing strategy. I still haven't seen a real-life software company that actually uses it and doesn't just talk about it. So many developers see automated testing as something that is great in theory but doesn't work in reality. Our business team would love developers to do it even at a cost of 30% extra time (at least they say so). But developers are skeptics.

No one really knows how to properly do automated testing. Yes we have all read the unit testing examples on the internet, but using them for a big project is something else altogether. The main culprit is mocking/stubbing the database or anything else that is non-trivial. You end up spending more time mocking than writing actual tests. Then when it starts taking longer to write tests than code, that's when you give up.

Are there any good examples of unit tests/system integration tests used in a complex data centric web applications? Any open source projects? Our application is data centric but also has plenty of domain logic. I tried the repository approach at some point and found it pretty good for unit testing, but it came at the price of being able to optimize data access easily and it added another layer of complexity.

We have a big project undertaken by 20 experienced developers. This would seem to be an ideal environment to introduce unit testing/integration testing.

Why doesn't it work for us? How did you make it work at your company?

答:
Automated testing is like software development. Unfortunately the people you hire for testing are originally intended to write test cases, plans, strategy, follow review process, manually test and log bugs.

As soon as they are given automated testing responsibilities it includes some amount of software development. The catch here is, that automated testing, regardless of what tools you use (and for heaven sake dont argue this point), need maintenance and updating on a daily basis. As developers change code,

you need to ensure that tests are kept running.

You need to ensure that tests are not removed since they did not run

your test metrics needs to show what you ran on the last build and this build. To ensure that your # of test cases is not reducing.

test cases need to be reviewed just like development to ensure that people are not messing up, kinda breaking 1 test into 2 just to bump up the numbers (some times testing is outsourced, so this tracking is important)

a lot more "healthy" communication between dev and test is important

keep non-functional tests separate, and don't expect them to run it daily, it takes time to keep these uptodate, and good. But dont give up, ensure that they are maintained.

You fail due to these reasons

your test engineers are manual test engineers with no analytical skills. they dont know the difference between a if and a while loop. Because frankly no course teaches automated testing, they only teach manual testing.

your test engineers are too busy manual testing your builds, and logging bugs so they loose track of automated tests

your test managers dont care for metrics from automated tests, just because they show invalid data (when the project starts), and they dont put effort or priority in daily standups and meetings to stress how important it is to get automation up and running

you choose to automate tests for mobile applications, which have a very short shelf life. by the time you write, stabilize the automated test suite your application requirements change, instead you should focus on testing your webservices that run your application

you dont understand that automated test team follows the same milestone are development team, feature complete, code complete, code lockdown and code freeze.

you dont differentiate between manual testing folks and automated testing folks.

they both get paid the same salary and report to the same manager, ideally they should report to the dev manager and their salaries should match those of development.

you actually think and believe that junit is not enough for developing automated tests :).

These are from my experience working for companies who take automated testing very seriously and understand that dev is important as automated testing engineers.  And from my experience working for people who dont know, understand the difference, no matter how much you explain to them.


问:
What the right pattern to Use here
scenario:

I have an entity called Member. Members fall into multiple types. Some types are below.

Supporter

Volunteer

Sponsor

Player

I can create class for each type and inherit from the base Member entity. But when a Member has more than 1 type in a given time, it is not the right way.

can I use decorator pattern to achieve this intent? Or what the best pattern to get this work?

EDIT :

Language I use is C#

Also different Member types can have additional methods, for example, Sponsor can have its own methods as Donate(), Or the Volunteer can have a method Subscribe() and so on. these thing have to be considered.

答:
As usual, there are several different ways. Here are a few possible ideas for you to investigate:

Inheritance

I would not realize this via inheritance - especially not if you want multiple such roles for a member. Even with just one role, there's the composition over inheritance principle that tells you to avoid doing that if possible. It reduces your potential for reuse dramatically. Anyways, with your requirement that you want to be able to support multiple roles for a member, you would have to provide subclasses for each combination, which results in an exponential explosion of class combinations. Not very feasible.

Decorator

As you pointed out the decorator pattern would be one possible way, but would still require a lot of work in wrapping the original Member and providing all of its methods via the decorated object as well.

Composition

Composition is a straightforward way that you could use to add a role to a Member, if you can define a common interface for the roles, i.e., Supporter, Volunteer, etc. would implement the interface MemberRole and the Member class gets an attribute of type MemberRole (or a list thereof).

If, however, the roles each have different methods, composition doesn't work so well anymore (f.ex. a Supporter has support(), but a Player has play()), because you cannot find a common interface for the roles.

Traits/Mixins

As you didn't tell us which programming language you want to use, let me add one more option that would be interesting, but harder to realize in some languages. As an example, consider Scala with its support for traits. They are a perfect match for your problem, as you could do the following:

```<code>trait Supporter {
  def support = ...
}
trait Player {
  def play = ...
}
// and so on

val memberA = new Member with Supporter with Player
memberA.support // works fine
memberA.play // works fine
val memberB = new Member with Supporter
memberB.support // works fine
memberB.play // won't compile
</code>```

Composing traits like this essentially saves you the exponential explosion mentioned above, as you do not need to provide a specific class for a MemberSupporterPlayer and such. An additional advantage is the type-safety shown above that ensures you cannot call methods from a role that your member does not possess.

For something similar in C# you can consider Mixins. See for example the remix project for this. They essentially allow the same kind of composition, but of course use a different syntax.

Title: What the right pattern to Use here

Body:
As usual, there are several different ways. Here are a few possible ideas for you to investigate:

Inheritance

I would not realize this via inheritance - especially not if you want multiple such roles for a member. Even with just one role, there's the composition over inheritance principle that tells you to avoid doing that if possible. It reduces your potential for reuse dramatically. Anyways, with your requirement that you want to be able to support multiple roles for a member, you would have to provide subclasses for each combination, which results in an exponential explosion of class combinations. Not very feasible.

Decorator

As you pointed out the decorator pattern would be one possible way, but would still require a lot of work in wrapping the original Member and providing all of its methods via the decorated object as well.

Composition

Composition is a straightforward way that you could use to add a role to a Member, if you can define a common interface for the roles, i.e., Supporter, Volunteer, etc. would implement the interface MemberRole and the Member class gets an attribute of type MemberRole (or a list thereof).

If, however, the roles each have different methods, composition doesn't work so well anymore (f.ex. a Supporter has support(), but a Player has play()), because you cannot find a common interface for the roles.

Traits/Mixins

As you didn't tell us which programming language you want to use, let me add one more option that would be interesting, but harder to realize in some languages. As an example, consider Scala with its support for traits. They are a perfect match for your problem, as you could do the following:

trait Supporter {
  def support = ...
}
trait Player {
  def play = ...
}
// and so on

val memberA = new Member with Supporter with Player
memberA.support // works fine
memberA.play // works fine
val memberB = new Member with Supporter
memberB.support // works fine
memberB.play // won't compile

Composing traits like this essentially saves you the exponential explosion mentioned above, as you do not need to provide a specific class for a MemberSupporterPlayer and such. An additional advantage is the type-safety shown above that ensures you cannot call methods from a role that your member does not possess.

For something similar in C# you can consider Mixins. See for example the remix project for this. They essentially allow the same kind of composition, but of course use a different syntax.


问:
How can I deal with a team member who dislikes making comments in code?
One of my team members consistently avoids making comments in his code.

His code is not self-documenting, and other programmers have a difficult time understanding his code.

I have asked him several times to comment his code, however he just gives excuses or claims that he will do it later. His concern is that adding comments will take too much time and delay the projects.

What argument can I present to him to convince him to properly document his code?

On that note, am I wrong to focus on the code comments or is this indicative of a larger problem which should be addressed?

答:
Comments alone don't make for better code, and just pushing for "more comments" is likely to give you little more than /* increment i by 1 */ style comments.

So ask yourself why you want those comments. "It's best practice" does not count as an argument unless you understand why.

Now, the most striking reason for using comments is so that the code is easier to understand, and when people complain about lack of comments, they are either clueless parrots, or they have a hard time understanding the code they're working with.

So don't complain about missing comments: complain about unreadable code. Or better yet, don't complain, just keep asking questions about the code. For anything you don't understand, ask the person who wrote it. You should be doing that anyway; with unreadable code, you'll just ask more questions. And if you come back to a piece of code later, and you are unsure you remember correctly what it does, ask the same question again.

If comments can fix it, and your colleague has a functioning brain, he/she will at some point realize that commenting the code is much easier than having you around asking stupid questions all the time. And if you can't come up with questions, then maybe that code is perfectly readable already, and it's you who is at fault - after all, not all code needs comments.

On the people skills front, avoid sounding condescending or accusing at all cost; be serious and honest about your questions.


答:
This depends on the code your team worker produces. If you read the Clean Code book from Uncle Bob you will find that he actually prefers to not add comments to code. If the code itself is as readable as it should be, then there is hardly any need for comments.

If that is not the case, or you require comments due to some non-negotiatable policy, then the main question becomes whether it is only you who wants him/her to write comments, or whether it is the whole team or the team/project leader. If it's just you, then you should talk to your other colleagues to find out why it may not be such a big deal at all.

If the project leader lacks the comments you can also require them as part of completeness, i.e. if the submitted code lacks comment the work is not finished yet. He may not continue to do other work, until his current work is finished and for that comments are required. However, keep in mind that this kind of forcing will most probably result in horrible comments (expect loads of crappy repetition-of-code-comments).

The only feasible way in my humble opinion is to discuss the actual profits you and everyone else gets from comments. If s/he does not understand it by mere discussion, there's always the hard way too: instead of letting them write new code have them work on existing code. If possible you should find them two different work areas - one with proper useful comments and another that lacks comments. Having to read someone else's non-readable non-commented code is an eye-opener with regards to your own work.

We have all been there once and were angry for the original author of some source for working so sloppy. It's the additional reflection that each of us is such an author as well that makes you realize you should care about readability - hence, do not forget to discuss the results with your colleague afterwards in order to promote this reflection.

Title: How can I deal with a team member who dislikes making comments in code?

Body:
One of my team members consistently avoids making comments in his code.

His code is not self-documenting, and other programmers have a difficult time understanding his code.

I have asked him several times to comment his code, however he just gives excuses or claims that he will do it later. His concern is that adding comments will take too much time and delay the projects.

What argument can I present to him to convince him to properly document his code?

On that note, am I wrong to focus on the code comments or is this indicative of a larger problem which should be addressed?


问:
How important is it to reduce the number of lines in code?
I am a Software developer who works on J2SE (core java).

Often during our code reviews we are asked to reduce the number of lines in our code.

It's not about removing redundant code, it's about following a style that focuses on doing the same things with fewer lines in the code, while I believe in having clarity in code even if it means increasing the number of lines.

What do you think is the right way of doing things?

If LOC (lines of code) is a small number, how does it affect the code?
If LOC is a larger number, how does it affect the code?

example from the website : "javaranch"  -

```<code>public static void happyBirthday(int age)
{
    if ((age == 16) || (age == 21) || ((age > 21) && (((age % 10) == 0) || ((age % 25) == 0))))
    {
        System.out.println("Super special party, this year!");
    }
    else
    {
        System.out.println("One year older. Again.");
    }
}
</code>```

VS

```<code>public static void happyBirthday(int age)
{

    boolean sweet_sixteen = (age == 16);
    boolean majority = (age == 21);
    boolean adult = (age > 21);
    boolean decade = (age % 10) == 0;
    boolean quarter = (age % 25) == 0;

    if (sweet_sixteen || majority || (adult && (decade || quarter)))
    {
        System.out.println("Super special party, this year!");
    }
    else
    {
        System.out.println("One year older. Again.");
    }
}
</code>```

答:
I agree with your code reviewers, but with an asterisk. Each statement that you write in your code is a technical liability -- it's a potential failure point. If you write a method with 10 statements and your coworker writes one that achieves the same functionality with 5 statements, his is likely to be 'better' as measured by likelihood of issues (there are twice as many places your code can be wrong, overly complex, or problematic).

Here's the asterisk, though. It's not about the actual number of lines of code in the idea because you can reduce the number of lines with something like:

```<code>void someMethod() {
 someobject.doSomething(someSingleton.getInstance().with().a().lot().of().law().of().demeter().violations()).and().if().that().werent().enough().theres().more();
}
</code>```

This is one line of code, but it's a place where an amazing amount of things can go wrong. So I'd say focus on doing the most with the fewest statements -- write as much code as you need to get things done and no more. At least, I think that's what your code reviewers are driving at.

Personally, I think there's a balance to be struck. As I said, each statement that you write is a liability, but if pulling out a local variable with a descriptive name makes your method a lot clearer and more readable, then there's a case to be made for that too. I think you can easily get into situations where people are quibbling over relatively minor aesthetic differences, but on the whole, I think your reviewers have the right idea -- favor minimizing the number of things that can go wrong.

Body:
I agree with your code reviewers, but with an asterisk. Each statement that you write in your code is a technical liability -- it's a potential failure point. If you write a method with 10 statements and your coworker writes one that achieves the same functionality with 5 statements, his is likely to be 'better' as measured by likelihood of issues (there are twice as many places your code can be wrong, overly complex, or problematic).

Here's the asterisk, though. It's not about the actual number of lines of code in the idea because you can reduce the number of lines with something like:

void someMethod() {
 someobject.doSomething(someSingleton.getInstance().with().a().lot().of().law().of().demeter().violations()).and().if().that().werent().enough().theres().more();
}

This is one line of code, but it's a place where an amazing amount of things can go wrong. So I'd say focus on doing the most with the fewest statements -- write as much code as you need to get things done and no more. At least, I think that's what your code reviewers are driving at.

Personally, I think there's a balance to be struck. As I said, each statement that you write is a liability, but if pulling out a local variable with a descriptive name makes your method a lot clearer and more readable, then there's a case to be made for that too. I think you can easily get into situations where people are quibbling over relatively minor aesthetic differences, but on the whole, I think your reviewers have the right idea -- favor minimizing the number of things that can go wrong.


答:
I tend to agree with your position of "having clarity in code even if it means increasing the number of lines."

I've seen too many one-liners that are fairly terse, but it's not immediately apparent what they are doing. Readability is king since other developers will have to maintain your code.

I would argue that a better thing to aim for is short methods. Not short for the sake of few lines of code, but short because they do a single thing.

Title: How important is it to reduce the number of lines in code?

Body:
I am a Software developer who works on J2SE (core java).

Often during our code reviews we are asked to reduce the number of lines in our code.

It's not about removing redundant code, it's about following a style that focuses on doing the same things with fewer lines in the code, while I believe in having clarity in code even if it means increasing the number of lines.

What do you think is the right way of doing things?

If LOC (lines of code) is a small number, how does it affect the code?
If LOC is a larger number, how does it affect the code?

example from the website : "javaranch"  -

```<code>public static void happyBirthday(int age)
{
    if ((age == 16) || (age == 21) || ((age > 21) && (((age % 10) == 0) || ((age % 25) == 0))))
    {
        System.out.println("Super special party, this year!");
    }
    else
    {
        System.out.println("One year older. Again.");
    }
}
</code>```

VS

```<code>public static void happyBirthday(int age)
{

    boolean sweet_sixteen = (age == 16);
    boolean majority = (age == 21);
    boolean adult = (age > 21);
    boolean decade = (age % 10) == 0;
    boolean quarter = (age % 25) == 0;

    if (sweet_sixteen || majority || (adult && (decade || quarter)))
    {
        System.out.println("Super special party, this year!");
    }
    else
    {
        System.out.println("One year older. Again.");
    }
}
</code>```


答:
David Heinemeier Hansson creating Rails.

The first time I installed Rails and set up a test site against a database and it did all the framework and CRUD stuff automatically, set up the test site and it was like my eyes were open for the first time to what can be done before you start programming. That was really impressive and I've seen ideas from that appearing all over the place in other languages/platforms since then.

Title: Whats the greatest most impressive programing feat you ever witnessed?

Body:
Everyone knows of the old adage that the best programmers can be orders of magnitude better than the average. I've personally seen good code and programmers, but never something so absurd. So the questions is, what is the most impressive feat of programming you ever witnessed or heard of?

You can define impressive by:

The scope of the task at hand e.g. John single handedly developed the framework for his company, a work comparable in scope to what the other 200 employed were doing combined.

Speed e.g. Stu programmed an entire real time multi-tasking app OS on an weekened including  its own C compiler and shell command line tools

Complexity e.g. Jane rearchitected our entire 10 millon LOC app to work in a cluster of servers. And she did it in an afternoon.

Quality e.g. Charles's code had a rate of defects per LOC 100 times lesser than the company average. Furthermore he code was clean and understandable by all.

Obviously, the more of these characteristics combined, and the more extreme each of them, the more impressive is the feat.

So, let me have it. What's the most absurd feat you can recount? Please provide as much detail as possible and try to avoid urban legends or exaggerations. Post only what you can actually vouch for.

Bonus questions:

Was the herculean task a one-of, or did the individual regularly amazed people?

How do you explain such impressive performance?

How was the programmer recognized for such awesome work?


答:
Your velocity has noise (fluctuations). Possible reasons:

Stories are too big and quite often a halfway finished story is lifted to the next sprint.

The stories were not accurately estimated. This can be because of an inexperienced team or again too big stories.

This noise is not necessarily a problem by itself: a noisy velocity that fluctuates around a constant average still allows you to do accurate release planning.

However, if you filter out the noise (rolling average over 5 consecutive sprints), then your velocity is still going down after 20 sprints. It makes it hard to do release planning and it is worth investigating:

Is the "definition of done" too weak and the team is piling up left-over work from previous sprints?

Is the organization getting better at diverting scrum and imposing non-backlog work upon the team?

The large stories (epics) at the bottom of the back log were estimated more optimistic than the more detailed stories on top?


问:
Is path coverage stronger than condition coverage?
I have encountered (even in literature) two contradicting opinions related to path vs condition coverage (not branch or edge!). Some say Path coverage is stronger than the condition coverage, some say the opposite.

Having code like this:

```<code>If(X<0 OR X>100)
   DoStuff();
</code>```

Path coverage - 1 test case is needed to cover the path. With X=-1, the second expression will not be tested and possible bug will be missed.

Condition coverage - test cases X=-1 and X=100 are needed to test both conditions.

Am I missing something or condition coverage is really stronger than path coverage?

Condition coverage (ISTQB):

Our next level of coverage is called condition coverage. The basic concept is that, when a decision is made by a complex expression that eventually evaluates to TRUE or FALSE, we want to make sure that each atomic condition is tested both ways.

Practical Insight Into CMMI

Condition coverage measures the true and false outcome of each Boolean
  subexpression.

References:

Software Testing: Principles and Practices from Srinivasan Desikan,Gopalaswamy Ramesh: page 61

Condition coverage is a much stronger criteria than path coverage,
  which in turns is a much stronger criteria than statement coverage.

答:
Path coverage basically includes the coverage of the statements in the program that you have written.That is if the program contains the following lines of code as:
1. if(condition 1)
2.  then call a function;
3.  else
4.  call that function;
5.  call another function;
So if we now closely observe the code we will find that there are 5 statements in total and the two of those statements are actually conditions.
So a path coverage will start from the first path and will check if the condition is true. If not then it will switch to the else part and execute the statement 4 and 5.Thus in all the number of statements covered are 1,3,4,5.
Whereas in the condition coverage the if and the else part are checked only not the 5th statement i.e. only statements 1 and 3 are checked. But also note that if there were only conditions then here in this case path coverage=statement coverage.</p>


答:
Taking the reviewers' advice literally won't do any good, because the obvious direct result is promoting terse one-liners (line length limit notwithstanding). I believe the lesson to be learnt here, though, is to make your code do fewer things.

In other words, this is a call for simplicity. It is quite popular claim that code is a liability, not asset, so reducing its amount while preserving the functionality is a noble effort. This can be achieved by a direct, more down-to-earth approach that addresses the problem directly and prefers concise solutions.

Like Ken Thompson once said: One of my most productive days was throwing away 1000 lines of code.

ContentLicense: CC BY-SA 4.0


问:
Declaring functions in order to avoid explicit nested loops
My programming professor has told me that it is a good programming practice (at least in C/C++) to declare a function with the inner loop when nesting loops (not for loops, since when, i.e. looping through a multidimensional array this is very intuitive). For example, if I write

```<code>int main()
{
    while (cond1)
    {
        // ...(1)...
        while (cond2)
        {
            // ...(2)...
        }
    }
}
</code>```

then it is better:

```<code>type inner_loop(param)
{
    // ...(1)...

    while (cond2)
    {
        // ...(2)...
    }
}

int main()
{
    while (cond1)
    {
        inner_loop(param);
    }
}
</code>```

What improvements do imply this method? Is there any case in which this way of programming nested loops can be counterproductive? Thanks in advance.

答: For different scenarios different approaches are considered. When we want to submit a code which is easy to understand by the teacher or any other friend then we should definitely avoid using nested loops as it becomes difficult for the checker to trace the calls for all the variables one by one. But if there is no such case then there is no harm in using the nested loops as there is not such any change in the complexities. Making additional functional calls just increases the number of the activation records used for different functions and also the memory required for the current activation record pointer and the other pointers that point to the previous records of the functions increases.Thus in both the ways the memory increases but it is up to the programmer which approach to use.But the preferred one is to make functions for different purposes as it becomes easy for the programmer also to understand the code after if he/she reviews it after some time.</p>


答:
The great problem with every formal method of code evaluation is that it will surely fail on some code.
This is because the programming is a kind of art and as such, there can not exists clear standards for evaluation.
Of course there are different kind of arts - mass production of paintings also exists, and maybe in this business is important how many paint is spend on one painting. :)</p>


答:
In earlier period it was assumed that if the number of lines of code that is loc is large then the code is effective and it accomplishes all the tasks that it is used to do but it was realized after some time that less is the loc more it has the efficiency as the cost incurred by the system in executing the code is very less.Thus for the betterment of the system loc was to be made less.Since then it was assumed that the loc shoul be less.To reduce it the following factors should be taken into account.

Make a flowchart of your problem

Try to use less number of loops as you can

The function calls should not be much

New data structures like TRIE should be used in the case where stack or queue is used for storing the relevant information.

And do try to solve the problem with a realistic approach rather than imaginative one where you are storing a huge amount of numbers in a very large array and then trying to access it through the array.]

Use as many macros as possible
Further the approach to use also depend upon the problem.If it is really complex then sometimes using the simple approach is also equivalent to the complex one


答:
The problem with measurements, no matter how well intended they are, is the very act of measuring the item makes it important, and the corollary, the act of not measuring an item makes it unimportant. It is absolutely essential to measure what is important, and not measure what is unimportant.

Measuring SLOC (Which is effectively what your reviews are doing), makes SLOC important.... Is SLOC important? - absolutely not, never has been (Outside Obfuscated programming contests), never will be in a commercial organization.

Ask yourself one simple question - how does "Reduce the SLOC of this routine" make anyones code better.  What is probably happening in this case is SLOC is being used as a naive way to measure complexity. What you must avoid at all costs is counting the easy to count beans - objective measures such as SLOC, instead of counting the important, but hard to count ones - e.g. Readability, complexity etc.


答:
Even with unique methods in each role, you can use Composition.
You could use Member that has multiple Roles and use Visitor pattern to access their unique methods.</p>

Title: What the right pattern to Use here

Body:
scenario:

I have an entity called Member. Members fall into multiple types. Some types are below.

Supporter

Volunteer

Sponsor

Player

I can create class for each type and inherit from the base Member entity. But when a Member has more than 1 type in a given time, it is not the right way.

can I use decorator pattern to achieve this intent? Or what the best pattern to get this work?

EDIT :

Language I use is C#

Also different Member types can have additional methods, for example, Sponsor can have its own methods as Donate(), Or the Volunteer can have a method Subscribe() and so on. these thing have to be considered.


答:
Many projects require code documentation: interface document, design document, ...

Some projects require that such documentation shall be put in code comments and extracted with tools like Doxygen or Sphinx or Javadoc, so that code and documentation keep more consistent.

That way, developers are required to write useful comments in code and this duty is integrated in project planning.


问:
C# Interview Preparation - References?
This is a specific question relating to C#. However, it can be extrapolated to other languages too.

While one is preparing for an interview of a C# Developer (ASP.NET or WinForms or ), what would be the typical reference material that one should look at?

Are there any good books/interview question collections that one should look at so that they can be better prepared?

This is just to know the different scenarios. For example, I might be writing SQL Stored Procedures and Queries, but I might stumble when asked suddenly

Given an Employee Table with the following column(s).

EmployeeId, EmployeeName, ManagerId

Write a SQL Query which will get me the Name of Employee and Manager Name?

NOTE: I am not asking for a Question Bank so that I can learn by rote what the questions are and reproduce them (which, obviously will NOT work!)

答:
There is one really important thing to notice (as metioned on DotNetRocks show 608)...usually you wanna hire a guy that knows concepts and algorithms over a language specialist.

Know stuff like... IoC, Design patterns and stuff like that. It doesn't really matter if you know how to do really tricky 10 nested Linq queries if you don't know how to bring bread on the table in a flexible manner...I actually think that the main focus is know allot more about how to create good solutions over how the language can do this and that.

Title: C# Interview Preparation - References?


答:
I have met lots of devs who had trouble in writing self-documenting code or helpful comments. These kinds of people often lack enough self-discipline or experience to do it right.

What never works is, "telling them to add more comments". This will increase neither their self-discipline or experience. IMHO the only thing that might work is to make frequent code-reviews & refactoring sessions. When a dev has completed a task, allow him/her to explain any parts of the code you don't understand. Immediately refactor or document the code in such a way that both of you will understand 6 months later.

Do this over a period of a few months, at least twice a week. If you are lucky enough, the other devs will learn through these sessions so that you can reduce the review frequency.


问:
Java labels. To be or not to be
Before I start this question I know the java 'goto' is a big no go.

So I've been writing a program and I have some indented Loops and statements and I need to BREAK multiple on command. Rather than having a load of boolean variables and if(!<booleanName>) BREAK; statements throughout these loops and statements what is everyone's opinions on using labels to break them using the BREAK  statement?

e.g.

```<code> for(...) {

      indented_example_loops:     // Label name
           for(...) {
                for(...) {
                     if(match) break indented_example_loops;
                     // ^ Break statement to break the 2 for loops at once
                }
           }

 }
</code>```

Perfectly okay? Okay to do occasionally? Completely avoid? or should i go to a corner and call the Devil to take my soul?

答:
It's all about readability and ease of understanding.

The easiest to understand loops are those with simple end conditions and no breaks at all. So if you can write your loops like that, do it.

```<code>while (someCondition) {
  // do stuff
}
</code>```

Next comes the loop with a simple end condition and a single break:

```<code>while (someCondition) {
  // do stuff
  if (otherCondition) {
    break;
  }
  // maybe some more stuff
}
</code>```

Everything that's more complex than this is a potential problem. A break with a label is always tricky, because you'll have to hunt for the label to find out which statement/block the break escapes from (i.e. the break will continue after the labeled block/statement).

The worst kind (the one you almost always want to avoid) is a nested loop where you break somewhere in the middle.

```<code>while (someCondition) {
  myLabel:
  for (Foo foo : getAllFoos()) {
    for (Bar bar : foo.getBars()) {
      // do stuff
      if (someCondition(bar)) {
        break myLabel; // BAD!
      }
      // some more stuff
    }
  }
}
</code>```

The problem here is that the actual flow of processing becomes really hard to follow. If you start having code like this, then you should definitely refactor it.

A common refactoring is to extract one (or more) nested loops into a method and turning the break into a return. This has the added benefit of giving a name to that piece of the code. If the name is chosen well, then that will help greatly in understanding the code.

Title: Java labels. To be or not to be

Body:
It's all about readability and ease of understanding.

The easiest to understand loops are those with simple end conditions and no breaks at all. So if you can write your loops like that, do it.

while (someCondition) {
  // do stuff
}

Next comes the loop with a simple end condition and a single break:

while (someCondition) {
  // do stuff
  if (otherCondition) {
    break;
  }
  // maybe some more stuff
}

Everything that's more complex than this is a potential problem. A break with a label is always tricky, because you'll have to hunt for the label to find out which statement/block the break escapes from (i.e. the break will continue after the labeled block/statement).

The worst kind (the one you almost always want to avoid) is a nested loop where you break somewhere in the middle.

while (someCondition) {
  myLabel:
  for (Foo foo : getAllFoos()) {
    for (Bar bar : foo.getBars()) {
      // do stuff
      if (someCondition(bar)) {
        break myLabel; // BAD!
      }
      // some more stuff
    }
  }
}

The problem here is that the actual flow of processing becomes really hard to follow. If you start having code like this, then you should definitely refactor it.

A common refactoring is to extract one (or more) nested loops into a method and turning the break into a return. This has the added benefit of giving a name to that piece of the code. If the name is chosen well, then that will help greatly in understanding the code.


答:
You are opening yourself up to spaghetti code

While the language allows you to do this, it is not good practice. There are many alternative looping structures that will achieve the same objective and make your code much more readable.

Essentially, labels and gotos are a holdover from assembly language code (which is all about labels and gotos). It really has no place in higher level languages.

Title: Java labels. To be or not to be

Body:
Before I start this question I know the java 'goto' is a big no go.

So I've been writing a program and I have some indented Loops and statements and I need to BREAK multiple on command. Rather than having a load of boolean variables and if(!<booleanName>) BREAK; statements throughout these loops and statements what is everyone's opinions on using labels to break them using the BREAK  statement?

e.g.

```<code> for(...) {

      indented_example_loops:     // Label name
           for(...) {
                for(...) {
                     if(match) break indented_example_loops;
                     // ^ Break statement to break the 2 for loops at once
                }
           }

 }
</code>```

Perfectly okay? Okay to do occasionally? Completely avoid? or should i go to a corner and call the Devil to take my soul?


问:
Is python lambda "really formal" λ-calculus or just share the name?
Now and then I use the Python lambda. Is it so formal that it is safe to say that you can do formal lambda calculus with it? I just used it but I didn't fully understand whether the python lambda and the lambda calculus like I read was done by Alonzo Church. I also used it in Javascript, I think. Isn't this more common in functional languages  (e.g. Haskell, Scheme/Lisp, Clojure...) and I never saw lambda in use with Java or C(++)?

Is says on this site " a function you can pass on to another function as argument.".

What is "lambda" code?

But how do I get used to it enough so that I can tell where to benefit from it? If I can do it in say SQL, JPQL or GQL instead, shouldn't I prefer to do it in the query language?

答:
Python lambda expressions are real, formal untyped λ-calculus lambda expressions.

They fit the formal definition; they can only represent one python expression, based on variables (free or otherwise) and references to other functions (abstract symbols). Python uses parenthesis in expressions too.

You use them wherever a lambda is more suitable and convenient than a full function definition. The python def functionname(argumentlist): syntax forms a statement; in Python you cannot put statements inside of expressions, only the other way around. A lambda on the other hand, is an expression, so you can use a lambda to insert a callback function inline:

```<code>map(lambda x, y: x[y+5], [(mapping1, integerkey1), (mapping2, integerkey2)])
</code>```

The above example consists only of an expression. The python map() function takes, as its first argument, a callable, which is applied to each and every element in the list given by the second argument. In the above example, using a lambda expression to define that callable is much easier than using a function statement:

```<code>def mapcallback(x, y):
    return x[y + 5]

map(mapcallback, [(mapping1, integerkey1), (mapping2, integerkey2)])
</code>```

For the full function syntax I need to assign a name, put the function definition on separate lines, and use the return statement to return the result of the expression.

Body:
Python lambda expressions are real, formal untyped λ-calculus lambda expressions.

They fit the formal definition; they can only represent one python expression, based on variables (free or otherwise) and references to other functions (abstract symbols). Python uses parenthesis in expressions too.

You use them wherever a lambda is more suitable and convenient than a full function definition. The python def functionname(argumentlist): syntax forms a statement; in Python you cannot put statements inside of expressions, only the other way around. A lambda on the other hand, is an expression, so you can use a lambda to insert a callback function inline:

map(lambda x, y: x[y+5], [(mapping1, integerkey1), (mapping2, integerkey2)])

The above example consists only of an expression. The python map() function takes, as its first argument, a callable, which is applied to each and every element in the list given by the second argument. In the above example, using a lambda expression to define that callable is much easier than using a function statement:

def mapcallback(x, y):
    return x[y + 5]

map(mapcallback, [(mapping1, integerkey1), (mapping2, integerkey2)])

For the full function syntax I need to assign a name, put the function definition on separate lines, and use the return statement to return the result of the expression.


问:
How do I avoid feature creep on a solo project?
So I have a program I'm worked on back in 2011 and all through 2012, but the last release was in December of 2011. I've been actively working on it, but feature creep lured its ugly head and now it is filled with tons on unfinished features.

The bad part is that as I implement a feature, a new one creeps in. What can I do to avoid feature creep in the future so I can actually get a release out in over a year?

The project is based around iOS and used to have releases around each iOS version update, but the last one was back with 5.1 (2011). I would like to be able to get that steady release cycle back, but it has proven too difficult.

答:
Use a version control system in which it is cheap to create a branch for some idea, and keep it out of your release path. For instance in git, you can "creep up" some idea, and then git stash it away. Later you can review these stashes and cherry pick them in whatever order seems interesting.

For larger features, make a real branch (so you can do multiple commits). Case in point: when I wanted to add generational support to the garbage collector, I made a branch. Stashes capture the distracting little stuff very well. Big features can start as stashes, then turn into branches, and then finally merge when they are ready.

With stashes and branches, you can take stock of your ideas, prioritize them and establish a scope for the releases of your solo project, just like a managed team project.

Look, when you get an idea, it has to go somewhere, and the best somewhere is code: the repo. Creeping features is better than forgetting good ideas. But of course, if you creep all your features into the same mainline, it will keep delaying release, unless you cut untidy releases full of half-done stuff that users have to be warned not to use.


问:
How can I understand aggregation and containment?
I'm confused between aggregation and containment. I'm wondering if the following represent an aggregation or containment?</p>

```<code>class Auto
{

   private string model;
   private int speed;

     class AutoCustomer
     {
        public string LastName;
        public string Address;
        public DateTime DateOfPurchase;
     }
}
</code>```

答:
The way to understand aggregation versus containment is association versus ownership. Does a car "have" an engine? Yes, it wouldn't be a car without it (in most situations). Does a fruit bowl "have" fruit? Yes; even if there are currently zero fruit in the bowl.

Does a car "have" a customer? No. The dealership "has" a customer—maybe—and the dealership "has" cars. The dealership may wish to associate a customer with a car they're interested in, but the car doesn't own that customer.


答:
Git and/or Linux

Linus Torvalds wrote an operating system kernel and a version control system from scratch. I don't know of anyone with a similar productivity.


答: Give him some one else code without comments and ask him to understand the code. May be he understand the importance of proper comments then.</p>


答:
If you follow good coding standard, there will be a minimum number of comments required.
naming conventions are important. Method names and variable names should describe their role..

My TL suggests me to use less comments. he wants my code should understandable and self descriptive.
 simple example:
variable name for string type which used for search pattern

```<code>   var str1:String="" //not uderstandable
   var strSearchPattern:String="" //uderstandable
</code>```

Body:
If you follow good coding standard, there will be a minimum number of comments required.
naming conventions are important. Method names and variable names should describe their role..

My TL suggests me to use less comments. he wants my code should understandable and self descriptive.
 simple example:
variable name for string type which used for search pattern

   var str1:String="" //not uderstandable
   var strSearchPattern:String="" //uderstandable


问:
Is a programmer supposed to be self-sufficient?
At my current workplace, we don't have any testers, the rationale for that from the management being: "if we had testers, you wouldn't test your own code at all". This kind of thinking seems to be detrimental to product quality, as while I do test my own code, there are a lot of things that I will miss just for the fact that I know the system inside out and don't know how to use it "wrong". Black box testing doesn't really work as I subconsciously avoid the pitfalls that a dedicated tester would fall into. A lot of my time goes into fixing bugs that have slid into production code and found by the end user.

The system in question is large but is developed solely by me. This has also caused some managemental duties to fall on my lap, such as defining schedules and working on specifications.

Should these kind of tasks be my responsibility? I see myself strictly as a programmer and nothing else. And if these are my responsibility, to what extent? When is a project so large that it requires testers? Should a programmer have to refine the specification, worry about management of the project or even provide customer support?

Note

Some might have got the impression that I am against widening my responsibilities – that is not the case, I'm eager to get a role that involves more management duties, but currently it is not in my job description. Until I'm officially employed as such or the additional duties start showing in my paycheck, I'm going to think of myself as 'just' a programmer. Unfortunately, as a junior developer, shifting to managerial duties is not going to happen very soon.

Excellent answers so far, keep them coming if you have something to add or personal experiences to share!

答:
"if we had testers, you wouldn't test your own code at all"

"If your car had a seat-belt you would crash it all the time!"

Should these kind of tasks be my responsibility? [...] And if these are my responsibility, to what extent?

The answer to that is "it depends". From what I understand,your employers see you as the "one-man-IT-department". If that is the case, yes, they are (your responsibility). If you have responsibilities that you absolutely hate and wish to avoid, look for a position within a company with a larger IT department.

When is a project so large that it requires testers?

That's not (quite) a correct question to ask. You're better of asking "when is the quality of the product/image of the company so important that it require testers?"

Should a programmer have to refine the specification, [...]

Yes, definitely. Most times, there is a large discrepancy between what a developer/implementer needs and what the clients provide [as specifications].

Many times it is up to you to find gray/unspecified areas and ask the right questions, to notice and point out technically impossible or contradicting requirements and so on (especially if you are the only developer).

[...] worry about management of the project or even provide customer support?

That depends on the responsibilities you accepted when you took the position. For example, some positions require you to address customers directly. All other things being equal, I would attempt to avoid such positions (but it's up to you, and you may not have many job choices).


答:
I agree that always requiring reduction of LOC would lead to a code difficult to read.

However, maybe your coding style is too verbose and your reviewer thinks a more concise way is better. For example a comment that only explains one statement that is fairly obvious thanks to clear choice of variable names is useless.


答:
I think you should indeed strive to have functions with a small number of SLOC.

If LOC (lines of code) is a small number, how does it effect the code and if LOC is a larger number, how does it effect the code ?

Ideally, it should be easier to understand 8 lines of code at a glance than it should be to understand 30.

That doesn't mean that 30 LOC compressed in 8 lines will be easier to understand.

What do you think is the right way of doing things.

Normally in a function, I try to group it by levels of abstraction ("IO code here", "validation here", "computation here" and so on).

Then, I split it up in blocks, separated by a blank line. If the code is more than around ten lines, I extract each block into a different function (I do that anyway for code that appears more than once). I've heard an argument about breaking performance this way (unnecessary function calls) but in practice I've never had a performance bottleneck caused by this.

That said, I've had functions with this extraction done, and after it the function was ~40 lines long (C code). If the code is as grouped as possible, I don't see a problem with longer functions.


答: In spite of its questionable purpose, I'm still in awe about a captcha solver userscript for MegaUpload. It's a GreaseMonkey script written in JavaScript that has a bitmap decoder and a neural network that recognizes the letters in the captcha image, and I repeat, all of this is made using JavaScript. </p>


问:
Is it fine to use logs in the Helper Methods?
I have been doubting for a long time whether or not to have a logger in methods (usually helper methods) like the one below.

Whether we can print it in the method or print the response where the method is called.

```<code>public String getAddNewUserUrl()
{
    String url      =   null;
    String mode     =   null;
    try
    {
        log.info("Inside getAddNewUserUrl "); // Can u use logs like this.
        mode        =   new ModeUtil().getMode();

        if("local".equalsIgnoreCase(mode) || "staging".equalsIgnoreCase(mode))
        {
            url     =   ResourceBundle.getBundle("ApplicationResources").getString("staging.url");
        }
        else
        {
            url     =   ResourceBundle.getBundle("ApplicationResources").getString("staging.url");
        }
        log.info("The url is  : " + url);  // Can u use logs like this

    }
    catch(Exception e)
    {
        e.printStackTrace();
        log.info("Exception in the method getMode ::"+e.getMessage());
    }
    return url;
</code>```

答:
It's ok to have logging in every part of your application, even in the helper methods, if you can set different logging levels.

```<code>log.info("The url is  : " + url, LogLevel.Debug);
</code>```

This ensures that you can turn it on or off depending on how much info you want to gather.
Having it always on, that's not so smart.

Body:
It's ok to have logging in every part of your application, even in the helper methods, if you can set different logging levels.

log.info("The url is  : " + url, LogLevel.Debug);

This ensures that you can turn it on or off depending on how much info you want to gather.
Having it always on, that's not so smart.


答:
Logs are supposed to be put in every part of your application to provide meaningful information to developer/tester/debuggers or any other stake holder.

Just be careful to put appropriate logging level in your helper classes/utilities especially if you going to distribute it as jar.

Title: Is it fine to use logs in the Helper Methods?

Body:
I have been doubting for a long time whether or not to have a logger in methods (usually helper methods) like the one below.

Whether we can print it in the method or print the response where the method is called.

```<code>public String getAddNewUserUrl()
{
    String url      =   null;
    String mode     =   null;
    try
    {
        log.info("Inside getAddNewUserUrl "); // Can u use logs like this.
        mode        =   new ModeUtil().getMode();

        if("local".equalsIgnoreCase(mode) || "staging".equalsIgnoreCase(mode))
        {
            url     =   ResourceBundle.getBundle("ApplicationResources").getString("staging.url");
        }
        else
        {
            url     =   ResourceBundle.getBundle("ApplicationResources").getString("staging.url");
        }
        log.info("The url is  : " + url);  // Can u use logs like this

    }
    catch(Exception e)
    {
        e.printStackTrace();
        log.info("Exception in the method getMode ::"+e.getMessage());
    }
    return url;
</code>```


答: Simple: if the employee does not make comments, tell him to press shift+alt+j for auto comment in each method simultaneously with entering the code. please do code revision for avoid these problems.


答:
This is largely an extension of tdammers answer, but perform code reviews at regular intervals.

Having him (and other developers) sit down, walk through their code, and more or less defend in front of their superiors and peers will make everybody better coders and will add real value over a relatively short period of time.  In the short term, it will give the developer in question no excuse to, at the time of review, properly comment his code.

EDIT: I'm unsure as to why somebody would downvote my suggestion - perhaps I took for granted that the benefits of code review would be common knowledge... please see this thread as to a community analysis of the practice:

Is code reviewing good practice?


问:
What problem domain is LINQ made for?
Each time I see a question posted on Stack Overflow on C#, I see at least one or two answers posted that solve a problem with LINQ. Usually people with very high reputation seem to use LINQ like pros.

So my question is, what problem domain is LINQ supposed to be used for?

Also on side notes: Are there any purposes in which it should be avoided? Does the size of dataset affect the performance of LINQ queries?

答:
LINQ is primarily designed to allow pure functional queries and transformations on sequences of data (you will notice that all the LINQ extensions take Func delegates but not Action delegates). Consequently the most common case of a loop that does not fit with LINQ very well is one that is all about non-pure functional side effects, e.g.

```<code>foreach(var x in list) Console.WriteLine(x);
</code>```

To get better at using LINQ, just practice using it.

Every time you are about to write a for or foreach loop to do something with a collection, stop, consider if it's a good fit for LINQ (i.e. it's not just performing an action/side effect on the elements), and if so force yourself to write it using LINQ.

You could also write the foreach version first then rewrite to a LINQ version.

As svick points out, LINQ should be about making your program more readable. It is usually good at this as it tends to emphasize the intent of the code rather than the mechanism; however if you find you cannot make your queries more readable than a simple loop, feel free to stick with the loop.

If you need exercises to practice, most functional programming exercises will map nicely to LINQ e.g. 99 problems (especially the first 20 or so) or project euler.

Title: What problem domain is LINQ made for?

Body:
LINQ is primarily designed to allow pure functional queries and transformations on sequences of data (you will notice that all the LINQ extensions take Func delegates but not Action delegates). Consequently the most common case of a loop that does not fit with LINQ very well is one that is all about non-pure functional side effects, e.g.

foreach(var x in list) Console.WriteLine(x);

To get better at using LINQ, just practice using it.

Every time you are about to write a for or foreach loop to do something with a collection, stop, consider if it's a good fit for LINQ (i.e. it's not just performing an action/side effect on the elements), and if so force yourself to write it using LINQ.

You could also write the foreach version first then rewrite to a LINQ version.

As svick points out, LINQ should be about making your program more readable. It is usually good at this as it tends to emphasize the intent of the code rather than the mechanism; however if you find you cannot make your queries more readable than a simple loop, feel free to stick with the loop.

If you need exercises to practice, most functional programming exercises will map nicely to LINQ e.g. 99 problems (especially the first 20 or so) or project euler.


答:
First of all, testing, or better said Quality Assurance, is not only about testing the funcionality by clicking through the application. Quality Assurance is about processes. Not only to test the application to find the errors, also they have to prevent developers from doing them.

Product specification + use cases

Even if everybody know (or think they do) what the product functionality is, it must be written down. You can't test an application without a clear specification. A specification defines what is correct behavior and what is a bug.

Unit tests, Integration tests

Tests of the internals which are difficult to test through the UI, exceptional application states. This is a must for every bigger system.
Both types of tests have also another interesting property - they force you to go through every part of the code again and you'll realize bugs/architecture problems you never saw before.

Code Quality & Coding Standards

One of the tasks QA should do is measuring code complexity. Low complexity code reduces the possibility of errors (preventing bugs).

Code reviews

When a project reaches a given size, or it is used by a lot of users, code reviews are a must. Another programmer always finds code problems/bugs you had missed. Programmer are sometimes blind to obvious bugs in their own code.

Documentation

Document your code and its architecture, it will help you realize possible flaws (my own experience).

Testers

Tester is not a monkey who just clicks through the UI. A tester takes the specification & use cases and creates test cases. Then he/she test them one by one. If a bug is reported by end users, a test case must be added for that.

A programmer should never create the specification (1). You are not there to decide the funcionality. Usually they have to speak to the end users, create graphic designs etc. It's a time consuming task. If a programmer decides the funcionality, usually it ends up with "it's okey but could you change everything in this window by tomorrow?" "this is not what I wanted" "it works but it's hard to use" "it's missing the only functionality we really needed".

What a programmer can achieve is 2, 3 and 5.

You need another programmer for 4. Any company with a big IT project and only one developer who knows the system steps on a very dangerous ground.

If you don't have enough time, don't ever bother to create test cases (5). A dedicated person is usually needed since it takes a lot of time.
Without test cases, testing is just a joke.

Title: Is a programmer supposed to be self-sufficient?

Body:
At my current workplace, we don't have any testers, the rationale for that from the management being: "if we had testers, you wouldn't test your own code at all". This kind of thinking seems to be detrimental to product quality, as while I do test my own code, there are a lot of things that I will miss just for the fact that I know the system inside out and don't know how to use it "wrong". Black box testing doesn't really work as I subconsciously avoid the pitfalls that a dedicated tester would fall into. A lot of my time goes into fixing bugs that have slid into production code and found by the end user.

The system in question is large but is developed solely by me. This has also caused some managemental duties to fall on my lap, such as defining schedules and working on specifications.

Should these kind of tasks be my responsibility? I see myself strictly as a programmer and nothing else. And if these are my responsibility, to what extent? When is a project so large that it requires testers? Should a programmer have to refine the specification, worry about management of the project or even provide customer support?

Note

Some might have got the impression that I am against widening my responsibilities – that is not the case, I'm eager to get a role that involves more management duties, but currently it is not in my job description. Until I'm officially employed as such or the additional duties start showing in my paycheck, I'm going to think of myself as 'just' a programmer. Unfortunately, as a junior developer, shifting to managerial duties is not going to happen very soon.

Excellent answers so far, keep them coming if you have something to add or personal experiences to share!


答:
There's only one thing that lines of code measures and that's the number of lines of code you have.  Everything else is hot air, speculation and prejudice.

There are a lot of great examples in the other answers of how having fewer lines of code can make something worse, not better, but one core point seems to have been missed, and that's: how many lines do you actually need to do this job?

If accomplishing your task needs 10 lines of code, so be it, you do it in 10 lines.  If accomplishing it needs 10,000 lines, then likewise so be it, you do it in 10,000 lines.  The cult of "fewer lines of code" will not make the latter example any better if you do it in 1,000 or 2,000 or whatever.  The job needed 10,000 lines and those 10,000 lines may include layers of validation, error checking, security, etc that would be missed if you did it in fewer.

Edit

Since this has garnered a few downvotes I think I need to be more specific about what I'm saying here.  The first thing anyone should do is read this - the point that I'm asking you to take away from it is that the high-level code you write may bear little or no resemblance to the machine code that actually gets executed.  The relevance of this point is that a focus on high-level code without an understanding of what actually goes on underneath is misguided.

Now consider the following:

It's possible to write really bad, slow code with just a single line - note the comment on the ternary operator at the end of the link I provided.

If a line of code makes an API call (or other call into an external library) then you have by definition little in the way of direct control over what gets executed or how efficient it is.

Error checking, cleanup, etc all add extra lines of code and I'm quite certain that even the most ardent fan of fewer lines of code wouldn't argue against those.

So - lines of code is a broken metric.  Fewer lines of code doesn't guarantee extra performance - that API or library call is one line and can be outside of your control.  Fewer lines of code doesn't guarantee extra robustness - error checking and cleanup will always require extra lines.  Fewer lines of code doesn't even guarantee extra readability or maintainability - you only need to look at the IOCCC to know that.

So what does fewer lines of code guarantee?  Fewer lines of code, that's all.

It's deeply wrong to focus on doing the same job in 2,000 lines versus in 10,000.  Instead focus on code that works, that is robust, that performs within accepted tolerances, and that is easier to read and maintain going forward.  Sometimes that means you can do it in a low LoC count, sometimes it means you have to do it in a higher LoC count, but the LoC count should not be what's important here.