问:
Notify players every x seconds in a multiplayer game
I'm working on a realtime multiplayer game using Django and gevent-socketio, I'm facing some issues:

I need to send an update of the game state to connected players every X seconds (~4 seconds), so basically I need to execute a function every x seconds which does some calculations and sends the connected players the new game state. What is the easiest way to achieve this? is using cron a good choice here or should I look for other tools like celery or ...?

Also, the function needs to operate on some data. querying the database every time the function is executed does not seems to be a good idea, where can I store the game data (which is updated frequently during the game)?

答:
Regarding your first question:

If the type of game allows it, I would advice a round-robin strategy. You send the game state in equal intervals you obtain by dividing the maximum time between updates by the number of players that are connected, and send the state as a series of updates to the single connected players.

Like that you avoid performance issues due to CPU spikes, as mentioned in some of the comments to @Kolyunya's answer.

As an aside this strategy allows the following optimisation:

Adapting the maximum time between updates to the number of players that are connected at a given time:

If the number of players is low you can set the maximum update time low as well, so the few players can have faster updates.

For the implementation I second @Brian's tip to see if this strategy suits your needs.

Regarding your second question:

You should keep your game state in memory, redis would be a obvious choice.


问:
Which approach to follow for developing applications targeting multiple platforms?
For applications targeting multiple platforms, I see mainly two development approaches-

Go for some JAVA-like development platform. Have one code solution and let the intermediate runtime handle different platforms. If something goes wrong in any platform, tweak the code a bit. But keep it same for all.

Make modular code separating core logic and UI. Develop separate UIs for respective platforms which will be calling same core libraries. Build the application separately for each of the target platforms.

So, which one to follow? I know, the answer will start with "It depends". But I want to hear your opinions on these approaches and the factors to be considered to choose any of them.

答:
In the Audacity sound editor we use wxWidgets as our cross platform library.  Since we need to link to C libraries, and we need speed and low level access a JVM approach would not work for us.  The GUI code is 95% the same on all platforms.  We use #ifdefs for the small variations.  However, we find it essential to have a developer working on each of the three platforms (Mac, Windows Linux), because even using the cross platform library it is too easy for a change on one machine to break things on another.

If you can get the performance you need, go for JVM.  If you can't, use QT or wxWidgets, and I would suggest QT over wxWidgets since it is less work to get it to look nice.

ContentLicense: CC BY-SA 2.5


问:
What degree of low-level programming can be achieved with Languages like Go?
Go and D provide garbage collection, and yet they claim to be system programming languages. What degree of low-level programming can be achieved with languages having garbage collection?

For low-level programming, I mean close to the hardware or being able to:

Runs directly in limited memory, with no latency, and performs well.  An example would be operating system kernels.

It runs on a software base, but still has to perform well. An example would be system utilities.

答:
I know this answer is late to the party, but I have a bit more to add.

Go is significantly higher-level than C/C++/D: it has no manual memory management, has a significant runtime environment, and uses more memory in general. You wouldn't want to write a kernel in Go because you'd have to port the (large) runtime to kernel space first. If you did, you'd probably run into some difficult-to-resolve performance issues as Go is not as CPU-friendly as C.

I believe D is meant mainly as a replacement for C++. C++'s role has largely been as a user-level systems/library programming language. Partly, this is because C was already dominant in the kernel when C++ started becoming popular. The other part is that the C++ runtime, while mostly limited to the memory allocator, has to be ported to the kernel as well. There have certainly been many C++ projects in kernel space, but it is not dominant there.

D has positioned itself to be an excellent library-writing language, and limited its in-kernel usefulness by requiring a larger runtime than C++ (or certainly than C). If you strip it down a bit (no GC, no standard libraries, no thread-scoped variables, some significant unsafe code segments, ...) you can make it run in the kernel easily, but then you're getting rid of many of its improvements over C++.

However, its performance characteristics and the ability to write low-level code are excellent and (more or less) match those of C or C++. I would especially recommend it for user-level projects, but there are some great things it can add to a kernel project as well (even if some of its nicest features aren't easily accessible there).

ContentLicense: CC BY-SA 3.0


问:
Improve Bad testing
We have a large team of developers and testers. The ratio is one tester for every one developer.

We have full bug tracking and reporting systems in place.

We have test plans in place.

Every change to the product, the testing team is involved in the design of the feature and are included in the development process as much as possible.

We build in small iterative blocks, using scrum methodology and every scrum they are included in, including the grooming sessions etc.

But every release of the product, they miss even the most simple and obvious defects.

How can we improve this?

答:
Are the test plans correct?

Do your requirements change a lot. Do the test team have enough time to update their test plans? Who validates that the test plans are correct?

Are there certain testers whose sections end up with more bugs than others?

Looking at it another way, there will always be bugs, but it's the "simple and obvious defects" phrase that sticks out.

Are you sure your dev team have enough unit tests in place? There is nothing I hate more as a dev when someone finds an "simple and obvious defect". Why didn't I catch that? Sure some will get through, but if there is a lot, then I am doing something wrong.

Title: Improve Bad testing


答:
Any methodology is only as good as your application of it.  Somewhere in the process (or perhaps at multiple places), something is not getting done adequately.  Hypothetical examples:

Tester X goes and tells his friend developer Y about a bug, rather than putting it into the bug tracking system.  It doesn't really get fixed, and the bug is forgotten.

The testing plan for feature A is not detailed enough.  It says "user can successfully open the file".  But it doesn't say what kind of file to use, so the tester only tries it with a small test file, rather than a 1,000,000 line file that might be used in real life.  The program passes the test, but doesn't work with real-world data.

The testing is rigorous, but after the testing process, someone notices small feature Z that is missing.  This tiny feature is added without retesting (it is a trivial feature anyway, what could it break?) and the release is rushed out the door.  But this breaks the release version.

And so on.  It is impossible to tell where your specific process is going wrong.  But it should be easy get at the root cause by looking at the bug reports and asking simple questions:

Was the bug in functionality covered in the testing plan?

Yes. Was the specific failure covered by a test case?

Yes. Was this test run and passed in the version that shipped?

Yes. The tests themselves are inadequate and need to be improved.

No. The process for releasing the software is broken if software that doesn't pass your own testing requirements was shipped.

No. Test cases are not rigorous enough or not specific enough.

No. The tests do not cover all functionality.

I do find one thing suspicious.  You say:

the testing team is involved in the design of the feature and are
  included in the development process as much as possible.

What kind of input are the testers giving here?

One essential feature of good testing is that the testers are separate from the process of creating the software.  They are not making decisions about the design and functionality of the program.  And they certainly should not be writing code.  If they are doing things like this on your project, then your testers are not really testers, and your testing process is broken.  Why is this such a problem?

Testers who help design features are likely to make the same assumptions made during the design process and less likely to discover problems due to those assumptions being broken. Also, due to human nature, they will be less free to criticize the way things work if they helped design the way it works.

Testers who also write the code are far less productive at finding bugs. None of us want our own code to break, so subconsciously we will make the testing less rigorous, avoiding things that might be a problem, rationalizing to ourselves "that doesn't really need to be tested, it would never happen anyway, etc...".  And the same assumptions used in writing the code will also be used in the testing, again preventing finding bugs that break those assumptions.

Title: Improve Bad testing

Body:
We have a large team of developers and testers. The ratio is one tester for every one developer.

We have full bug tracking and reporting systems in place.

We have test plans in place.

Every change to the product, the testing team is involved in the design of the feature and are included in the development process as much as possible.

We build in small iterative blocks, using scrum methodology and every scrum they are included in, including the grooming sessions etc.

But every release of the product, they miss even the most simple and obvious defects.

How can we improve this?


问:
Where can I get free public raw data?
In web development, a mashup is a web page or application that uses and combines data, presentation or functionality from two or more sources to create new services.

There are many sources for getting data (raw information). Governments are good examples.

However many of those cost and have complicated licensing.
For instance SMHI in Sweden sells temperature data meanwhile our neighbor country Norway gives it away for free.

One would say what kind of application are you building and what data do you need?  I would say give me some interesting data and I will do a good mashup of it. I could just plot it on Google maps for instance.

So the question is: Where can I get free data?

Note: I'm not interested in data mining using a webcrawler!

Please share your sources with me.

答:
There is data all over the place of every type. Best approach is to figure out what type of data you need, find a website that has it, and see if the site offers an API. However, here are some general sources:

Freebase

MediaWiki API

data.gov

Title: Where can I get free public raw data?


问:
Readable regular expressions without losing their power?
Many programmers know the joy of whipping up a quick regular expression, these days often with help of some web service, or more traditionally at interactive prompt, or perhaps writing a small script which has the regular expression under development, and a collection of test cases. In either case the process is iterative and fairly quick: keep hacking at the cryptic-looking string until it matches and captures what you want and will reject what you don't want.

For a simple case result might be something like this, as a Java regexp:

```<code>Pattern re = Pattern.compile(
  "^\\s*(?:(?:([\\d]+)\\s*:\\s*)?(?:([\\d]+)\\s*:\\s*))?([\\d]+)(?:\\s*[.,]\\s*([0-9]+))?\\s*$"
);
</code>```

Many programmers also know the pain of needing to edit a regular expression, or just code around a regular expression in a legacy code base. With a bit editing to split it up, above regexp is still very easy to comprehend for anyone reasonably familiar with regexps, and a regexp veteran should see right away what it does (answer at the end of the post, in case someone wants the exercise of figuring it out themselves).

However, things don't need to get much more complex for a regexp to become truly write-only thing, and even with diligent documentation (which everybody of course does for all complex regexps they write...), modifying the regexps becomes a daunting task. It can be a very dangerous task too, if regexp is not carefully unit tested (but everybody of course has comprehensive unit tests for all their complex regexps, both positive and negative...).

So, long story short, is there a write-read solution/alternative for regular expressions without losing their power? How would the above regexp look like with an alternative approach? Any language is fine, though a multi-language solution would be best, to the degree regexps are multi-language.

And then, what the earlier regexp does is this: parse a string of numbers in format 1:2:3.4, capturing each number, where spaces are allowed and only 3 is required.

答:
Every language that uses regexes allows you to compose them from simpler blocks to make reading easier, and with anything more complicated than (or as complicated as) your example, you should definitely take advantage of that option. The particular trouble with Java and many other languages is that they don't treat regular expressions as "first-class" citizens, instead requiring them to sneak into the language via string literals. This means many quotation marks and backslashes that aren't actually part of regex syntax and make things hard to read, and it also means that you can't get much more readable than that without effectively defining your own mini-language and interpreter.

The prototypical better way of integrating regular expressions was of course Perl, with its whitespace option and regex-quoting operators. Perl 6 extends the concept of building up regexes from parts to actual recursive grammars, which is so much better to use it's really no comparison at all. The language may have missed the boat of timeliness, but its regex support was The Good Stuff(tm).


问:
How do I make my ASP.NET application take an action based on time?
I have an ASP.NET MVC4 application. Suppose that I wanted to execute some code (such as emailing a user) 2 days after a user has signed up.

One way is to have code executed on every request which scans the database for users and their sign-up time and execute code based on that. This seems messy and it depends on people visiting the application frequently.

Another way is to write a separate application that runs on the server 24/7 and performs the checking itself, perhaps scanning the database at 12:00am every day and performing its own code.

What's the good way to go about this?

答:
A scheduled task triggered by either the Task Scheduler or Sql Server is the way to go here. But if you really want to manage it within your webapp, you might want to look at something like Quartz.NET. It will let you do scheduled tasks from the CLR. Then your challenge is "how do I make sure the AppDomain stays up to run the tasks."

Another way to do it as a scheduled task yet keep most of the "smarts" on the server is to make it a task that can be called over HTTP with some sort of authorization key. This lets you write a relatively simple program to call it -- if not a simple shell script -- and to keep most of the complexity in the web app which is likely already capable of running the task.

Either way rolling your own task manager is really a path fraught with peril.


答: As of ASP.Net 4.0 you have access to an Auto-Start feature for your ASP.Net applications, which means that you can have a thread running like a Windows Service, but keep it running in the context of your ASP.Net application. It's a little fiddly to configure the first time you use it, but once you have it set up, it's very handy for keeping your whole application in one place.</p>

Title: How do I make my ASP.NET application take an action based on time?

Body:
I have an ASP.NET MVC4 application. Suppose that I wanted to execute some code (such as emailing a user) 2 days after a user has signed up.

One way is to have code executed on every request which scans the database for users and their sign-up time and execute code based on that. This seems messy and it depends on people visiting the application frequently.

Another way is to write a separate application that runs on the server 24/7 and performs the checking itself, perhaps scanning the database at 12:00am every day and performing its own code.

What's the good way to go about this?


问:
Hosting long-running worker inside IIS
Is it possible to host a long-running worker inside ASP.NET application? The worker (for it's a windows service) is reading from a queue sending messages to a WCF service.
Would it be possible to run this "light long-running worker" inside an ASP.NET application? I read about setting pool start mode to "always running" and then using an AppStart class, which starts the worker.

I would like to get rid of the windows service (deployments and debugging problems) and embed this work inside IIS. Any suggestion?

答:
It is indeed possible to do what you describe- I am currently working on an application that uses the Asp.Net Auto-start feature to have a worker thread behaving like a windows service but hosted inside IIS so you keep all your code in one place.

It is very handy and after the somewhat fiddly configuration it seems to work more or less as advertised although running a service in IIS rather than as a Windows service does have a slight impact on reliability in my ( non scientifically validated ) opinion. Certainly a useful tool, however and something that will only get better as it becomes more popular.

Edit: After a couple more months of using this configuration, it becomes clear that it will sometimes behave in slightly curious ways - for example starting multiple instances of the same thread - and it can generally be a little quirky. It also isn't necessarily any easier to troubleshoot than a regular windows service, although it probably is easier to deploy. On the application I am working with, we are looking at moving our long-running services from IIS to Windows Services.

Title: Hosting long-running worker inside IIS


答:
I like to use Expresso: http://www.ultrapico.com/Expresso.htm

This free application has the following features that I find useful over time:

You can simply copy and paste your regex and the application will parse it for you

Once your regex is written, you can test it directly from the application (the application will give you the list of captures, replacements...)

Once you have tested it, it will generate the C# code to implement it (note that the code will contain the explanations about your regex).

For example, with the regex you just submitted, it would would look like:

Of course, giving it a try is worth a thousand word describing it. Please also note that I'm note related in any way with the editor of this application.

Title: Readable regular expressions without losing their power?

Body:
Many programmers know the joy of whipping up a quick regular expression, these days often with help of some web service, or more traditionally at interactive prompt, or perhaps writing a small script which has the regular expression under development, and a collection of test cases. In either case the process is iterative and fairly quick: keep hacking at the cryptic-looking string until it matches and captures what you want and will reject what you don't want.

For a simple case result might be something like this, as a Java regexp:

```<code>Pattern re = Pattern.compile(
  "^\\s*(?:(?:([\\d]+)\\s*:\\s*)?(?:([\\d]+)\\s*:\\s*))?([\\d]+)(?:\\s*[.,]\\s*([0-9]+))?\\s*$"
);
</code>```

Many programmers also know the pain of needing to edit a regular expression, or just code around a regular expression in a legacy code base. With a bit editing to split it up, above regexp is still very easy to comprehend for anyone reasonably familiar with regexps, and a regexp veteran should see right away what it does (answer at the end of the post, in case someone wants the exercise of figuring it out themselves).

However, things don't need to get much more complex for a regexp to become truly write-only thing, and even with diligent documentation (which everybody of course does for all complex regexps they write...), modifying the regexps becomes a daunting task. It can be a very dangerous task too, if regexp is not carefully unit tested (but everybody of course has comprehensive unit tests for all their complex regexps, both positive and negative...).

So, long story short, is there a write-read solution/alternative for regular expressions without losing their power? How would the above regexp look like with an alternative approach? Any language is fine, though a multi-language solution would be best, to the degree regexps are multi-language.

And then, what the earlier regexp does is this: parse a string of numbers in format 1:2:3.4, capturing each number, where spaces are allowed and only 3 is required.


答:
Just guessing... could you be suffering from a Test Pyramid with feet of clay (poor or no unit test suite) ?

"Simple and obvious defects" sounds like something that should be taken care of right from early development stages.

Also, it might be a good idea (if not already the case) to include quality assurance as part of the definition of Done for your user stories. Having the testing team test during the sprint and reject any non-compliant story is much more efficient than testing once per release.

Title: Improve Bad testing


答:
The simplest way would be to still use regex but build your expression from composing simpler expresssions with descriptive names e.g. http://www.martinfowler.com/bliki/ComposedRegex.html (and yes this is from string concat)

however as an alternative you could also use a parser combinator library e.g. http://jparsec.codehaus.org/ which will give you a full recursive decent parser. again the real power here comes from composition (this time functional composition).


问:
How do you manage the testing of your Android software on physical devices?
I'm in charge of managing mobile application development at my company, and I am currently building a mobile device "library" for testing. Essentially, we want to have a representative device in-house for each of the OSes we are developing for, currently iOS (iPhone-only), Blackberry, and Android. Simulators only go so far, but I'm placing into the process a step to test software on the devices themselves.

The problem we're finding is with Android. I don't think any of us here ever really understood just how fragmented the whole platform is until we started looking at devices to acquire. We are going to wait until v2.3 of Android is released, but which products to choose? Do we go by the most popular by market share? Do we get a small range of products by specs from least to most powerful overall?

We're trying to avoid having to manage a dozen different devices to test each app, if not because of cost if only for the repeated time sink.

How do you manage the testing of your Android software on physical devices?

UPDATE: To bring some context to what I am asking, the applications being created are for educational purposes only, and there are no plans to use any of the hardware features of the devices themselves. We are simply displaying content in a variety of ways: eBooks, test questions, flash cards, etc. that may or may not be downloadable content. So, no cameras, GPS, user-driven content management, or anything like that; just as device-agnostic as we can possibly make it.

答:
Pick a device with the oldest OS version your code supports.

Pick a relatively slow device, so that you notice any performance problems.


答:
The "verbose" mode offered by some languages and libraries is one of the answers to these concerns. In this mode, whitespace in regexp string is stripped out (so you need to use \s) and comments are possible. Here's a short example in Python which supports this by default:

```<code>email_regex = re.compile(r"""
    ([\w\.\+]+) # username (captured)
    @
    \w+         # minimal viable domain part
    (?:\.w+)    # rest of the domain, after first dot
""", re.VERBOSE)
</code>```

In any language that doesn't, implementing a translator from verbose to "normal" mode should be a simple task. If you're concerned about your regexps' readability, you would probably justify this time investment pretty easily.

Body:
The "verbose" mode offered by some languages and libraries is one of the answers to these concerns. In this mode, whitespace in regexp string is stripped out (so you need to use \s) and comments are possible. Here's a short example in Python which supports this by default:

email_regex = re.compile(r"""
    ([\w\.\+]+) # username (captured)
    @
    \w+         # minimal viable domain part
    (?:\.w+)    # rest of the domain, after first dot
""", re.VERBOSE)

In any language that doesn't, implementing a translator from verbose to "normal" mode should be a simple task. If you're concerned about your regexps' readability, you would probably justify this time investment pretty easily.


问:
FOSS licensing decision: What to read? What factors to consider?
Which reasoning would you follow to choose one license over another? Which literature would you expect someone to read, if he wants to make a meaningful decision about licensing?

I specifically don't go too much into detail about my current project, because I am looking to learn how to make this decision correctly without getting a degree in law first.

答:
To get started, you really need to decide what kind of permissions you want to give. If you want your project very open with a lax license, go with a BSD or MIT style license. The only difference between those two is that the BSD requires people who distribute your software to keep your name with it, the MIT doesn't. If you want copyleft (requiring modified versions to have the same license), go with the GPL (or the LGPL if you are okay with your software being bundled). These licenses are often called viral licenses because they require forks of your program to be under that same license. They are also the #1 and #3 most popular licenses if I recall correctly. If you want the same kind of protections as the GPL minus the copyleft, look at the Apache License, which is the #2 most popular license and is also used for it's sections regarding patents. Documentation should also be openly licensed, try the GFDL or a Creative Commons license for it.

I don't really know how to directly answer this question. I have provided as much info as I can on licensing, but I'm not sure exactly what you want.

Finally, for info on how to implement your license, please look at this answer.

I am not a lawyer, this is not legal advice. Blah blah blah, etc.


答:
The approach we use is to create an NServiceBus Saga with a timeout.

The Web application sends a message, the saga is started and requests a timeout for 2 days time. If no other action has occurred which would prevent the email being sent, the timeout wakes the saga back up and sends the email. The cancellation request also sends a message which wakes the saga and sets a cancelled flag.

This approach is nicely decoupled from the web application and easily scaled, also the messages are queued so we can shut down the processing side for updates without shutting off the web app.

The other nice thing about this architecture is that it is event based rather than polling based so there is less contention and traffic on the database.


答:
The key to documenting the regular expression is documenting it.  Far too often people toss in what appears to be line noise and leave it at that.

Within perl a single /x tells the regular expression parser to ignore most whitespace that is neither backslashed nor within a bracketed character class.

The above regular expression would then become:

$re = qr/
  ^\s*
  (?:
    (?:
      ([\d]+)\s*:\s*
    )?
    (?:
      ([\d]+)\s*:\s*
    )
  )?
  ([\d]+)
  (?:
    \s*[.,]\s*([\d]+)
  )?
  \s*$
/x;
```
<p>Yes, its a bit consuming of vertical whitespace, though one could shorten it up without sacrificing too much readability.</p>
<blockquote>
<p>And then, what the earlier regexp does is this: parse a string of numbers in format 1:2:3.4, capturing each number, where spaces are allowed and only 3 is required.</p>
</blockquote>
<p>Looking at this regular expression one can see how it works (and doesn't work).  In this case, this regex will match the string <code>1</code>.</p>
<p>Similar approaches can be taken in other language.  The python re.VERBOSE option works there.</p>
<p>Perl6 (the above example was for perl5) takes this further with the concept of rules which leads to even more powerful structures than the PCRE (it provides access to other grammars (context free and context sensitive) than just regular and extended regular ones).</p>
<p>In Java (where this example draws from), one can use string concatenation to form the regex.</p>
<pre class="lang-java prettyprint-override"><code>Pattern re = Pattern.compile(
  "^\\s*"+
  "(?:"+
    "(?:"+
      "([\\d]+)\\s*:\\s*"+  // Capture group #1
    ")?"+
    "(?:"+
      "([\\d]+)\\s*:\\s*"+  // Capture group #2
    ")"+
  ")?"+ // First groups match 0 or 1 times
  "([\\d]+)"+ // Capture group #3
  "(?:\\s*[.,]\\s*([0-9]+))?"+ // Capture group #4 (0 or 1 times)
  "\\s*$"
);
</code>```

Admittedly, this creates many more " in the string possibly leading to some confusion there, can be more easily read (especially with syntax highlighting on most IDEs) and documented.

The key is recognizing the power and "write once" nature that regular expressions often fall into.  Writing the code to defensively avoid this so that the regular expression remains clear and understandable is key.  We format Java code for clarity - regular expressions are no different when the language gives you the option to do so.

Body:
The key to documenting the regular expression is documenting it.  Far too often people toss in what appears to be line noise and leave it at that.

Within perl a single /x tells the regular expression parser to ignore most whitespace that is neither backslashed nor within a bracketed character class.

The above regular expression would then become:

$re = qr/
  ^\s*
  (?:
    (?:
      ([\d]+)\s*:\s*
    )?
    (?:
      ([\d]+)\s*:\s*
    )
  )?
  ([\d]+)
  (?:
    \s*[.,]\s*([\d]+)
  )?
  \s*$
/x;

Yes, its a bit consuming of vertical whitespace, though one could shorten it up without sacrificing too much readability.

And then, what the earlier regexp does is this: parse a string of numbers in format 1:2:3.4, capturing each number, where spaces are allowed and only 3 is required.

Looking at this regular expression one can see how it works (and doesn't work).  In this case, this regex will match the string 1.

Similar approaches can be taken in other language.  The python re.VERBOSE option works there.

Perl6 (the above example was for perl5) takes this further with the concept of rules which leads to even more powerful structures than the PCRE (it provides access to other grammars (context free and context sensitive) than just regular and extended regular ones).

In Java (where this example draws from), one can use string concatenation to form the regex.

Pattern re = Pattern.compile(
  "^\\s*"+
  "(?:"+
    "(?:"+
      "([\\d]+)\\s*:\\s*"+  // Capture group #1
    ")?"+
    "(?:"+
      "([\\d]+)\\s*:\\s*"+  // Capture group #2
    ")"+
  ")?"+ // First groups match 0 or 1 times
  "([\\d]+)"+ // Capture group #3
  "(?:\\s*[.,]\\s*([0-9]+))?"+ // Capture group #4 (0 or 1 times)
  "\\s*$"
);

Admittedly, this creates many more " in the string possibly leading to some confusion there, can be more easily read (especially with syntax highlighting on most IDEs) and documented.

The key is recognizing the power and "write once" nature that regular expressions often fall into.  Writing the code to defensively avoid this so that the regular expression remains clear and understandable is key.  We format Java code for clarity - regular expressions are no different when the language gives you the option to do so.

ContentLicense: CC BY-SA 4.0


问:
Worst coding standard you've ever had to follow?
Have you ever had to work to coding standards that:

Greatly decreased your productivity?

Were originally included for good reasons but were kept long after the original concern became irrelevant?

Were in a list so long that it was impossible to remember them all?

Made you think the author was just trying to leave their mark rather than encouraging good coding practice?

You had no idea why they were included?

If so, what is your least favorite rule and why?

Some examples here

答:
Having what amounted to C header files, in a Java project.

Interfaces exist for some good reasons, but this standard mandated an interface (foo.java) for every single class (fooImpl.java) whether it made any sense or not. Lots of stuff to keep in sync, complete disruption of Eclipse click-into-method, pointless busy-work.

The build system enforced it, but I cannot imagine what the original purpose was. Fortunately we ditched it for new code when we switched to a new version-control and build system, but there's still a lot of it about.

At the same time we also ditched the stupid version-control-info-in-file-comments habit which had been mandatory.


答:
A number of people have mentioned composing from smaller parts, but no one's provided an example yet, so here's mine:

```<code>string number = "(\\d+)";
string unit = "(?:" + number + "\\s*:\\s*)";
string optionalDecimal = "(?:\\s*[.,]\\s*" + number + ")?";

Pattern re = Pattern.compile(
  "^\\s*(?:" + unit + "?" + unit + ")?" + number + optionalDecimal + "\\s*$"
);
</code>```

Not the most readable, but I feel like it's clearer than the original.

Also, C# has the @ operator which can be prepended to a string in order to indicate that it is to be taken literally (no escape characters), so number would be @"([\d]+)";

Body:
A number of people have mentioned composing from smaller parts, but no one's provided an example yet, so here's mine:

string number = "(\\d+)";
string unit = "(?:" + number + "\\s*:\\s*)";
string optionalDecimal = "(?:\\s*[.,]\\s*" + number + ")?";

Pattern re = Pattern.compile(
  "^\\s*(?:" + unit + "?" + unit + ")?" + number + optionalDecimal + "\\s*$"
);

Not the most readable, but I feel like it's clearer than the original.

Also, C# has the @ operator which can be prepended to a string in order to indicate that it is to be taken literally (no escape characters), so number would be @"([\d]+)";


问:
Performance considerations when using Go for my project
I'm looking at using Go (aka golang) for a project (a SQL database, but that mostly doesn't matter here) where performance is critical, but under low load the primary bottleneck will be I/O to disk. In this case, I think Go would be great!

Under high load or while hitting cache a lot, CPU and memory utilization will increasingly become bottlenecks, and I'm worried that Go could make the high-end of the performance spectrum significantly lower than what C/C++/D might provide.

Can anyone with experience working in Go give some insight into how quickly that bottleneck is reached (networking applications are subject to the same bottleneck, typically) and what you can do to relax it in Go other than rewriting the bottleneck in a faster language?

Note my question was sufficiently different that I asked it anyway after reading related question:

https://softwareengineering.stackexchange.com/questions/160651/what-kind-of-software-is-done-best-with-the-go-programming-language

I have a specific application I'm asking about, and I've limited my concerns to performance (not usability, library support, development tools, etc).

答:
I come to Go as a mostly "dynamic languages" programmer.

With that said it seems like you get pretty close to C performance as long as you take care to do the same things you'd do in a fast C program, avoid excessive allocations, pick your algorithms, etc.

Go will make some of that much easier to do whole staying bug-free, so given the same development time my estimate is that your performance can be similar, too. (Buy you will definitely have more fun).


问:
How to determine most challenging test-cases to test any algorithm?
While solving any problem, we write algorithms. Some efficient, some not, some work, some fail. But sometimes we end up writing something which is mostly a success when we do a dry test run, perhaps, the way we frame the test data is prejudiced, but the algorithm fails in some other cases.

For some algorithms , the nature of data can be diverse and magnitude large, for example like the problem:

Find the maximum subsequence sum of an array of integers which contains both positive and negative numbers and return the starting and ending indices within the array.

Can anyone tell me is there any specific and generic thumb rule by which we can design the most stringent of test cases to test the correctness of algorithms like this one ?

答:
The best way that I know of is to write programs using the Test-Driven-Development methodology.  By writing your test cases first, and then only writing code that makes your test cases pass,  your algorithms will only implement your test cases, and therefore your code will have 100% coverage.

That's a bit tongue-in-cheek, of course.  Writing test cases and code that passes those test cases doesn't guarantee that your code won't have edge behavior beyond the test cases, and I still don't believe that you can simply grow a sensible architecture from the ground up using only red-green-refactor.

The way out is not to find the best possible testing algorithms (although that is certainly a good thing to do).  Rather, use best programming practices and design your programs in such a way that the amount of unexpected behavior is minimized.  Learning how to do that is the ongoing life and learning of every professional programmer.

You can also use code coverage tools to identify test cases.


答:
I will assume we are talking about a reasonably complex algorithm, i.e., one where you'd usually get out pen and paper and need quite some time before you arrive at the pseudocode for a solution.

In such a scenario, the most challenging test cases are the edge cases you probably didn't think of. Here, test-driven design can only get you so far, as the test coverage is severely limited by your imagination. In these cases, I try a branch-based approach. I try to create input sequences that will hit all possible control flow variations and assert important properties that help me believe that the algorithm is in fact correct.

The important thing is that for algorithmic problems, there is usually quite a high density of edge cases. It is easier to find out what these edge cases are if you can look at the algorithm, i.e., do whitebox testing. In TDD, you'd do the opposite: ideally, you write all your test cases before you even start implementing the code. However, if the problem was algorithmically challenging, you probably already made sure that your algorithm would return the correct solution for all instances that you managed to come up with upfront.

Title: How to determine most challenging test-cases to test any algorithm?

Body:
While solving any problem, we write algorithms. Some efficient, some not, some work, some fail. But sometimes we end up writing something which is mostly a success when we do a dry test run, perhaps, the way we frame the test data is prejudiced, but the algorithm fails in some other cases.

For some algorithms , the nature of data can be diverse and magnitude large, for example like the problem:

Find the maximum subsequence sum of an array of integers which contains both positive and negative numbers and return the starting and ending indices within the array.

Can anyone tell me is there any specific and generic thumb rule by which we can design the most stringent of test cases to test the correctness of algorithms like this one ?


答:
While I agree with Robert Harvey's statement, that test driven development is a useful approach, I often do get the impression that people think that with sufficiently many unit tests in place they are on the safe side and are allowed to stop thinking about useful tests. This, I think, is far from being true.

In my opinion creating reasonable tests is something between an art and good craftmansship, where knowing about existing tools and best practices together with thouroghly and correctly applying them is the craftmanship part. Good knowledge about common programming errors as an additional ingredient will also help to find useful tests. But you also have to understand the problem solved by the algorithm and need to get an idea of where an implementation is likely to fail. You will, at least partially, often be on your own here. I'd try to find at least one alternative algorithm and run them concurrently. Often you choose a specific one for performance -- in automated test you may use a slower one for croos checking. Whatever helps is somehting you can put into the best practices box.

Without spending too much time on it, for the problem you gave as an example I'd try to find an (automated) way to (randomly) create (many) sequences of varying length for which I can predict the result. Then feed them into the algorithm, and log the input in case it fails. In addition I'd do that for a fixed (not random) set of sequences as a protection against code changes which might break the code. I'd make sure there will be sequences with more than one solution and try to figure out whether sequences with overlapping solutions exist and if yes, try to create some of them, as well.

I'd like to add that for me, as a mathematician, an algorithm is either correct or wrong for at least one set of input data and it is something for which you can (often) give prove (the link is more or less randomly chosen) -- of course there is always the human factor....But even if you believe that, you cannot do that (give proof) for an implementation of a (sufficiently complex) algorithm, which is the thing which is actually under test.

Edit I think an example which is sufficiently obvious and elaborate at the same time might be useful. For this I'd like to draw your attention to Knuth's 'The art of computer programming' vol 2, where under the heading 'The classical algorithms' an algorithm is presented which shows how to divide an (arbitrary) positive integer by another positive integer. Knuth actually gives (a somewhat hard to understand) proof of why his algorithm is correct. A certain part of that proof shows that there is a straightforward way of performing a certain task in that algorithm and an exceptional case. The exceptional case (see step D3 in algorithm D if you really want to look it up) occurs extremely rarely and, actually, requires a more complex calculation.

Now while there is a quite obvious way to create unit tests for this algorithm (apply it to two integers and then perform the reverse operation to check whether you get to from where you started) it is absolutely not obvious how to make sure the exceptional case is tested. First you need to know about it (that is, you have to understand the hairy details of the proof of the algorithm), then you need to find data for which it will actually occur. Doing the whole chain (finding the algorithm, which is, in this case, done for you  by the author, finding a proof for it, identifying the exceptional cases and making sure test data is found which test this as well) is not covered by any kind of best practice.


答:
For some things, it might help to just use a grammar like BNF. These can be much easier to read than regular expressions. A tool such as GoldParser Builder can then convert the grammar into a parser that does the heavy lifting for you.

The BNF, EBNF, etc. grammars can be much easier to read and make than a complicated regular expression. GOLD is one tool for such things.

The c2 wiki link below has a list of possible alternatives which can be googled, with some discussion on them included. It is basically a "see also" link to top off my grammar engine recommendation:

Alternatives To Regular Expressions

Taking "alternative" to mean "semantically equivalent facility with different syntax", there are at least these alternatives to/with RegularExpressions:

Basic regular expressions

"Extended" regular expressions

Perl-compatible regular expressions

... and many other variants...

SNOBOL-style RE syntax (SnobolLanguage, IconLanguage)

SRE syntax (RE's as EssExpressions)

different FSM syntaces

Finite-state intersection grammars (quite expressive)

ParsingExpressionGrammars, as in OMetaLanguage and LuaLanguage (http://www.inf.puc-rio.br/~roberto/lpeg/lpeg.html)

The parse mode of RebolLanguage

ProbabilityBasedParsing...


问:
How can a commercially-driven open source project build an open source community?
I'm working on an ambitious, commercially-driven open source project* (it probably falls under Wikipedia's definition of Open Core).  I'm an engineer on the project, so I don't have too much sway over all of our policies.

The majority of the code is in public projects on GitHub, and we have public mailing lists, but that's pretty much the extent of our open source engagement.  We use the mailing lists to respond to questions from the community, but a lot of topics go unanswered because they are low quality or because we have deadlines and we don't want to spend too much time on the mailing list instead of delivering features.

We have a corporate-mandated contributor agreement that is more like a contributor deterrent IMO.

We currently few or no passionate community members who feel strongly enough to represent our support as a volunteer.

I've been able to find a few guides (Producing Open Source Software was perhaps the best) about building open source communities, but all of them are under the premise that a project is pure open source with the ability to be transparent to the community.

For the things I've described above, I know we can do better, but I don't know that I will be able to get management's buy-in on any of those.

What kind of things can a commercial open source project do to engage and build an open source community?

*(I've intentionally not explicitly named the project; the views in this question are my own and do not necessarily represent the views of the rest of my team or my employer.)

答:
So what?

As in, you need to ask yourself "so what?" in order to help determine the answer to your various questions.

So what

is it you're looking for from an open-source community?

does it mean to them to be a member of your open-source community?

will you do to demonstrate your commitment as the commercial backer to the members of the community?

will you do to protect the community should your employer choose not to continue development of the core project?

will you put in place to make it easier for your community to understand what your product is; what it can do; and how to work with it?

I was actually half-tempted to vote-to-close as Not a Real Question because this is a very, very broad question.  But I'll offer up questions in return in order to help you answer your own questions.

First off, what does having an open-source community around your core mean to your employer?  Either there's value, or there isn't.  Prioritize according to the perceived value to the business that will be generated.  In this chicken-and-egg game, the business has to commit first.

Next up, demonstrate how the business is going to commit to the community.  Is there a high barrier to understand the core? Put some tutorials out there.  Can the core do anything interesting?  Demonstrate it, or better yet continue to expand the core!  Reasonable questions from the community not getting answered by someone from the business means you're not committed.  Often times, you need a community evangelist within the company before the community finds its own evangelists.

Finally, consider what value there is to the community to use your core.  Your software needs to provide value to them.  That value could be "fun", making XYZ easier to do, or whatever, but there has to be some sort of appeal.  You have to protect the community's investment of time as well.  The talented folk who have the ability to advance your core aren't lacking from things to do.  They need to see the value and long-term payoff in contributing to your commercial entity.

Those are the issues you need to delve into in order to find the answers you're looking for.

As an aside, the contributor agreement issue may or may not really be an issue.  It's clear you don't like it.  So what?  Can you prove that it's detracting from contributing? Can you suggest a better way of protecting the company's ability to expand and control the project's direction while also protecting the community's contribution? I agree it may be an issue, but you have to evaluate it within the broader perspective.

Are there other projects within your employer that have successfully built a community of support?  Identify what they're doing and yours is not.  Run each of those differences against the So What? tests and implement the ones that matter.


问:
How do you write scalable Java?
I like to think I write good maintainable Java code, using best practices, design patterns and try and get good test coverage with unit tests.

Recently whilst looking for a job, I came across a couple of job adverts that have references to "scalable" code.

So, my question is, what is considered to be "scalable" code?

答:
As Robert mentioned in a comment, what they mean is that the software performs well under increasing volumes of load.

Any programming language has potential for code that performs or scales well and code that performs or scales poorly.

However, Java is an interesting language to be picky about in regards to scaling perhaps because of its popularity and low barrier-to-entry.

I see two reasons to be concerned about developers who claim expertise in Java but make poorly performing or poorly scaling software:

Commonly taught - every IT, SE and CS curriculum that I have seen teaches Java first.  At least in the US, it's the de facto programming 101 language.  Not a bad idea since it teaches the basics of control flow such as loops, functions, arrays, I/O etc. and allows for a "warming" into OOP.

It is popular and heavily marketed.

One of the dangers of garbage collected languages is that you do not need to understand how a computer works to make software with it.  You can even make software that performs fairly well without understanding memory management, CPU registers, GC algorithms, etc. these days.  However, if you want the software to scale, you need some understanding of these concepts (not necessarily an expert however).

For example, you write code that selects a bunch of data from a database table and then load it into an ArrayList in Java.  You try it in development and it works great.  Try it in QA, it works great.  Run it in production where the table has 2 million rows instead of the 10 rows from development and 2,000 users are selecting from it at the same time, suddenly your database CPU spikes up and your app servers run low on memory.  How would you handle this?

Title: How do you write scalable Java?


问:
Is there an accepted name for a fake object injected via a preprocessor seam?
I am dealing with some old, sloppy C++ code in which there is a structure with a lot of data members and functions. I want to test a class that I have implemented which uses the struct, but this dependency is going to make the unit tests extremely ugly. Inspired by Michael Feather's discussion of preprocessing seams in Working Effectively with Legacy Code, I am getting ready to solve this problem by faking the ugly structure with the preprocessor (not with inheritance) polymorphism as demonstrated in the following notional code.

test.cpp

```<code>#include <iostream>
#include "NotAsUgly.hpp"
#include "DependsOnUgly.hpp"
int main() {
    std::cout << DependsOnUgly().getDescription() << std::endl;
}
</code>```

DependsOnUgly.hpp

```<code>#ifndef _DEPENDS_ON_UGLY_HPP_
#define _DEPENDS_ON_UGLY_HPP_
#include <string>
#include "Ugly.hpp"
class DependsOnUgly {
public:
    std::string getDescription() {
        return "Depends on " + Ugly().getName();
    }
};
#endif
</code>```

Ugly.hpp

```<code>#ifndef _UGLY_HPP_
#define _UGLY_HPP_
struct Ugly {
    double a, b, ..., z;
    void extraneousFunction { ... }
    std::string getName() { return "Ugly"; }
};
#endif
</code>```

NotAsUgly.hpp

```<code>#ifndef _UGLY_HPP_ // Same name as in Ugly.hpp---deliberately!
#define _UGLY_HPP_
struct Ugly { // Once again, duplicate name is deliberate
    std::string getName() { return "not as ugly"; } // All that DependsOnUgly depends on
};
#endif
</code>```

Is there a special name for the type of fake object I'm using here? I'm inclined to call it a stub, but it is rather different than what I would normally think of as a stub. I would expect something called a stub to implement the interface of the class which is to be faked through inheritance. Here the implementation is only partial, and happens through the preprocessor.

Is there already a known name for this kind of fake/testing object? I want to name the header file that contains it (NotAsUgly.hpp in my example) to properly signify to other developers what it is for.

答: This question has gone almost two months without anything but generic answers, so I judge that this is not a well-known pattern with an existing name. Therefore I suggest the name Preprocessor Partial Mock, to indicate that it is injected via a preprocessor seam and is only a partial implementation of the class being mocked.


问:
Resources for improving your comprehension of recursion?
I know what recursion is (when a patten reoccurs within itself, typically a function that calls itself on one of its lines, after a breakout conditional... right?), and I can understand recursive functions if I study them closely. My problem is, when I see new examples, I'm always initially confused. If I see a loop, or a mapping, zipping, nesting, polymorphic calling, and so on, I know what's going just by looking at it. When I see recursive code, my thought process is usually 'wtf is this?' followed by 'oh it's recursive' followed by 'I guess it must work, if they say it does.'

So do you have any tips/plans/resources for building up skills in this area? Recursion is kind of a weird concept so I'm thinking the way to tackle it may be equally weird and inobvious.

答:
As much as I like SICP and Gödel, Escher, Bach: an Eternal Golden Braid, Touretzky's LISP: A Gentle Introduction to Symbolic Computation also does a good job on introducing recursion.

The basic concept is this: First, you have to know when your recursive function is finished, so it can return a result.  Then, you have to know how to take the unfinished case, and reduce it to something that you can recur on.  For the traditional factorial(N) example, you're finished when N <= 1, and the unfinished case is N*factorial(N-1).

For a much uglier example, there's Ackermann's function A(m,n).

```<code>A(0,n) = n+1.                                   This is the terminal case.
A(m,0) = A(m-1,1) if m > 0.                     This is a simple recursion.
A(m,n) = A(m-1, A(m, n-1)) if m > 0 and n > 0.  This one is ugly.
</code>```

Body:
As much as I like SICP and Gödel, Escher, Bach: an Eternal Golden Braid, Touretzky's LISP: A Gentle Introduction to Symbolic Computation also does a good job on introducing recursion.

The basic concept is this: First, you have to know when your recursive function is finished, so it can return a result.  Then, you have to know how to take the unfinished case, and reduce it to something that you can recur on.  For the traditional factorial(N) example, you're finished when N <= 1, and the unfinished case is N*factorial(N-1).

For a much uglier example, there's Ackermann's function A(m,n).

A(0,n) = n+1.                                   This is the terminal case.
A(m,0) = A(m-1,1) if m > 0.                     This is a simple recursion.
A(m,n) = A(m-1, A(m, n-1)) if m > 0 and n > 0.  This one is ugly.


答:
I suggest playing around with some ML-style functional languages like OCaml or Haskell. I found that pattern-matching syntax really helped me understand even relatively complicated recursive functions, certainly much better than Scheme's if and cond statements. (I learned Haskell and Scheme at the same time.)

Here's a trivial example for contrast:

```<code>(define (fib n)
   (cond [(= n 0) 0]
         [(= n 1) 1]
         [else (+ (fib (- n 1)) (fib (- n 2)))]))
</code>```

and with pattern matching:

```<code>fib 0 = 0
fib 1 = 1
fib n = fib (n - 1) + fib (n - 2)
</code>```

This example does not really do the difference justice--I never had problems with either version of the function. It's just to illustrate what the two options look like. Once you get to much more complex functions, using things like lists and trees, the difference becomes much more pronounced.

I particularly recommend Haskell because it's a simple language with very nice syntax. It also makes much easier to work with more advanced ideas like corecursion:

```<code>fibs = 0 : 1 : zipWith (+) fibs (drop 1 fibs)
fib n = fibs !! n
</code>```

(You won't understand the above code until you play a bit with Haskell, but rest assured that it's basically magical :P.) Sure you could do the same with streams in Scheme, but it's much more natural in Haskell.

Title: Resources for improving your comprehension of recursion?

Body:
I know what recursion is (when a patten reoccurs within itself, typically a function that calls itself on one of its lines, after a breakout conditional... right?), and I can understand recursive functions if I study them closely. My problem is, when I see new examples, I'm always initially confused. If I see a loop, or a mapping, zipping, nesting, polymorphic calling, and so on, I know what's going just by looking at it. When I see recursive code, my thought process is usually 'wtf is this?' followed by 'oh it's recursive' followed by 'I guess it must work, if they say it does.'

So do you have any tips/plans/resources for building up skills in this area? Recursion is kind of a weird concept so I'm thinking the way to tackle it may be equally weird and inobvious.

Body:
I suggest playing around with some ML-style functional languages like OCaml or Haskell. I found that pattern-matching syntax really helped me understand even relatively complicated recursive functions, certainly much better than Scheme's if and cond statements. (I learned Haskell and Scheme at the same time.)

Here's a trivial example for contrast:

(define (fib n)
   (cond [(= n 0) 0]
         [(= n 1) 1]
         [else (+ (fib (- n 1)) (fib (- n 2)))]))

and with pattern matching:

fib 0 = 0
fib 1 = 1
fib n = fib (n - 1) + fib (n - 2)

This example does not really do the difference justice--I never had problems with either version of the function. It's just to illustrate what the two options look like. Once you get to much more complex functions, using things like lists and trees, the difference becomes much more pronounced.

I particularly recommend Haskell because it's a simple language with very nice syntax. It also makes much easier to work with more advanced ideas like corecursion:

fibs = 0 : 1 : zipWith (+) fibs (drop 1 fibs)
fib n = fibs !! n

(You won't understand the above code until you play a bit with Haskell, but rest assured that it's basically magical :P.) Sure you could do the same with streams in Scheme, but it's much more natural in Haskell.


问:
Is it a bad design to specify default bindings when using Inversion of Control (IOC) containers and dependency injection (DI)?
I'm using Ninject, but this is not a Ninject-specific question. I'm wondering if the advanced and flexible capabilities of the IoC container are giving me enough rope to hang myself with a bad application design.

I've got a service interface:

```<code>public interface IBuilderService {
    IBuilder Create(string category, string clientID);
}
</code>```

and I lean on Ninject Factory Extension's ToFactory() to create what I'd guess you'd call a proxy implementation at runtime.

I have IBuilder bindings like this:

```<code>kernel.Bind<IBuilder>().To<NullObjectBuilder>(); // for unbound categories
kernel.Bind<IBuilder>().To<CatOneBuilder>();
kernel.Bind<IBuilder>().To<CatTwoBuilder>();
kernel.Bind<IBuilder>().To<CatThreeBuilder>();
kernel.Bind<IBuilder>().To<CatOneBuilder_ClientA>();
kernel.Bind<IBuilder>().To<CatOneBuilder_DefaultClient>(); // for unbound clients
// etc...
</code>```

I've been experimenting with custom instance providers and binding generators with Ninject (I'm guessing other IoC containers may have similar constructs for overriding basic binding behavior), but I have two distinct binding scenario goals:

if a category of binding cannot be resolved from the IBuilderService.Create category argument, then a NullObjectBuilder should be resolved - this is the default binding in this case.

if a clientID of a binding cannot be resolved, then a default client implementation like CatOneBuilder_DefaultClient for a category of CatOne should be resolved - this is the default binding in this case.

This is a situation where unknown category requests are expected, so it seems a shame to use expensive exception handling for something I know will happen very often.  I want there to be fallback behavior to "handle the unhandled" in a very plain way - in this case, it would be the NullObjectBulder for unknown categories, and *_DefaultClient for unknown clients of known categories - two distinct fallback behaviors for two different types of unknown behavior.

I'm having trouble keeping the two fallback behaviors mapped to their respective scenarios, but it's given me pause to think about the friction I'm feeling in fleshing out this design…

Is it a flawed design to have default bindings and the logic used to determine when one is needed contained in IoC custom implementations, rather than an exception being thrown?  Is this a mis-use of IoC containers?

答:
It is not misuse ... but it isn't necessarily good design either.

It really depends on the context.

If the purpose is to allow you to build simple configurations in a less verbose way, and the defaults are such that you achieve that goal ... then that would suggest that this is a good design.

If the purpose is to allow you be careless without getting exceptions and/or the defaulting is likely to result in configurations that are likely to be incorrect (versus what you meant) ... that would suggest a poor design.

Use (or not) of defaulting is a trade-off between the "goodness" of making things explicit, and the "badness" of being verbose.  You really need to make up your own mind what is going to work best for you in the context of your problem.

Or another way to look at it is to say that you should be selective in what "injection points" you define defaults for.


答:
It's out of print, but if you can find it, "Recursive Algorithms" by Richard Lorentz is about nothing but recursion.  It covers the basics of recursion, as well as specific recursive algorithms.

The examples are in Pascal, but are not so large that the choice of language is bothersome.


问:
Data aggregation like polyvore.com
I'm creating a website that is going to aggregate women shoes from across the web into one website.  Kinda like polyvore.com does with all their products but mine is only going to be shoes.  Which makes my job a lot easier but I'm still lost as to how I go about getting all the data.

I have access to 3 APIs so far. Which provide over 15000 line items so far but there are 1000's of sites out there and millions of products.

What is the best method to aggregate all this data?  Is it even possible?

Or should I rely on a social aspect as well, like polyvore where users can upload any products that aren't on the website?

Also, do I have to resort to screen scraping?

And is this possible as a one man show? Polyvore has a team of 30 programmers, do I need a team to do this properly?

答:
From a technical perspective, this is all possible to one degree or another. Your best option is periodic ingestion of another site's collection via APIs that they provide which return machine-readable data. Failing that, scraping could also work, though it is inherently brittle and might need to be tweaked often for every site you scrape from. Also, you are limited to pages that are discoverable through web crawls: anything hidden behind a search form will be inaccessible to you.

Once you have the raw data, you'll need to normalize it into the core items necessary to display a pair of shoes and allow people to shop for them. This is essentially a problem of mapping data in a variety of formats into yours. All the information is probably there, but it will likely be organized differently on each source. There's no rocket science here, but all this data mapping (not to mention building your own site) may make you wish you had a team of 30 programmers. :)

From a business perspective, I think you are on somewhat shaky ground. First of all, what you are doing may or may not be legal: you will likely either make copies of their text and images, which may be copyrighted, or you will be deep linking into their site, which is itself controversial. Second, if you homogenize the shopping experience, you suppress an individual store's unique branding and shopping features and put it on equal footing with its competitors, which it may not like (there goes its hard-won SEO and expensive engineering). To help with legality and safety, respect robots.txt files (if you scrape) and make it easy for stores to opt out (or for you to opt them out). That being said, Google Shopper has been doing this for years (though they recently switched to a model where stores pay to be included in search results--no more controversy).

I think your best bet is to establish business and technical relationships with each store you support. This will give you clean APIs to use and you will be perceived as a friend, not a nuisance (or worse).

Title: Data aggregation like polyvore.com


问:
Why doesn't C# have local scope in case blocks?
I was writing this code:

```<code>private static Expression<Func<Binding, bool>> ToExpression(BindingCriterion criterion)
{
    switch (criterion.ChangeAction)
    {
        case BindingType.Inherited:
            var action = (byte)ChangeAction.Inherit;
            return (x => x.Action == action);
        case BindingType.ExplicitValue:
            var action = (byte)ChangeAction.SetValue;
            return (x => x.Action == action);
        default:
            // TODO: Localize errors
            throw new InvalidOperationException("Invalid criterion.");
    }
}
</code>```

And was surprised to find a compile error:

A local variable named 'action' is already defined in this scope

It was a pretty easy issue to resolve; just getting rid of the second var did the trick.

Evidently variables declared in case blocks have the scope of the parent switch, but I'm curious as to why this is. Given that C# does not allow execution to fall through other cases (it requires break, return, throw, or goto case statements at the end of every case block), it seems quite odd that it would allow variable declarations inside one case to be used or conflict with variables in any other case. In other words variables appear to fall through case statements even though execution cannot. C# takes great pains to promote readability by prohibiting some constructs of other languages that are confusing or or easily abused. But this seems like it's just bound to cause confusion. Consider the following scenarios:

If were to change it to this:

```<code>case BindingType.Inherited:
    var action = (byte)ChangeAction.Inherit;
    return (x => x.Action == action);
case BindingType.ExplicitValue:
    return (x => x.Action == action);
</code>```

I get "Use of unassigned local variable 'action'". This is confusing because in every other construct in C# that I can think of var action = ... would initialize the variable, but here it simply declares it.

If I were to swap the cases like this:

```<code>case BindingType.ExplicitValue:
    action = (byte)ChangeAction.SetValue;
    return (x => x.Action == action);
case BindingType.Inherited:
    var action = (byte)ChangeAction.Inherit;
    return (x => x.Action == action);
</code>```

I get "Cannot use local variable 'action' before it is declared". So the order of the case blocks appears to be important here in a way that's not entirely obvious -- Normally I could write these in any order I wish, but because the var must appear in the first block where action is used, I have to tweak case blocks accordingly.

If were to change it to this:

```<code>case BindingType.Inherited:
    var action = (byte)ChangeAction.Inherit;
    return (x => x.Action == action);
case BindingType.ExplicitValue:
    action = (byte)ChangeAction.SetValue;
    goto case BindingType.Inherited;
</code>```

Then I get no error, but in a sense, it looks like the variable is being assigned a value before it's declared.
(Although I can't think of any time you'd actually want to do this -- I didn't even know goto case existed before today)

So my question is, why didn't the designers of C# give case blocks their own local scope? Are there any historical or technical reasons for this?

答:
A simplified way of looking at scope is to consider scope by blocks {}.

Since switch does not contain any blocks, it cannot have different scopes.


答:
I think a good reason is that in every other case, the scope of a “normal” local variable is a block delimited by braces ({}). The local variables that are not normal appear in a special construct before a statement (which is usually a block), like a for loop variable or a variable declared in using.

One more exception are local variables in LINQ query expressions, but those are completely different from normal local variable declarations, so I don't think there is a chance of confusion there.

For reference, the rules are in §3.7 Scopes of the C# spec:

The scope of a local variable declared in a local-variable-declaration is the block in which the declaration occurs.

The scope of a local variable declared in a switch-block of a switch statement is the switch-block.

The scope of a local variable declared in a for-initializer of a for statement is the for-initializer, the for-condition, the for-iterator, and the contained statement of the for statement.

The scope of a variable declared as part of a foreach-statement, using-statement, lock-statement or query-expression is determined by the expansion of the given construct.

(Though I'm not completely sure why is the switch block explicitly mentioned, since it doesn't have any special syntax for local variable declations, unlike all the other mentioned constructs.)

Title: Why doesn't C# have local scope in case blocks?

Body:
I was writing this code:

```<code>private static Expression<Func<Binding, bool>> ToExpression(BindingCriterion criterion)
{
    switch (criterion.ChangeAction)
    {
        case BindingType.Inherited:
            var action = (byte)ChangeAction.Inherit;
            return (x => x.Action == action);
        case BindingType.ExplicitValue:
            var action = (byte)ChangeAction.SetValue;
            return (x => x.Action == action);
        default:
            // TODO: Localize errors
            throw new InvalidOperationException("Invalid criterion.");
    }
}
</code>```

And was surprised to find a compile error:

A local variable named 'action' is already defined in this scope

It was a pretty easy issue to resolve; just getting rid of the second var did the trick.

Evidently variables declared in case blocks have the scope of the parent switch, but I'm curious as to why this is. Given that C# does not allow execution to fall through other cases (it requires break, return, throw, or goto case statements at the end of every case block), it seems quite odd that it would allow variable declarations inside one case to be used or conflict with variables in any other case. In other words variables appear to fall through case statements even though execution cannot. C# takes great pains to promote readability by prohibiting some constructs of other languages that are confusing or or easily abused. But this seems like it's just bound to cause confusion. Consider the following scenarios:

If were to change it to this:

```<code>case BindingType.Inherited:
    var action = (byte)ChangeAction.Inherit;
    return (x => x.Action == action);
case BindingType.ExplicitValue:
    return (x => x.Action == action);
</code>```

I get "Use of unassigned local variable 'action'". This is confusing because in every other construct in C# that I can think of var action = ... would initialize the variable, but here it simply declares it.

If I were to swap the cases like this:

```<code>case BindingType.ExplicitValue:
    action = (byte)ChangeAction.SetValue;
    return (x => x.Action == action);
case BindingType.Inherited:
    var action = (byte)ChangeAction.Inherit;
    return (x => x.Action == action);
</code>```

I get "Cannot use local variable 'action' before it is declared". So the order of the case blocks appears to be important here in a way that's not entirely obvious -- Normally I could write these in any order I wish, but because the var must appear in the first block where action is used, I have to tweak case blocks accordingly.

If were to change it to this:

```<code>case BindingType.Inherited:
    var action = (byte)ChangeAction.Inherit;
    return (x => x.Action == action);
case BindingType.ExplicitValue:
    action = (byte)ChangeAction.SetValue;
    goto case BindingType.Inherited;
</code>```

Then I get no error, but in a sense, it looks like the variable is being assigned a value before it's declared.
(Although I can't think of any time you'd actually want to do this -- I didn't even know goto case existed before today)

So my question is, why didn't the designers of C# give case blocks their own local scope? Are there any historical or technical reasons for this?


问:
Proper name for a project supports 2 different release
Is there a technical name for a software project where the current and prior stable releases are both maintained?</p>

答: The naming convention of such is usually determined by the source control system that you're using.  SVN, for example, uses the terms branches and tags to refer to its codebases, with the latter referring to previously closed versions.  My company refers to a branch of code for a previously released version as maintenance branch, although that may be individual of us.


问:
Thinking pragmatically about scalability
I was recently asked as an exercise to design a scalable graph.

My first intuition was how to seperate the graph and distribute it (sharding,consistent hashing..etc)

Turns out my thinking was on the wrong line, I know what scalability is, but it seems it is a bit hard to think about it pragmatically.

What is the pragmatic way of thinking about scalability. I know I need high-availability, replication, fault tolerance, but what are some of the common patterns/paradigms needed to  implement these key points?

答:
I think scalability is really about having the ability to add capacity by adding components (hardware, typically) without any individual component becoming ever more loaded as system demand increases. In other words, in a scalable system there is no bottleneck component that will ultimately limit performance and throughput. Instead, performance and throughput ideally remain constant or at least shrink more slowly than demand on the system grows.

You are right to keep concepts like fault-tolerance and high-availability (and their implementation via replication) in mind, but I see them as more concerned with reliability than scalability. While often implemented together, they are two different things.

In your graph example, what is it that needs to scale?

Is it access to the graph (large numbers of simultaneous users)?

Regarding access, is it write access that needs to scale, or just read access?

Is it the size of the graph itself (e.g. large numbers of nodes like Facebook's social graph)?

What about complexity? Does each node need to support arbitrarily large numbers of connections?

Does each node (and therefore the system as a whole) need to hold arbitrarily large amounts of data?

These are some pragmatic issues that need to be addressed when one talks about scalability. Like most design challenges, it's really all about breaking the problem down into the core issues that need to be addressed.

Lastly, I see techniques like asynchronous programming as contributing to efficiency, but not scalability. Unless you are doing something major like replacing a quadratic algorithm with a linear one, efficiencies alone will not help much with scalability (see Amdahl's Law). They will help with the cost of scalability (a very pragmatic issue), but not with the potential for it.


问:
Is there such a thing as truly random?
I saw a video about random numbers and how the programmer in that video was talking about computers generating pseudo random numbers and that they are not really random. I knew about this.

Then he showed the decay of a radioactive material to generate random numbers where he claimed to be truly random. Is there really such a thing? I mean the process of the radioactive material shooting electrons might seem random but is it? Isn't it just a mysterious black box to us simply because we don't know how it really works?

Or does randomness just depend on the current level of scientific knowledge?

If so, then how come quantum computers are often quoted to be capable of generating truly random numbers? Can they really do this?

答:
The short answer is no.  By a definition of the word random in this context, it means that, in terms of cause and effect, an effect must occur without any cause.  In a deterministic universe, this is impossible.

At least, until you get into quantum theory.

I am not capable of explaining quantum theory to you, but know that it is a lens by which we do not ordinarily look at our world through.  Anything we typically use the word "random" to talk about in normal speech is not truly random - it is simply unpredictable beyond our limited perceptions.  However, were our knowledge or perception greater, we could find the cause of the effect, as there is one.

This may be a good starting point for further research.

Title: Is there such a thing as truly random?


问:
Implicit optimization versus explicit optimization
To explain what I mean, let me start with an example.

Consider a deque that supports O(logn) concatenation with another deque  and O(n) addition of n elements at one end. This dequeimplements a general seq interface (or type-class, or what have you) that allows iterating over a collection.

An explicit optimization approach would be, having a concat method (or function) for deque  objects and a separate pushSeq method for seq objects. Each method would be documented with the appropriate complexity.

An implicit optimization approach would be to have a single concat method that accepts a seq. An internal dynamic type test checks whether the supplied argument is actually a deque, and if so, calls an implementation method for concating deques. This is documented in the API.

Obviously you could have both of these. The point of implicit optimization is that you don't give the user explicit control over optimization. It just "happens", unless the user deliberately looks for it.

Right now I'm writing a library and I'm facing a very similar choice. I very much like the idea of a compact interface where things just "work". An implicit approach also gives me a lot more freedom. For example, maybe I can perform ten dynamic type tests to optimize the concat operation for different collections. Having ten different methods wouldn't make sense.

What's your take on this? Which approach is better, and why?

答:
I don't optimize anything until I have identified a performance problem, measured and profiled the software and know where the problem is. I want your library to "just work". As it's a library, I expect it to be reasonably efficient most of the time for most use cases, but also do not expect it to be optimal for every case. If I find it is inefficient for a simple or typical use case, to kick off a performance concern, I will not be impressed - especially if it is trivial to fix - you, as the library vendor, should have (as you are doing now) thought of that. </p>

<p>Give me one method that works reasonably well most of the time, and if you feel the need, give some "advanced" API's that I can use when, and only if, needed. Make sure the advanced API's are clearly documented as such. e.g. "Use this API to optimize the speed/memory use/whatever...." </p>

Body:
I don't optimize anything until I have identified a performance problem, measured and profiled the software and know where the problem is. I want your library to "just work". As it's a library, I expect it to be reasonably efficient most of the time for most use cases, but also do not expect it to be optimal for every case. If I find it is inefficient for a simple or typical use case, to kick off a performance concern, I will not be impressed - especially if it is trivial to fix - you, as the library vendor, should have (as you are doing now) thought of that.

Give me one method that works reasonably well most of the time, and if you feel the need, give some "advanced" API's that I can use when, and only if, needed. Make sure the advanced API's are clearly documented as such. e.g. "Use this API to optimize the speed/memory use/whatever...."


答:
All our current theories of Quantum Mechanics suggest that those quantum properties that are random - are truly random.

Some practical applications are the time between decays of atoms in a sample of a radioisotope or thermal noise in a resistor. These are fundementally random.

However you have to be a little careful with actual implimentations of these hardware effects, it's very easy to have a quantization of some timing circuit, or bias in an ADC which reduces the actual randomness.

There are a number of questions on physics.so eg. https://physics.stackexchange.com/questions/317/why-cant-the-outcome-of-a-qm-measurement-be-calculated-a-priori

Title: Is there such a thing as truly random?

Body:
I saw a video about random numbers and how the programmer in that video was talking about computers generating pseudo random numbers and that they are not really random. I knew about this.

Then he showed the decay of a radioactive material to generate random numbers where he claimed to be truly random. Is there really such a thing? I mean the process of the radioactive material shooting electrons might seem random but is it? Isn't it just a mysterious black box to us simply because we don't know how it really works?

Or does randomness just depend on the current level of scientific knowledge?

If so, then how come quantum computers are often quoted to be capable of generating truly random numbers? Can they really do this?


答:
On the macroscopic (i.e. non-quantum) level, things that we think of as "random" are in fact "chaotic." This means that minute perturbations can have non-minute effects on the outcome. For example, you could in theory accurately predict the outcome of a fair coin toss or a fair die toss, but this would take a ridiculous amount of calculation and a slight shift in the air currents would throw all of your calculations off. But the fact remains that in theory you could predict these outcomes, and so the processes aren't truly random.

As Southpaw Hare brought up, quantum effects are truly random, at least by our current reckoning in physics. An electron does not exist in any one place at any one time, instead the electron exists in a probabilistic cloud - at the quantum level, particles exist as both particles and waves. The Heisenberg Uncertainty Principle says that attempts to narrow down the location of an electron are doomed to failure; when you measure the electron's location then you affect its velocity (so the more accurately you know the electron's current position, the less accurately you know its subsequent position). I believe that random number generators based on radioactive decay are tapping into quantum uncertainty and are therefore truly random, but it's been a decade since I took a physics course and so radioactive decay may instead be a chaotic process.

Title: Is there such a thing as truly random?


答:
There are random events.

Events such as the decay of radioactive material (hotbits) cannot be predicted.  It is random.  In the hotbits situation, the random bits are generated by comparing decay events.  If the time of the first two events is less than the time of the next two events it is a 0 - if the second pair is shorter than the first pair, it is a 1, and if they are the same, it is thrown out.

These bits are 'expensive' in that there is not a lot of bits out there.  Most people use this as a seed to a pseudo random number generator.  If the seed is random it becomes very difficult to identify the sequence (most people seed random from the time or the process id or the combination...)

There are random sequences (known sequences, but the order of the values is random).

This was largely examined in Can you use Pi as a crude random number generator.  The question if the number is normal plays a role (though not all normal numbers have apparently random next digits).

There are chaotic events, that while not random are extremely difficult to predict.

Lavarand uses a set of lava lamps to seed a random number generator, much the same as the radioactive decay does.  Look at patent 5,732,138 for more on this.

Title: Is there such a thing as truly random?


问:
Is it OK to have a method return different types based on a parameter?
I'm looking for a reference to clean coding styles that I can pass to a team member.

In particular, the rule that a method should not change its return type based on an input parameter. If you need different output, use a different method.

Example:

```<code>$invoice_items = getInvoiceItems();
$total         = getInvoiceItems( TRUE );
</code>```

To me, this is bad coding style (and I'm not even talking about a parameter whose meaning can't be determined from the calling code).

The above example should actually be:

```<code>$total = totalInvoiceItems();
</code>```

... where totalInvoiceItems() might call getInvoiceItems() to get the items it needs to total.

Where would I find a reference to this (and possibly other important coding style rules)?

答:
Functions should behave consistently.
There is a second function hidden in functions with a boolean parameter anyway.

If there is a branch inside the function with two mostly different behaviors, why not make it two function and maybe factor the common code into a third? Is there a shortage of function declarators?


答:
As Eric said, the boolean flag as a behavior selector is a tell-tale sign that something's wrong with the function's contract.

Reasons, why this is a problem

The function contract is unnecessarily complicated. It's easy to explain the contract of a function that simply returns all invoice items, just as well as it is easy to explain a function that takes a list of invoice items and returns the sum of their payment amounts. It's much more involved to explain the optional boolean parameter version.

The function name is not telling you what the function does anymore. getInvoiceItems sounds like it should give me some items, not a sum value. Analogously, splitting it into two functions gives you something like sumOfInvoiceItems($items), which is self-explanatory again.

The function implementation itself is unnecessarily complicated. The simpler a function, the easier it is to understand, explain, verify, ... More complex functions are better at attracting bugs, so simple is better.

References

The main reference about clean code is of course the book of the same name by Uncle Bob. You will find more detailled reasoning there for keeping your functions short and simple with clean contracts and to name them well.

Albeit it is usually applied to classes, instead of functions, the Single Responsibility Principle can also be applied to a function. But anyways, the SRP also stems from Uncle Bob, so I guess the real reference is just Uncle Bob himself.

Title: Is it OK to have a method return different types based on a parameter?

Body:
I'm looking for a reference to clean coding styles that I can pass to a team member.

In particular, the rule that a method should not change its return type based on an input parameter. If you need different output, use a different method.

Example:

```<code>$invoice_items = getInvoiceItems();
$total         = getInvoiceItems( TRUE );
</code>```

To me, this is bad coding style (and I'm not even talking about a parameter whose meaning can't be determined from the calling code).

The above example should actually be:

```<code>$total = totalInvoiceItems();
</code>```

... where totalInvoiceItems() might call getInvoiceItems() to get the items it needs to total.

Where would I find a reference to this (and possibly other important coding style rules)?


答:
But it does. You can create local scopes anywhere by wrapping lines with <code>{}</code>   </p>

```<code>switch (criterion.ChangeAction)
{
  case BindingType.Inherited:
    {
      var action = (byte)ChangeAction.Inherit;
      return (x => x.Action == action);
    }
  case BindingType.ExplicitValue:
    {
      var action = (byte)ChangeAction.SetValue;
      return (x => x.Action == action);
    }
  default:
    // TODO: Localize errors
    throw new InvalidOperationException("Invalid criterion.");
}
</code>```

Body:
But it does. You can create local scopes anywhere by wrapping lines with {}

switch (criterion.ChangeAction)
{
  case BindingType.Inherited:
    {
      var action = (byte)ChangeAction.Inherit;
      return (x => x.Action == action);
    }
  case BindingType.ExplicitValue:
    {
      var action = (byte)ChangeAction.SetValue;
      return (x => x.Action == action);
    }
  default:
    // TODO: Localize errors
    throw new InvalidOperationException("Invalid criterion.");
}


答:
As @Thomas has pointed out, designing good test cases needs experience. But there is good news for you: you can learn it, and there are a lot of books out there where systematic approaches are described. For example, a "classic" one is "The Art of Software Testing, Second Edition" by Glenford Myers.

Some examples for techniques described there

code coverage / branch coverage tests: create a set of test cases so that every line of code is executed, or every possible branch is executed (this is a white-box technique for a program where you have inside into the source code)

equivalence class: partition your space of input data into disjoint classes, and create at least one test case for each class

edge case analysis: in your example above that may mean to create a test case with an empty array, a one-element array, a two-element array, an array with just positive, just negative elements or just zeros etc.

So, the answer to your question is: there is not just "one" rule of thumb, there are many, and you should have a look into additional literature to learn them.


问:
Is a lambda expression something more than an anonymous inner class with a single method?
There is a new hype with the long awaited lambda expressions in Java 8; every 3 day another article appears with them about how cool they are.

As far as I have understood a lambda expression is nothing more than an anonymous inner class with a single method (at least at the byte-code level). Besides this it comes with another nice feature - type inference but I believe the equivalent of this can be achieved with generics on some level (of course not in such a neat way as with lambda expressions).

Knowing this, are lambda expressions going to bring something more than just a syntactic sugaring in Java? Can I create more powerful and flexible classes or other object-oriented constructs with lambda expressions that aren't possible to be built with current language features?

答:
tl;dr: while it's mostly syntactic sugar, that nicer syntax makes lots of things practical that used to end in endless, unreadable lines of braces and parentheses.

Well, it's actually the other way around as lambdas are much older than Java. Anonymous inner classes with a single method are (were) the closest Java came to lambdas. It's an approximation that was "good enough" for some time, but has a very nasty syntax.

On the surface, Java 8 lambdas seem to be not much more than syntactic sugar, but when you look below the surface, you see tons of interesting abstractions. For example the JVM spec treats a lambda quite differently from a "true" object, and while you can handle them as if they where objects, the JVM is not required to implement them as such.

But while all that technical trickery is interesting and relevant (since it allows future optimizations in the JVM!), the real benefit is "just" the syntactic sugar part.

What's easier to read:

```<code>myCollection.map(new Mapper<String,String>() {
  public String map(String input) {
    return new StringBuilder(input).reverse().toString();
  }
});
</code>```

or:

```<code>myCollection.map(element -> new StringBuilder(element).reverse().toString());
</code>```

or (using a method handle instead of a lambda):

```<code>myCollection.map(String::toUpperCase);
</code>```

The fact that you can finally express in a concise way which would previously be 5 lines of code (of which 3 are utterly boring) brings a real change of what is practical (but not of what is possible, granted).

Body:
tl;dr: while it's mostly syntactic sugar, that nicer syntax makes lots of things practical that used to end in endless, unreadable lines of braces and parentheses.

Well, it's actually the other way around as lambdas are much older than Java. Anonymous inner classes with a single method are (were) the closest Java came to lambdas. It's an approximation that was "good enough" for some time, but has a very nasty syntax.

On the surface, Java 8 lambdas seem to be not much more than syntactic sugar, but when you look below the surface, you see tons of interesting abstractions. For example the JVM spec treats a lambda quite differently from a "true" object, and while you can handle them as if they where objects, the JVM is not required to implement them as such.

But while all that technical trickery is interesting and relevant (since it allows future optimizations in the JVM!), the real benefit is "just" the syntactic sugar part.

What's easier to read:

myCollection.map(new Mapper<String,String>() {
  public String map(String input) {
    return new StringBuilder(input).reverse().toString();
  }
});

or:

myCollection.map(element -> new StringBuilder(element).reverse().toString());

or (using a method handle instead of a lambda):

myCollection.map(String::toUpperCase);

The fact that you can finally express in a concise way which would previously be 5 lines of code (of which 3 are utterly boring) brings a real change of what is practical (but not of what is possible, granted).


问:
How to 'convert' a Static Access style project to IoC/DI style?
Are there any best approaches for refactoring a programming project that has previously been written with the static reference anti-pattern (the majority of classes refer at some point to  a static bean, where all of it's variables are public.

Half-way through a class, for instance you'll see something like:

EvilClass.Variable = returnsABoolean().

Then in a different class, you'll see something like:

```<code>if(EvilClass.Variable == foo){
    doSomething();
}else{
    doSomethingDifferent();
}
</code>```

Up until recently, I've been working with this pattern as I don't want to mix the styles and get things confused between the styles. But I want to slowly but surely 'fix' this project.

I'm the only dev on this, and I don't have the time to just sit down and refactor it (otherwise, I'd push for a full rewrite!).

Are there any best practices to be aware of when working with/fixing this kind of project/problem?

答:
I would probably approach this with the following multi-step approach:

Phase 1:

If possible, isolate the static class.

In C# I would move the static class to its own assembly, or an isolated assembly, and make all its public members internal so that only a class in the same isolated assembly can use it.

Then, create a non-static class that exposes the static class.

Inject this non-static class into all other bits of code that required the static class.

Phase 2:

Make the non-static class a singleton (only temporarily) so that it throws an exception if instantiated more than once

Move all the static code into the non-static class.

Delete the static class

Phase 3:

Run your entire set of tests with this new setup (if you're lucky enough to have any)

Or, make a list of all code paths that use the new non-static class, and try to cover all of them with manual testing.

If you encounter singleton exceptions, then you're accidentally instantiating your non-static class more than once.  See why, and fix it

If you don't encounter any exceptions, you're safe.

Phase 4:

Refactor any code that uses your new non-static class so that it respects the Single Responsibility Principle.  This will be a lot of work, but should help prevent accidentally using the wrong instance of the non-static class won't happen.

Remove the Singleton pattern from the non-static class

Keep refactoring until all dependencies are injected everywhere :) (easier said!)