问:
Semantic coupling vs. large class
I have hardware I communicate with via TCP. This hardware accepts ~40 different commands/requests with about 20 different responses. I've created a HardwareProxy class which has a TcpClient to send and receive data. I didn't like the idea of having 40 different methods to send the commands/requests, so I started down the path of having a single SendCommand method which takes an ICommand and returns an IResponse, this results in 40 different SpecificCommand classes. The problem is this requires semantic coupling, i.e. the method that invokes SendCommand receives an IResponse which it has to downcast to SpecificResponse, I use a future map which I believe ensures the appropriate SpecificResponse, but I get the impression this code smells. Besides the semantic coupling, ICommand and IResponse are essentially empty abstract classes (Marker Interfaces) and this seems suspicious to me.

If I go with the 40 methods I don't think I have broken the single responisbility principle as the responsibility of the HardwareProxy class is to act as the hardware, which has all of these commands. This route is just ugly, plus I'd like to have Asynchronous versions, so there'd be about 80 methods.

Is it better to bite the bullet and have a large class, accept the coupling and MarkerInterfaces for a smaller soultuion, or am I missing a better way?

Thanks.

Possible Solution

Based on DXM's response this is what I came up with:

```<code>struct Proxy
{
   template<class C, class R>
   ICommand<C, R>* CreateCommand()
   {
      return new C(this);
   }

   void Send(std::string s)
   {
      //tcp send here for now print command name
      cout << s << endl;
   }
};

template<class Derived>
struct IResponse
{};

struct DerivedResponse1 : IResponse<DerivedResponse1>
{
   std::string GetValue()
   {
      return "DerivedResponse1";
   }
};

template<class Derived, class Response>
struct ICommand
{
protected:
   Proxy* pProxy;
public:
   Response* Send()
   {
      std::string msg = "Sent: " + static_cast<Derived*> (this)->GetName();
      pProxy->Send(msg);
      //wait for future here
      Response* pResponse = new Response; //replace with future value
      return pResponse;
   }
};

struct DerivedCommand1 : public ICommand<DerivedCommand1, DerivedResponse1>
{
   DerivedCommand1(Proxy* pProxy)
   {
      this->pProxy = pProxy;
   }

   std::string GetName()
   {
       return "DerivedCommand1";
   }
};

int main()
{
   Proxy proxy;

   ICommand<DerivedCommand1, DerivedResponse1>* pDerivedCommand =
           proxy.CreateCommand<DerivedCommand1, DerivedResponse1>();
   DerivedResponse1* pDerivedResponse = pDerivedCommand->Send();
   cout << "Received :" << pDerivedResponse->GetValue() << endl;

   return 0;
}
</code>```

How does that look? It seems like I may not even need the IResponse interface, we'll have to see when I implment it. I realize there is a lot of important stuff missing, like the futures, but I wanted to get the template stuff down first. What do you think of passing in the proxy to the command? Also what about having the send in the ICommand as opposed to writting it for every command? Am I violating any other OO principles? Thanks.

答:
I think your move from 40 methods in one class to 40 command classes was done in the right direction. I would continue moving even more in that direction...

Instead having one sending entry point in your main HardwareProxy class, have each one of your concrete classes implement send() which would return a concrete response object.  Under the hood the command classes can derive from the common command base class, which would contain a reference back to the HardwareProxy (or maybe TcpClient is enough).

Another good thing about c++ is that you can define a clean, type-safe public interface with the help of templates:

```<code>proxy = new HardwareProxy();
proxy->Connect(...);

SpecificCommand* command = proxy.CreateCommand<SpecificCommand>();
SpecificResponse* response = command->send(...);
</code>```

Update:
Your last idea isn't exactly what I had in mind, as you could probably tell if you compare my client example above with your main function

I don't think there's any value in defining generic ICommand class, especially since as soon as you make it a ICommand<> template, you lose polymorphism anyway.  Your client code should be able to work directly with your SpecificCommand/SpecificResponse classes.  Presumably each command/response would have different data going in and coming out, so no point of putting that part into a generic interface.

However, if you have 40 different unique commands/responses, they will have something in common, the way they package/serialize/deserialize the data in order to prepare it for the TCP comms.  So what I would do is have a BaseCommand/BaseResponse classes (still not templates) that would handle all common work and potentially be responsible of talking to the TCPClient object.  Base classes would also allow your proxy to treat all commands in a uniform way in case you need things like being able to track which commands is outstanding or being able to iterate through all of them and gracefully clean them up when TCP connection drops.

While I love c++ templates and do use them rather freely, one word of advice I would give you is avoid them unless you actually need them.  In your example, you are using a template parameter to do a pointer cast in order to access derived class' member function.  You could just as easily declare GetName() to be virtual.  The reason why I suggested CreateCommand<>() template function is only because the code would look cleaner because otherwise, it would have to return BaseCommand type and your client code would have to do yet another pointer cast.  But the actual underlying design that I'm thinking could easily be done with no templates.

Title: Semantic coupling vs. large class

Body:
I think your move from 40 methods in one class to 40 command classes was done in the right direction. I would continue moving even more in that direction...

Instead having one sending entry point in your main HardwareProxy class, have each one of your concrete classes implement send() which would return a concrete response object.  Under the hood the command classes can derive from the common command base class, which would contain a reference back to the HardwareProxy (or maybe TcpClient is enough).

Another good thing about c++ is that you can define a clean, type-safe public interface with the help of templates:

proxy = new HardwareProxy();
proxy->Connect(...);

SpecificCommand* command = proxy.CreateCommand<SpecificCommand>();
SpecificResponse* response = command->send(...);

Update:
Your last idea isn't exactly what I had in mind, as you could probably tell if you compare my client example above with your main function

I don't think there's any value in defining generic ICommand class, especially since as soon as you make it a ICommand<> template, you lose polymorphism anyway.  Your client code should be able to work directly with your SpecificCommand/SpecificResponse classes.  Presumably each command/response would have different data going in and coming out, so no point of putting that part into a generic interface.

However, if you have 40 different unique commands/responses, they will have something in common, the way they package/serialize/deserialize the data in order to prepare it for the TCP comms.  So what I would do is have a BaseCommand/BaseResponse classes (still not templates) that would handle all common work and potentially be responsible of talking to the TCPClient object.  Base classes would also allow your proxy to treat all commands in a uniform way in case you need things like being able to track which commands is outstanding or being able to iterate through all of them and gracefully clean them up when TCP connection drops.

While I love c++ templates and do use them rather freely, one word of advice I would give you is avoid them unless you actually need them.  In your example, you are using a template parameter to do a pointer cast in order to access derived class' member function.  You could just as easily declare GetName() to be virtual.  The reason why I suggested CreateCommand<>() template function is only because the code would look cleaner because otherwise, it would have to return BaseCommand type and your client code would have to do yet another pointer cast.  But the actual underlying design that I'm thinking could easily be done with no templates.


问:
Releasing a project under GPL v2 or later without the source code of libraries
I wrote a system in Java that I want to release under the terms of GPL v2 or later. I've used Apache Maven to deal with all the dependencies of the system, so I don't have the source code of any of the libraries used. I've already checked, all the libraries were released under GPL-compatible licenses (Apache v2, 3-clause BSD, MIT, LGPL v2 and v2.1).

Can I distribute my work under GPLv2 or later and provide the libraries on the same package as separate works, without having to apply GPL on them (as stated by GPLv2 sec 2), even if they are necessary to compile and run the system? If so, I know I am bound only by the original licenses of each one, and than I know what to do.

答:
For the most part, the GPL v2 and LPGL v2 and v2.1 require that, if you distribute GPL'ed or LGPL'ed code, you also make the source code available yourself.  With a few exceptions, pointing to an upstream repository is not sufficient.  See section 3 of the GPL v2 and section 6 of the LGPL v2.1.  (Version 3 of the GPL and LGPL ease this restriction and permit pointing people to third-party servers; see section 6 of the GPL v3.)

So #1 and #3 are okay; #2, not so much.

Update: A few additional notes:

In practice, if you simply point people to third-party servers, most people won't care.  Complaints have occurred, though; several years ago, the FSF contacted several Linux distros for not making source available.  Slashdot and Linux.com covered this.

If the libraries are distributed under "LGPL v2 / v2.1 or later," then I believe that distributing library binaries but not the source would be okay, as long as you point people to the source, since LGPL v3 allows that.

In my own experience, I've found it simplest to just make the source for everything available, rather than trying to track the details of when I do and don't have to distribute things.

In response to your edit:  Sections 5 and 6 of the LGPL specifically cover creating "a work that uses the Library."  Based on my understanding, it's fine to distribute your work as GPLv2 or later without applying the GPL to the libraries.  (Your source code itself is not a derivative of the libraries, so the LGPL doesn't directly apply to your source code, and section 6 allows the resulting binary to use LGPL'ed libraries as long as the license terms are compatible.)

Standard disclaimers apply, I am not a lawyer, etc.

ContentLicense: CC BY-SA 3.0


问:
Are there Any Concerns with Importing Document Files From a Competing Product?
I have a new product that serves the same purpose as my competitor's long-standing product. One thing I have considered doing is allowing my program to import document files created by their product in order to provide an easy way for users to migrate towards mine. Naturally, this would be done without the competitor's permission, as it goes against their interests.

I've seen this done before with office suite software (e.g. Open Office and Apple Pages can import MS Word documents), but I'm wondering if there are any concerns, legal or ethical, with me doing this.

I fully expect any answers will most likely fall under the "I am not a lawyer" clause, but it would be helpful to have a starting point for anything I would need to be aware of, or if I shouldn't need to worry.

答:
There are a number of possible factors to consider:

Does your competitor disallow reverse engineering of their technology?  Is the prohibition in their license agreement?  Do you have to agree to their license terms (or their website TOS) to get access to the materials you need to perform such reverse engineering?

Will the company go after you?  Is it worth the risk?  Maybe it is, maybe it isn't.

How will your customers and your competitor perceive this import?  Do you position yourself as the David against the competing Goliath that sucks and blows at the same time, or do you take a more charitable position as merely being a competing, and complementary, product?

How much does it go against your competitor's interest?  Does it give you an unfair competitive advantage, or does it merely give you a chance to compete equally?


问:
Is it viable to make a port from a C++ application to Java through LLVM
how viable is it to port a C++ application to Java bytecode using LLVM (I guess LLJVM)?

The thing is that we currently have a process written in C++ but a new client has made mandatory to been able to run the program in a multiplatform way, using the Java Virtual Machine with obviously no native code (no JNI). The idea is to be able to take the generated jar and copy then to different systems (Linux, Win, 32 bits - 64 bits) and it should just work.

Looking around looks like it is possible to compile C++ to LLVM IR code and then that code to java bytecode. There is no need of the generated code to be readable.

I have test a bit with similar things using emscripten, this takes C++ code and compile it to JavaScript. The result is valid JS but totally unreadable (looks like assambler).

Does anybody done a port of an application from C++ to Java bytecode using this tecnique?

What problems could we face?

Is a valid approach for production code?

To make more clear my point after some comments, maybe port is not well used, I do not expect readable source code as a result, just java bytecode, so it is not a 'port' which will be developed for anymore, just that the target plattform must be the java JVM not the native assamblear.

Note: I am aware that currently we have some non standard C++ and close source libraries, we are looking to removing this non standard code and all close source libraries and use Free Libre Open Source Software, so lets suppose all code is standard C++ code with all code available at compile time.

Note2: It is not an option to write portable C++ code and then compile it to the desired target platform, the compiled program must be multiplatform, thus the use of JVM .

Note3: Right now we are not looking into similar solutions applied to Python or other language base, but i would also like to heard about it. With this I mean that our target executable must be java bytecode but if there are options to compile C++ to valid python compiled code I would also like to hear about them.

答:
I really doubt this will work. You might be able to translate your code into Java byte code, but it will not magically translate library calls into equivalent calls to the Java runtime and libraries. There may not even be equivalent Java runtime calls! Even if you eliminate all proprietary libraries you're still left with the C++ standard library.

To make this concrete: your C++ program may contain a call to fprintf(). That function is implemented in the C standard library and it's perfectly legit for a C++ program to call it. The LLVM to LLJVM translator is probably not going to magically figure out the sequence of Java run time calls that will produce the equivalent result to fprintf() and substitute those in. To provide that facility would require essentially reimplementing the C and C++ runtimes in Java byte code.

There are some tools that perform C++ to Java translation but they only convert a handful of the simpler runtime library calls. The rest are left to you to figure out.


问:
Dependency injection: At what point am I allowed to create a new object?
I am refactoring a PHP application, and I am trying to do has much dependency injection (DI) as possible.

I feel like I've got a good grasp of how it works, and I can certainly see my classes becoming a lot leaner and more robust.

I'm refactoring so that I can inject a dependency rather than create a new object within the class, but at some point I am going to have to create some objects, that is, use the dreaded new keyword.

The problem I have now run into is at what point can I actually create new objects? It's looking like I'll end up at a top level class, creating loads of new objects as there is no where else to go. This feels wrong.

I've read some blogs that use factory classes to create all the objects, and then you inject the factory into other classes. You can then call the factory methods, and the factory creates the new object for you.

My concern with doing this is now my factory classes are going to be a new free-for-all! I guess this may be OK as they are factory classes, but are there some rules to stick to when using a factory pattern and DI, or am I going way off the mark here?

答:
A Factory class will have new sprinkled throughout it.  It would be creating whatever object that you are requesting and probably the dependencies of that object.  Since the factories job is to instantiate the correct object for you having new in this class isn't a bad thing.

DI is so that you have a loosely-coupled design.  You are able to make changes to your application by changing the dependency that is provided to each object.  Making your application flexible, extensible and easy to test.

At the top level, you will have code that will instantiate a couple of factories, setup connections to services, and send a response.  It will have a fair amount of new or static calls to instantiate objects that are necessary for your application.  That would be where that belongs.


答:
I did some further research into the matter and determined the following (although of course, I am not a lawyer).

Legal Concerns

The short answer is that it's perfectly legal. Reverse engineering software (and the files it creates) is ruled as fair use in the United States, as established by Sega v. Accolade. The case, and other legal matters relating to reverse engineering software, are described in great detail in this article from the Yale Law Journal (the part about software is on page 1608). Even if the output document were to be classified as a trade secret, reverse engineering trade secrets is also legal.

Wikipedia lists a number of projects, that legally use reverse engineering, specifically mentioning OpenOffice.org and others reverse engineering the Microsoft Office file formats. (Heck, Wikibooks even has the book Reverse Engineering/File Formats that gives you advice on how to do it). At the end of the day, the legal precedent would be that you are covered, although of course each legal situation is unique.

Ethical Concerns

As the Yale Law Journal article notes, not everybody is happy about reverse engineering software and some believe that it ought to be illegal. If the competitor sees it, they will probably be unhappy about the process since it might drive users from their product to yours and damage their sales. However, since that's the goal in the first place, that's probably not a concern.

It's possible that the company could see what you're doing as ethically wrong and give you all sorts of flak for converting their files. While we'd hope they would be professional about it, there's a small chance that you may have to deal with it. More likely though, they would implement the means for converting your documents if they thought you were a big enough concern. I wouldn't recommend converting their documents if you're not alright with them converting yours in return.

At the end of the day though, the choice is up to you and, like anything that involves other companies, there is an element of risk. While I believe that converting documents would be just fine and has a relatively low risk, each person will ultimately have to decide for themselves if they are alright with it.

Title: Are there Any Concerns with Importing Document Files From a Competing Product?

Body:
I have a new product that serves the same purpose as my competitor's long-standing product. One thing I have considered doing is allowing my program to import document files created by their product in order to provide an easy way for users to migrate towards mine. Naturally, this would be done without the competitor's permission, as it goes against their interests.

I've seen this done before with office suite software (e.g. Open Office and Apple Pages can import MS Word documents), but I'm wondering if there are any concerns, legal or ethical, with me doing this.

I fully expect any answers will most likely fall under the "I am not a lawyer" clause, but it would be helpful to have a starting point for anything I would need to be aware of, or if I shouldn't need to worry.


问:
A good way to learn a language and fully understand its features and classes is to try all methods in the API
I have an idea that to learn a language (i.e. Java)I need to try all methods from language's API; i want to try to use methods from its API.

is this a good idea? there are 10 000 methods in Java API. is this a good way to learn all language capabilities?

答:
Java has a very rich library that comes with the language.  It is rather impractical to attempt to learn all of the API (and then you start getting into the frameworks that aren't default for the language - Spring (ghads)).  You'll be reading methods and classes from now until the next release of Java (and then you'll be reading the change lists).

What is useful is to know what type of things are in the main packages and what they do.

The ones you will want to be familiar with are:

java.lang - these are the 'core' ones that you'll likely be encountering on a fairly frequent basis.

java.util - ones you'll often find yourself using.

java.util.Collection and its implementing classes - you'll find yourself using these time and time again.

Cases can certainly be made for additional packages of interest, though they start to get to specific domains of Java programming.  The swing and awt packages are for gui programming, while the servlet package is something that web programmers tend to deal with (who never even glance at awt or swing - and vice versa).

The key is not to know everything in the language, but to know how to find anything in the language.  The later is something that is an attainable goal and will continue to serve you well as the versions change.


问:
Why should I use MSBuild instead of Visual Studio Solution files?
We're using TeamCity for continuous integration and it's building our releases via the solution file (.sln). I've used Makefiles in the past for various systems but never msbuild (which I've heard is sorta like Makefiles + XML mashup). I've seen many posts on how to use msbuild directly instead of the solution files but I don't see a very clear answer on why to do it.

So, why should we bother migrating from solution files to an MSBuild 'makefile'? We do have a a couple of releases that differ by a #define (featurized builds) but for the most part everything works.

The bigger concern is that now we'd have to maintain two systems when adding projects/source code.

UPDATE:

Can folks shed light on the lifecycle and interplay of the following three components?

The Visual Studio .sln file

The many project level .csproj files (which I understand an "sub" msbuild scripts)

The custom msbuild script

Is it safe to say that the .sln and .csproj are consumed/maintained as usual from within the Visual Studio IDE GUI while the custom msbuild script is hand-written and usually consumes the already existing individual .csproj "as-is"? That's one way I can see reduce overlap/duplicate in maintenance...

Would appreciate some light on this from other folks' operational experience

答:
First point of fact is that the solution file pretty much magically becomes a MSBuild file when MSBuild executes it -- which is what happens when you build the solution in visual studio. In addition, all those project files are just msbuild files managed by visual studio. In fact, the project files handle most of the real dirty work here no matter if you are building from solutions or building from a custom msbuild script.

We also use TeamCity to build and publish applications and we will typically end up with custom msbuild scripts for most projects. The role these scripts play -- which isn't really easy with a solution file -- is packaging the project for deployment or distribution. Typically these scripts handle some more operational aspects of things -- like building the web project to a particular folder or copying in the running configurations as opposed to the development configurations. The heavy lifting tends to be handled in the project files themselves -- the build script just references those, we don't try and recreate that wheel. Overall it works great and it isn't really much duplicated code. Typically once we get the build scripts up we rarely have to touch them until there are massive changes in a project mandating massively changing the build.


答:
The makefile for msbuild is the .sln file (or vcproj file).

A typical msbuild command line would be something like:

```<code>msbuild /p:Configuration=Release BigProject.sln
</code>```

The point of using msbuild is so you can script and automate the build process. Whether you were aware of it or not, TeamCity has been using msbuild all along.

Title: Why should I use MSBuild instead of Visual Studio Solution files?

Body:
We're using TeamCity for continuous integration and it's building our releases via the solution file (.sln). I've used Makefiles in the past for various systems but never msbuild (which I've heard is sorta like Makefiles + XML mashup). I've seen many posts on how to use msbuild directly instead of the solution files but I don't see a very clear answer on why to do it.

So, why should we bother migrating from solution files to an MSBuild 'makefile'? We do have a a couple of releases that differ by a #define (featurized builds) but for the most part everything works.

The bigger concern is that now we'd have to maintain two systems when adding projects/source code.

UPDATE:

Can folks shed light on the lifecycle and interplay of the following three components?

The Visual Studio .sln file

The many project level .csproj files (which I understand an "sub" msbuild scripts)

The custom msbuild script

Is it safe to say that the .sln and .csproj are consumed/maintained as usual from within the Visual Studio IDE GUI while the custom msbuild script is hand-written and usually consumes the already existing individual .csproj "as-is"? That's one way I can see reduce overlap/duplicate in maintenance...

Would appreciate some light on this from other folks' operational experience

Body:
The makefile for msbuild is the .sln file (or vcproj file).

A typical msbuild command line would be something like:

msbuild /p:Configuration=Release BigProject.sln

The point of using msbuild is so you can script and automate the build process. Whether you were aware of it or not, TeamCity has been using msbuild all along.


问:
GPL Notice on Snippets
Is it ok to put a GPL notice inside a small script or a snippet?

This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

You should have received a copy of the GNU General Public License
  along with this program.  If not, see http://www.gnu.org/licenses/.

Or will just the Copyright notice be enough?

答:
Everything you need to know about that should be here:

http://www.gnu.org/licenses/gpl-howto.html

Out of curiosity, why would you make a snippet GPL?  Making a code snippet GPL pretty much makes it useless for anyone except those writing software that is also GPL.  Seems to me the LGPL (or even the MIT) license would make more sense.

Title: GPL Notice on Snippets

ContentLicense: CC BY-SA 2.5


答:
Personally, I am fine with the 40 functions. It doesn't sound as though you'll want som subset/interface of the proxy, and tcp commnication isn't a great candidate for unit testing.

Not everything needs to be OOP.

That said, I think it is foolish to try and tie that'll of the commands into a single interface. Let's face it - not all commands take the same parameters, and users will not want to iterate through arbitrary commands and execute them. They are going to request a specific command (likely by specific command interface) which implicitly contains its payload and response.

Title: Semantic coupling vs. large class

Body:
I have hardware I communicate with via TCP. This hardware accepts ~40 different commands/requests with about 20 different responses. I've created a HardwareProxy class which has a TcpClient to send and receive data. I didn't like the idea of having 40 different methods to send the commands/requests, so I started down the path of having a single SendCommand method which takes an ICommand and returns an IResponse, this results in 40 different SpecificCommand classes. The problem is this requires semantic coupling, i.e. the method that invokes SendCommand receives an IResponse which it has to downcast to SpecificResponse, I use a future map which I believe ensures the appropriate SpecificResponse, but I get the impression this code smells. Besides the semantic coupling, ICommand and IResponse are essentially empty abstract classes (Marker Interfaces) and this seems suspicious to me.

If I go with the 40 methods I don't think I have broken the single responisbility principle as the responsibility of the HardwareProxy class is to act as the hardware, which has all of these commands. This route is just ugly, plus I'd like to have Asynchronous versions, so there'd be about 80 methods.

Is it better to bite the bullet and have a large class, accept the coupling and MarkerInterfaces for a smaller soultuion, or am I missing a better way?

Thanks.

Possible Solution

Based on DXM's response this is what I came up with:

```<code>struct Proxy
{
   template<class C, class R>
   ICommand<C, R>* CreateCommand()
   {
      return new C(this);
   }

   void Send(std::string s)
   {
      //tcp send here for now print command name
      cout << s << endl;
   }
};

template<class Derived>
struct IResponse
{};

struct DerivedResponse1 : IResponse<DerivedResponse1>
{
   std::string GetValue()
   {
      return "DerivedResponse1";
   }
};

template<class Derived, class Response>
struct ICommand
{
protected:
   Proxy* pProxy;
public:
   Response* Send()
   {
      std::string msg = "Sent: " + static_cast<Derived*> (this)->GetName();
      pProxy->Send(msg);
      //wait for future here
      Response* pResponse = new Response; //replace with future value
      return pResponse;
   }
};

struct DerivedCommand1 : public ICommand<DerivedCommand1, DerivedResponse1>
{
   DerivedCommand1(Proxy* pProxy)
   {
      this->pProxy = pProxy;
   }

   std::string GetName()
   {
       return "DerivedCommand1";
   }
};

int main()
{
   Proxy proxy;

   ICommand<DerivedCommand1, DerivedResponse1>* pDerivedCommand =
           proxy.CreateCommand<DerivedCommand1, DerivedResponse1>();
   DerivedResponse1* pDerivedResponse = pDerivedCommand->Send();
   cout << "Received :" << pDerivedResponse->GetValue() << endl;

   return 0;
}
</code>```

How does that look? It seems like I may not even need the IResponse interface, we'll have to see when I implment it. I realize there is a lot of important stuff missing, like the futures, but I wanted to get the template stuff down first. What do you think of passing in the proxy to the command? Also what about having the send in the ICommand as opposed to writting it for every command? Am I violating any other OO principles? Thanks.


问:
"Whole-team" C++ features?
In C++, features like exceptions impact your whole program: you can either disable them in your whole program, or you need to deal with them throughout your code. As a famous article on C++ Report puts it:

Counter-intuitively, the hard part of coding exceptions is not the explicit throws and catches. The really hard part of using exceptions is to write all the intervening code in such a way that an arbitrary exception can propagate from its throw site to its handler, arriving safely and without damaging other parts of the program along the way.

Since even new throws exceptions, every function needs to provide basic exception safety — unless it only calls functions which guarantee throwing no exception — unless you disable exceptions altogether in your whole project.

Hence, exceptions are a "whole-program" or "whole-team" feature, since they must be understood by everybody in a team using them. But not all C++ features are like that, as far as I know.

A possible example is that if I don't get templates but I do not use them, I will still be able to write correct C++ — or will I not?. I can even call sort on an array of integers and enjoy its amazing speed advantage wrt. C's qsort (because no function pointer is called), without risking bugs — or not? It seems templates are not "whole-team".

Are there other C++ features which impact code not directly using them, and are hence "whole-team"? I am especially interested in features not present in C.

Update: I'm especially looking for features where there's no language-enforced sign you need to be aware of them. The first answer I got mentioned const-correctness, which is also whole-team, hence everybody needs to learn about it; however, AFAICS it will impact you only if you call a function which is marked const, and the compiler will prevent you from calling it on non-const objects, so you get something to google for. With exceptions, you don't even get that; moreover, they're always used as soon as you use new, hence exceptions are more "insidious". Since I can't phrase this as objectively, though, I will appreciate any whole-team feature.

Update 2: instead of C++ feature I should have written something like "C++-specific feature", to exclude things like multithreading which apply to a large amount of mainstream programming languages.

Appendix: Why this question is objective (if you wonder)

C++ is a complex language, so many projects or coding guides try to select "simple" C++ features, and many people try to include or exclude some ones according to mostly subjective criteria. Questions about that get rightfully closed regularly here on SO.

Above, instead, I defined (as precisely as possible) what a "whole-team" language feature is, provide an example (exceptions), together with extensive supporting evidence in the literature about C++, and ask for whole-team features in C++ beyond exceptions.

Whether you should use "whole-team" features, or whether that's a relevant concept, might be subjective — but that only means the importance of this question is subjective, like always.

答:
Pointers.

Does the pointer point to memory on the stack?

Does the pointer point to memory on the heap?

Does the pointer point to a single object?

Does the pointer point to an array?

Does the pointer point to a location in the middle of an array?

Is the pointer valid?

Is the pointer mangled?

What code "owns" the pointer?

Should the referenced object be manually deallocated? If so how?

OwnerDisplayName: Blaisorblade

Title: "Whole-team" C++ features?


问:
Adding a callback to a function?
Is there a way to add a callback to any given javascript function without changing its definition? Can this be done without using a library like deferred? </p>

答:
You can make any function have a callback in javascript using the following function:

```<code>//takes a function fn, and returns a function that is exactly the same as fn
//except that it also takes a callback as the last argument
//and calls that callback with the return value of fn
function makeCallbacked(fn){
  return function(){
    var callback = arguments[arguments.length-1];
    var args = Array.prototype.slice.call(arguments, 0, arguments.length-1);
    var result = fn.apply(this, args);
    callback(result);
    return result;
  }
}
</code>```

Sample in console:

```<code>> var test = makeCallbacked(function(a,b){ return a + b });
undefined
> test(1,2,function(result){ console.log("result: "+result); });
result: 3
3
</code>```

If you want the callbacks deferred, you can make the slight modification:

```<code>//takes a function fn, and returns a function that is exactly the same as fn
//except that it also takes a callback as the last argument
//and defers a call to that callback with the return value of fn
//note that it may take a very long time for the callback to be called
function makeCallbacked(fn){
  return function(){
    var callback = arguments[arguments.length-1];
    var args = Array.prototype.slice.call(arguments, 0, arguments.length-1);
    var result = fn.apply(this, args);

    setTimeout(function(){
      callback(result);
    }, 0);

    return result;
  }
}
</code>```

Sample in console:

```<code>> var test = makeCallbacked(function(a,b){ return a + b });
undefined
> test(1,2,function(result){ console.log("result: "+result); });
3
result: 3
</code>```

Note that this is basically a really stupid thing to write, as you usually want to pass more interesting information to a callback than a return value, and you want the function to probably have better control over when the callback gets called. Just changing the function is probably for the best. It's also a huge waste of resources, if you care about that kind of thing.

Title: Adding a callback to a function?

Body:
You can make any function have a callback in javascript using the following function:

//takes a function fn, and returns a function that is exactly the same as fn
//except that it also takes a callback as the last argument
//and calls that callback with the return value of fn
function makeCallbacked(fn){
  return function(){
    var callback = arguments[arguments.length-1];
    var args = Array.prototype.slice.call(arguments, 0, arguments.length-1);
    var result = fn.apply(this, args);
    callback(result);
    return result;
  }
}

Sample in console:

> var test = makeCallbacked(function(a,b){ return a + b });
undefined
> test(1,2,function(result){ console.log("result: "+result); });
result: 3
3

If you want the callbacks deferred, you can make the slight modification:

//takes a function fn, and returns a function that is exactly the same as fn
//except that it also takes a callback as the last argument
//and defers a call to that callback with the return value of fn
//note that it may take a very long time for the callback to be called
function makeCallbacked(fn){
  return function(){
    var callback = arguments[arguments.length-1];
    var args = Array.prototype.slice.call(arguments, 0, arguments.length-1);
    var result = fn.apply(this, args);

    setTimeout(function(){
      callback(result);
    }, 0);

    return result;
  }
}

Sample in console:

> var test = makeCallbacked(function(a,b){ return a + b });
undefined
> test(1,2,function(result){ console.log("result: "+result); });
3
result: 3

Note that this is basically a really stupid thing to write, as you usually want to pass more interesting information to a callback than a return value, and you want the function to probably have better control over when the callback gets called. Just changing the function is probably for the best. It's also a huge waste of resources, if you care about that kind of thing.


问:
Help with design structure choice: Using classes or library of functions
So I have GUI Class that will call another class called ImageProcessor that contains a bunch functions that will perform image processing algorithms like edgeDetection, gaussianblur, contourfinding, contour map generations, etc. The GUI passes an image to  ImageProcessor, which performs one of those algorithm on it and it returns the image back to the GUI to display. So essentially ImageProcessor is a library of independent image processing functions right now.

It is called in the GUI like so

```<code>Image image = ImageProcessor.EdgeDetection(oldImage);
</code>```

Some of the algorithms procedures require many functions, and some can be done in a single function or even one line.

All these functions for the algorithms jam packed into ImageProcessor can be pretty messy, and ImageProcessor doesn't sound it should be a library. So I was thinking about making every algorithm be a class with a shared interface say IAlgorithm. Then I pass the IAlgorithm interface from the GUI to the ImageProcessor.

```<code>public interface IAlgorithm{
   public Image Process();
}
public class ImageProcessor{

  public Image Process(IAlgorithm TheAlgorithm){
        return IAlgorithm.Process();
  }
}
</code>```

Calling in the GUI like so

Image image = ImageProcessor.Process(new EdgeDetection(oldImage));

I think it makes sense in an object point of view, but the problem is I'll end up with some classes that are just one function. What do you think is a better design, or are they both crap and you have a much better idea? Thanks!

答:
IMO the first scenerio is more readable and easy to understand

```<code>Image image = ImageProcessor.EdgeDetection(oldImage);
</code>```

a simple utility method call

while the second introduces complexity that does not seem to serve any purpose

Body:
IMO the first scenerio is more readable and easy to understand

Image image = ImageProcessor.EdgeDetection(oldImage);

a simple utility method call

while the second introduces complexity that does not seem to serve any purpose


问:
Should single purpose utility app use a class
When writing a small utility app, that does just one thing, should that one thing be encapsulated in a seperate class, or just let it be part of whatever class/module is used to start the application?  I.e. Main would consist of 2 or three lines calling the constructor and then the DoIt methods, nothing else.  Or should Main be the DoIt method, with whatever functions it needs added to the main class?

Asking because I want to get some alternative perspective, but couldn't find a similar question.  If my google-fu is bad and there's a dup, please close.

答:
Your end goal is to produce, clean, reliable code that is easy to read.  That means managing complexity. Classes help you do that by creating abstraction layers so that you can look at a simple public interface and not be concerned by all the nuts and bolts that make the class do it's thing.

However you also need to remember that "abstraction layers can solve just about any problem except for having too many abstraction layers" (not my quote but don't remember where I saw it).

So my advice would be to start small and follow some other good advice which is also not mine:

Make it work

Make it work right

Make it work fast (this one probably doesn't apply here)

If a main function is simple enough, stick with it.  If you notice it is starting to grow and morph, then take a chunk of functionality and make it a class (or 2, or 20)


问:
how to write good programming logic?
recently I got job as a java developer,
and now I have assigned project too.

I want to know what is a good logic?
when I check in the code my team lead is saying that
its a good code.

But when it comes to my project manager he is saying that its a bad code.
And he is changing my code, after his changes if I see his code
its really very very good and even simple.

can you please tell me how to develop the good program, good logic?
what is the best way to structure a problem in terms of code?

答:
There are some basic rules like high cohesion (the methods , classes and modules should do what they are supposed to do....nothing more and nothing less), low coupling, simplicity (yes there can be 'smarter' ways to accomplish more tasks with one unit but with time the complexity rises and maintainability suffers.)

Personally, I feel that one should always write a pseudo-code first. It might appear to be a waste of time but in the long run it actually saves time. Typing the code should be the last part.

Also, before writing the code, decide what all its supposed to do, the possible instances when it can break and how it should handle them.

Lastly, read best practices and blogs to keep yourself updated. Google blogs can be a good starting point.

Title: how to write good programming logic?


问:
How should I structure my database to gain maximum efficiently in this scenario?
I'm developing a PHP script that analyzes the web traffic of my clients websites. By placing a link to a javascript on the clients website (think of Google Analyses), my script harvests information like: the visitors IP address, reference link, current page link, user agent, etc. Now my clients can view these statistics via a control panel that I have build. These clients can also adjust profile settings, set firewall rules, create support tickets and pay invoices.

Currently all the the traffic is stored in one table. You can imagine that this tabel would become very large as some my clients receive thousands of pageviews per day. Furthermore, all the traffic data of each client would be stored in the same table, creating a mess. This is the same for the firewall rules currently, and the invoice and support system. I'm looking for way to structure my database in a more organized way to hold large amounts of data of multiple users.

This is the first project that I'm developing that deals with so much data, and would like to hear suggestions and tips. I was thinking of using multiple databases to structure the data. The main database will store users data (email,pass,id,etc) admin/website settings. Than each client will have an unique database labeled prefix_userid, which carry tables holding their traffic, invoice, and support ticket data.

Would this be a solution, and would it slow down or speed up overall performances (that is spreading the data over muliple databases). I have a solid VPS, but would like to safe and be as effient as possible.

答:
Sounds like you need a schema for each client.

i.e. you have the same set of tables you presently have but defined once per client.

Your client would only access the table belonging to their schema.

While this may make it easier to organize and track each client I doubt there would be much difference in performance. Writing 1GB to 20 tables vs. writing 20GB to 1 table is pretty much the same thing as far as the file system and hardware are concerned.


答:
What you're dealing with here aren't objects, they're functions.  They take arguments and return something of type Image, they don't hide data or encapsulate anything.  Some OO people will tell you that "everything is an object", but they're wrong.

The arguments aren't always going to be the same either; you could have a blur function that takes an image and a float that tells it how much blur to apply, or a fade function that takes two images and a float that tells it how much of image2 to blend into image1.  I don't think the idea of an interface works if the functions don't take the same arguments.

Treating them like functions is also more readable than the other two approaches:

```<code>Image image = ImageProcessor.Process(new EdgeDetection(oldImage));

Image image = ImageProcessor.EdgeDetection(oldImage);

Image image = edgeDetect(oldImage);
</code>```

If you're using Java, it may not let you have free-standing functions, but you can use your original approach with static import statements to avoid having to prefix everything with ImageProcessor. all the time.

Your ImageProcessor isn't really a class (you can't use it to make objects), it's a convenient container for some related functions.  You're being tempted to split it up into pieces because you're thinking of it as a single large thing, but it's already split into pieces: it's just that the pieces are functions, not objects or classes.

Title: Help with design structure choice: Using classes or library of functions

Body:
So I have GUI Class that will call another class called ImageProcessor that contains a bunch functions that will perform image processing algorithms like edgeDetection, gaussianblur, contourfinding, contour map generations, etc. The GUI passes an image to  ImageProcessor, which performs one of those algorithm on it and it returns the image back to the GUI to display. So essentially ImageProcessor is a library of independent image processing functions right now.

It is called in the GUI like so

```<code>Image image = ImageProcessor.EdgeDetection(oldImage);
</code>```

Some of the algorithms procedures require many functions, and some can be done in a single function or even one line.

All these functions for the algorithms jam packed into ImageProcessor can be pretty messy, and ImageProcessor doesn't sound it should be a library. So I was thinking about making every algorithm be a class with a shared interface say IAlgorithm. Then I pass the IAlgorithm interface from the GUI to the ImageProcessor.

```<code>public interface IAlgorithm{
   public Image Process();
}
public class ImageProcessor{

  public Image Process(IAlgorithm TheAlgorithm){
        return IAlgorithm.Process();
  }
}
</code>```

Calling in the GUI like so

Image image = ImageProcessor.Process(new EdgeDetection(oldImage));

I think it makes sense in an object point of view, but the problem is I'll end up with some classes that are just one function. What do you think is a better design, or are they both crap and you have a much better idea? Thanks!

Body:
What you're dealing with here aren't objects, they're functions.  They take arguments and return something of type Image, they don't hide data or encapsulate anything.  Some OO people will tell you that "everything is an object", but they're wrong.

The arguments aren't always going to be the same either; you could have a blur function that takes an image and a float that tells it how much blur to apply, or a fade function that takes two images and a float that tells it how much of image2 to blend into image1.  I don't think the idea of an interface works if the functions don't take the same arguments.

Treating them like functions is also more readable than the other two approaches:

Image image = ImageProcessor.Process(new EdgeDetection(oldImage));

Image image = ImageProcessor.EdgeDetection(oldImage);

Image image = edgeDetect(oldImage);

If you're using Java, it may not let you have free-standing functions, but you can use your original approach with static import statements to avoid having to prefix everything with ImageProcessor. all the time.

Your ImageProcessor isn't really a class (you can't use it to make objects), it's a convenient container for some related functions.  You're being tempted to split it up into pieces because you're thinking of it as a single large thing, but it's already split into pieces: it's just that the pieces are functions, not objects or classes.


问:
NDIS Driver Filter VS API Hooking
I've seen many developers asking for "How to intercept in/out HTTP packets ", "How to modify them on the fly". The most "clean" answer I've seen is to make a kernel-mode-driver filter from the scratch (TDI for XP and  earlier winx9 or NDIS for NT systems).

An other way, is to use a user-mode-driver like Windivert, also Komodia has a great solution (without writing any single code).

The idea behind this introduction is just I want to know is API Hooking can be considered as alternative of writing of whole of driver-filter? writing a driver from the scratch is not an easy task, why just not Hooking the HttpSendRequest or any other API used by the browser? There are many free/commercial libraries to do this in a safe manner (eg: EasyHook, Mhook, Nektra..).

I'm not the first who ask, there already Sockscap that uses Hook(DLL injection) to change behavior to other applications and force them to use a Socks proxy, also Form grabbing  attack 'used by keylogger..

答:
In most cases, using an API hook is good enough.  But for the socket calls, the wininet API hook is not enough to make a workable network filter.

I believe that API hook technique could be used to make a pass-thru wrapper.  But
it is difficult to give you a sample due to the complexity involved.  For example, you need to consider DLL injection, code injection, etc...

Title: NDIS Driver Filter VS API Hooking


问:
How does the maintenance of OpenJDK work in practice?
Specifically, I would like to understand the difference between Oracle's JDK and OpenJDK in terms of bug fixes and other patches.

Which branch gets fixes first and is Oracle in a habit of making patches that never make it to OpenJDK?

答:
This is explained in much details at OpenJDK page: JDK 7 Update Project Proposal Q & A

Will this Project serve as the basis for Oracle JDK 7 Update releases?

Yes.

To quote from Joe Darcy's FOSDEM blog post on OpenJDK 6:

In particular, there will not be the same dichotomy between the OpenJDK 7 code base and the 7 update code base as there is between OpenJDK 6 and the 6 update train...

Per my reading, above essentially means that patches and updates will typically go first to Open JDK and then, with as small delay as possible, delivered in Oracle JDK.

For security patches, the picture seems to be opposite, ie I'd rather expect them to go first to Oracle releases and then (again, with as small delay as possible) to OpenJDK:

Will the 7 Update Project receive security fixes from Oracle?

Yes.

As with OpenJDK 6, security fixes are first kept confidential and applied to a private forest before being pushed to the public forest as part of the general synchronized publication of the fix to affected JDK release trains. In addition, they will not go through the public code review and putback approval process, and their corresponding issues in the Project's issue tracker will not be publicly visible.

When will this Project receive security fixes from Oracle?

The schedule for Oracle Java SE Critical Patch Updates is publicly available.

Security fixes for this Project's source code will be made available in the JDK 7 Update Project around the same time as they're released in products from Oracle...

To better understand the reasons why there seem to be so much effort put into keeping Oracle and Open JDKs in sync, it makes sense to take a look at the Oracle decision on preceding project: Moving to OpenJDK as the official Java SE 7 Reference Implementation:

...Oracle and the other members of the Java SE 7 Expert Group have been putting the finishing touches to the Java SE 7 specification (JSR 336). In its role as the specification lead, Oracle is responsible for delivering the Java SE 7 Reference Implementation... we are going to provide a Reference Implementation that is based entirely on the OpenJDK open source code and make it available under the GPL open source license.

The role of the Reference Implementation (RI) is to be used as the gold standard for all Java implementations. In order to have an implementation certified as Java SE compatible, an implementor must pass a large number of compatibility tests - the Technology Compatibility Kit (TCK). Furthermore, implementations may be compared to the RI as an additional check of compatibility. Basically, if your implementation has been certified to have the same behavior as the RI then it is Java compatible. For more information on this topic, consult the JCP FAQ.

Historically, Sun always used the Sun JDK as the RI and made it available under the Binary Code License (BCL). This was very convenient for Sun since it meant that its product implementation was compatible by definition. However, it was also confusing since the Sun JDK contained quite a few features that were not part of the standard, such as the Java Plugin. Also, continuing this practice would make things difficult for open source implementors as they would not be able to study and evaluate the official RI source code. (The source code for the Oracle JDK is slightly different from OpenJDK - something we will be addressing moving forward).

With that in mind, Oracle will:

Create RI binaries based only on the OpenJDK code base.

Make RI binaries available under the BCL (the normal Java license) for commercial implementors and GPLv2 (with the Classpath exception) for open-source implementors.

Continue to provide the TCK to commercial licensees, but also update the OCTLA license so that it covers Java SE 7. The latter allows open source implementators gratis access to the TCK to verify their implementations...

Above decision means a lot of effort to put into Open JDK code, to release officially verified, tested, licensed and compliant code. If you add that it has to be released following the agreed public schedule, it becomes obvious that such an effort will be about the same as previously was put into "traditional" Sun/Oracle Java releases.

This makes it only reasonable to keep Open and Oracle JDK code bases as close as possible: otherwise, duplication of development and fixes to make both projects compliant with TCK could become prohibitively daunting.

It looks like decision to use Open JDK as a Reference Implementation made it in the best interest of Oracle to keep their JDK as close as possible in sync with Open JDK - up to release of JDK 7.

To understand what could motivate Oracle to keep mentioned sync further, with JDK 7 update releases, one would better take a look at the Open JDK 8 project, which purpose is described pretty much similar to that of Open JDK 7:

The goal of this Project is to produce an open-source reference implementation of the Java SE 8 Platform, to be defined by JSR 337 in the Java Community Process.

For the same reasoning as was explained above regarding reference implementation of JDK 7, it is, again, in the best interest of Oracle to keep the updates of both JDKs as much in sync as possible.

The more differences would be between these JDKs now, the harder would it be for Oracle to release Java SE 8, due to duplication of efforts necessary to bring their own release into compliance with TCK. The opposite is also true, ie the closer both projects are going to be now, the less effort will be required to release both Java 8 implementations.

Did it ever happened to you to support in parallel two slightly different versions of the same software, targeted at different clients? If yes, you likely remember the desire to keep them both as close as possible, and the inconveniences you experienced when these were out of sync. With Open and Oracle JDKs, it is pretty much like that, only on a larger scale.


答:
Almost all bug fixes go directly through the OpenJDK project and then into downstream JVM vendors (Oracle, Azul, RedHat etc).

An exception is that some security patches are fixed in downstream versions (in particular Oracle) before quietly being ported it back into OpenJDK. This allows
the vendors to upgrade most of the world with the security fix before publicising the vulnerability in the open source project.

Some vendors also choose not to push their changes back into OpenJDK. For example both Google and Twitter have modified OpenJDK versions that they use internally  with bug fixes and features that have not gone back into the main OpenJDK project.

HTH

Title: How does the maintenance of OpenJDK work in practice?

Body:
Specifically, I would like to understand the difference between Oracle's JDK and OpenJDK in terms of bug fixes and other patches.

Which branch gets fixes first and is Oracle in a habit of making patches that never make it to OpenJDK?


问:
How to draw a class diagram where a class contains either 6 other classes or mupltie different
I know the basics but do not how to model this (it is an exercise with no solution provided):

An order can contain 1 or more cartons of beer. Each carton contains 6
  beers of the same kind but there are some consisting of 3 different
  beer types (still 6 in total)

Also having a "Carton" class, how can I make it the way it either 6 beers (same ones) OR 6 beers of 3 various types?

答:
You can use the xor contstraint to model exclusive or. If you really know the basics, you should have seen it already.

The rest is straightforward. You have an order class which has one or more cartons (1..*). You could model this association with an aggregation but it's not exactly specified. However, it's not a composition, as cartons can definately exist on their own.

There are multiple solutions for how to model the carton. The first that comes to my mind is to split the cartons into two types. One for single beer type, one for multiple beer types. Both derive from an abstract class using generalization. The order is associated only with the abstract carton class.

The thing with beer is, that you could also split it up into an abstract class and its derived beer types, but this is not really needed here. So you could just create three different beer classes. The first carton (single beer type) is associated with all three beer classes with the multiplicity 6 and constrained with xor constraints between the three associations. The other carton (three beer types) is also associated with all three beer types. However, you don't need the xor constraint and the multiplicity is 2 for each association.

There are definately more elegant ways to model this, but this one should at least be considered correct.


答:
Depending on your needs (specifically wanting to filter packets and block sites) the most clean option is definitely not to attempt to roll your own security tools.  Instead use a tool specifically made for the job that allows you to put a network level job entirely out of reach of the client.  Installation, configuration and maintenance are much simpler than coding your own filters and having to deploy them and manage them.

Squid has a range of ways to do what you want to do (including rewriting http headers and even html content.)  If necessary it can be installed 'transparently' on a network to ensure that nobody can get around it even if they are aware it is present.  It can even work with SSL if needed.

It also has some other positives which include lists of known proxy servers which are regularly updated to stop people using those to get around your filters.

Title: NDIS Driver Filter VS API Hooking

Body:
I've seen many developers asking for "How to intercept in/out HTTP packets ", "How to modify them on the fly". The most "clean" answer I've seen is to make a kernel-mode-driver filter from the scratch (TDI for XP and  earlier winx9 or NDIS for NT systems).

An other way, is to use a user-mode-driver like Windivert, also Komodia has a great solution (without writing any single code).

The idea behind this introduction is just I want to know is API Hooking can be considered as alternative of writing of whole of driver-filter? writing a driver from the scratch is not an easy task, why just not Hooking the HttpSendRequest or any other API used by the browser? There are many free/commercial libraries to do this in a safe manner (eg: EasyHook, Mhook, Nektra..).

I'm not the first who ask, there already Sockscap that uses Hook(DLL injection) to change behavior to other applications and force them to use a Socks proxy, also Form grabbing  attack 'used by keylogger..


问:
what would be the best data structure for my situation
I have a list of unique string. I don't want to destroy this string i need it.I only need to find out weather an given string S exist in this unique list.What would be the best data structure for this purpose.

答: A HashSet of strings would give you good lookup performance but is only suitable if the strings are guaranteed to be unique.</p>


答:
You could use a Set.

A Set is a data structure that can not contain duplicate objects, so you can just dump all your strings into it and it automatically filters out duplicates.

To explicitely check if a given string is already in the set you could call a contains() method.

See also: http://en.wikipedia.org/wiki/Set_%28computer_science%29 and the corresponding Java interface http://docs.oracle.com/javase/6/docs/api/java/util/Set.html

Title: what would be the best data structure for my situation

Body: I have a list of unique string. I don't want to destroy this string i need it.I only need to find out weather an given string S exist in this unique list.What would be the best data structure for this purpose.


答:
You need to do more accurate calculation for an amount of the purposes. For end users, your utility has single purpose, right, but for it's code this isn't so.

Let's see, there's the part you denoted DoIt - it has that single purpose of doing utility function, fine, but besides it there's a part you called Main and it has own, separate purpose - it serves as program entry point.

You see, code serves at least two purposes, not one (second one isn't visible to end users but maintainers of your code will see it very well). And, if you happen to also have command line arguments passed to utility, there will also be a code with third purpose of parsing these, go figure.

All right, there is more than one purpose, and serving them in one class deviates from a single responsibility principle (SRP) - from this perspective you have full right to split it, you only need to decide whether deviation is justified or not.

Say, if your code to handle command line arguments is only 5-10 lines, benefits of keeping it nearby most likely outweight blind adherence to theoretical principles. Or maybe your DoIt code is so clean, elegant and compact that having even a small amount of unrelated parsing code there makes you feel "dirty"? if so, don't hold your pain, split it out and feel better - the very purpose of SRP is to justify moves like that.

Similar reasoning applies when you decide whether to keep the entry point in or split it out. If all your code is in single file, it's only reasonable to assume that anyone reading it will figure that entry point is also there. If you have more than that, well, it makes sense to give it a little more thought.

I for one feel quite frustrated when entering an unfamiliar codebase I have to waste time trying to figure whether to start reading code from MyVeryImportantClass or from MyAbsolutelyNecessaryClass. In cases like that, I would much appreciate if there was a simple, separate (however small) MainClass file I could start with without thinking.

Title: Should single purpose utility app use a class

Body:
When writing a small utility app, that does just one thing, should that one thing be encapsulated in a seperate class, or just let it be part of whatever class/module is used to start the application?  I.e. Main would consist of 2 or three lines calling the constructor and then the DoIt methods, nothing else.  Or should Main be the DoIt method, with whatever functions it needs added to the main class?

Asking because I want to get some alternative perspective, but couldn't find a similar question.  If my google-fu is bad and there's a dup, please close.


问:
Reverse subarray of an array with O(1)
I have an idea how to implement sub array reverse with O(1), not including precalculation such as reading the input. I will have many reverse operations, and I can't use the trivial solution of O(N).

Edit: To be more clear I want to build data structure behind the array with access layer that knows about reversing requests and inverts the indexing logic as necessary when someone wants to iterate over the array.

Edit 2: The data structure will only be used for iterations

I been reading this and this and even this questions but they aren't helping.

There are 3 cases that need to be taking care of:

Regular reverse operation

Reverse that including reversed area

Intersection between reverse and part of other reversed area in the array

Here is my implementation for the first two parts, I will need your help with the last one.

This is the rule class:

```<code>class Rule {
    public int startingIndex;
    public int weight;
}
</code>```

It is used in my basic data structure City:

```<code>public class City {
    Rule rule;
    private static AtomicInteger _counter = new AtomicInteger(-1);
    public final int id = _counter.incrementAndGet();

    @Override
    public String toString() {
        return "" + id;
    }
}
</code>```

This is the main class:

```<code>public class CitiesList implements Iterable<City>, Iterator<City> {

    private int position;
    private int direction = 1;
    private ArrayList<City> cities;
    private ArrayDeque<City> citiesQeque = new ArrayDeque<>();
    private LinkedList<Rule> rulesQeque = new LinkedList<>();

    public void init(ArrayList<City> cities) {
        this.cities = cities;
    }

    public void swap(int index1, int index2){
        Rule rule = new Rule();
        rule.weight = Math.abs(index2 - index1);
        cities.get(index1).rule = rule;
        cities.get(index2 + 1).rule = rule;
    }

    @Override
    public void remove() {
        throw new IllegalStateException("Not implemented");
    }

    @Override
    public City next() {
        City city = cities.get(position);
        if (citiesQeque.peek() == city){
            citiesQeque.pop();
            changeDirection();
            position += (city.rule.weight + 1) * direction;
            city = cities.get(position);
        }
        if(city.rule != null){
            if(city.rule != rulesQeque.peekLast()){
                rulesQeque.add(city.rule);
                position += city.rule.weight * direction;
                changeDirection();
                citiesQeque.push(city);
            }
            else{
                rulesQeque.removeLast();
                position += direction;
            }
        }
        else{
            position += direction;
        }
        return city;
    }

    private void changeDirection() {
        direction *= -1;
    }

    @Override
    public boolean hasNext() {
        return position < cities.size();
    }

    @Override
    public Iterator<City> iterator() {
        position = 0;
        return this;
    }

}
</code>```

And here is a sample program:

```<code>public static void main(String[] args) {
        ArrayList<City> list = new ArrayList<>();
        for(int i = 0 ; i < 20; i++){
            list.add(new City());
        }
        CitiesList citiesList = new CitiesList();
        citiesList.init(list);

        for (City city : citiesList) {
            System.out.print(city + " ");
        }
        System.out.println("\n******************");
        citiesList.swap(4, 8);

        for (City city : citiesList) {
            System.out.print(city + " ");
        }
        System.out.println("\n******************");
        citiesList.swap(2, 15);

        for (City city : citiesList) {
            System.out.print(city + " ");
        }
    }
</code>```

How do I handle reverse intersections?

答:
Using ratchet freak idea with xor linked list, I came to my own Java implementation with O(1) time complexity for sub array rotation not including pre calculation(such as reading the input).

I call this data structure Directed linked list.

```<code>public class Node {
    Node next = null;
    Node previous = null;

    Node getNext(Node node){
        if(node == next){
            return previous;
        }

        if(node == previous){
            return next;
        }

        throw new IllegalStateException("How did you got here?");
    }

    void setNext(Node node, Node next){
        if(node == previous){
            previous = next;
        }
        else if(node == this.next){
            this.next = next;
        }
        else{
            throw new IllegalStateException("How did you got here?");
        }
    }
}
</code>```

DirectedLinkedList class

```<code>public class DirectedLinkedList implements Iterable<Node>, Iterator<Node> {

    private Node head = new Node();
    private Node tail = head;
    private Node current;
    private Node previuse;

    @Override
    public Iterator<Node> iterator() {
        prepareForIteration();
        return this;
    }

    public void addToHead(Node node) {
        Node next = head.getNext(null);
        Node previuse = head.getNext(next);
        head.setNext(previuse, node);
        node.setNext(null, head);
        head = node;
    }

    public void swap(Node a, Node i, Node c, Node j){
        System.out.println("Swaping " + i + " , " + j);
        Node jNext;

        jNext = j.getNext(c);
        jNext.setNext(j, i);

        j.setNext(jNext, a);
        i.setNext(a, jNext);

        a.setNext(i, j);

        System.out.println();

    }

    private void prepareForIteration() {
        current = head;
        previuse = null;
    }

    @Override
    public boolean hasNext() {
        return current != tail;
    }

    @Override
    public Node next() {
        Node tmp = previuse;
        previuse = current;
        current = current.getNext(tmp);
        return previuse;
    }

    @Override
    public void remove() {
        // TODO Auto-generated method stub

    }
}
</code>```

And here is a sample program:
I use City for Node

```<code>public class City extends Node{
    private static AtomicInteger _counter = new AtomicInteger(-1);
    public final int id = _counter.incrementAndGet();

    @Override
    public String toString() {
        return "" + id;
    }
}
</code>```

Main

```<code>public class Sample{

    private static DirectedLinkedList directedLinkedList;

    public static void main(String[] args) {
        directedLinkedList = new DirectedLinkedList();
        ArrayList<City> list = new ArrayList<City>();

        for (int i = 0; i < 20; i++) {
            City city = new City();
            directedLinkedList.addToHead(city);
            list.add(city);
        }

        for (Node city : directedLinkedList) {
            System.out.print(city + " ");
        }
        System.out.println("\n******************");
        swap(4, 8);

        for (Node city : directedLinkedList) {
            System.out.print(city + " ");
        }
        System.out.println("\n******************");
        swap(6, 12);

        for (Node city : directedLinkedList) {
            System.out.print(city + " ");
        }
    }

    private static void swap(int i, int j) {
        Node a = null, b = null, c = null, d = null;

        int count = 0;
        for (Node city : directedLinkedList) {
            if (count == i) {
                a = city;
            }
            else if (count == i + 1) {
                b = city;
            }
            else if (count == j) {
                c = city;
            }
            else if (count == j + 1) {
                d = city;
            }
            count++;
        }

        directedLinkedList.swap(a, b, c, d);
    }
}
</code>```

Title: Reverse subarray of an array with O(1)

Body:
Using ratchet freak idea with xor linked list, I came to my own Java implementation with O(1) time complexity for sub array rotation not including pre calculation(such as reading the input).

I call this data structure Directed linked list.

public class Node {
    Node next = null;
    Node previous = null;

    Node getNext(Node node){
        if(node == next){
            return previous;
        }

        if(node == previous){
            return next;
        }

        throw new IllegalStateException("How did you got here?");
    }

    void setNext(Node node, Node next){
        if(node == previous){
            previous = next;
        }
        else if(node == this.next){
            this.next = next;
        }
        else{
            throw new IllegalStateException("How did you got here?");
        }
    }
}

DirectedLinkedList class

public class DirectedLinkedList implements Iterable<Node>, Iterator<Node> {

    private Node head = new Node();
    private Node tail = head;
    private Node current;
    private Node previuse;

    @Override
    public Iterator<Node> iterator() {
        prepareForIteration();
        return this;
    }

    public void addToHead(Node node) {
        Node next = head.getNext(null);
        Node previuse = head.getNext(next);
        head.setNext(previuse, node);
        node.setNext(null, head);
        head = node;
    }

    public void swap(Node a, Node i, Node c, Node j){
        System.out.println("Swaping " + i + " , " + j);
        Node jNext;

        jNext = j.getNext(c);
        jNext.setNext(j, i);

        j.setNext(jNext, a);
        i.setNext(a, jNext);

        a.setNext(i, j);

        System.out.println();

    }

    private void prepareForIteration() {
        current = head;
        previuse = null;
    }

    @Override
    public boolean hasNext() {
        return current != tail;
    }

    @Override
    public Node next() {
        Node tmp = previuse;
        previuse = current;
        current = current.getNext(tmp);
        return previuse;
    }

    @Override
    public void remove() {
        // TODO Auto-generated method stub

    }
}

And here is a sample program:
I use City for Node

public class City extends Node{
    private static AtomicInteger _counter = new AtomicInteger(-1);
    public final int id = _counter.incrementAndGet();

    @Override
    public String toString() {
        return "" + id;
    }
}

Main

public class Sample{

    private static DirectedLinkedList directedLinkedList;

    public static void main(String[] args) {
        directedLinkedList = new DirectedLinkedList();
        ArrayList<City> list = new ArrayList<City>();

        for (int i = 0; i < 20; i++) {
            City city = new City();
            directedLinkedList.addToHead(city);
            list.add(city);
        }

        for (Node city : directedLinkedList) {
            System.out.print(city + " ");
        }
        System.out.println("\n******************");
        swap(4, 8);

        for (Node city : directedLinkedList) {
            System.out.print(city + " ");
        }
        System.out.println("\n******************");
        swap(6, 12);

        for (Node city : directedLinkedList) {
            System.out.print(city + " ");
        }
    }

    private static void swap(int i, int j) {
        Node a = null, b = null, c = null, d = null;

        int count = 0;
        for (Node city : directedLinkedList) {
            if (count == i) {
                a = city;
            }
            else if (count == i + 1) {
                b = city;
            }
            else if (count == j) {
                c = city;
            }
            else if (count == j + 1) {
                d = city;
            }
            count++;
        }

        directedLinkedList.swap(a, b, c, d);
    }
}


答:
I think you should separate some things since you are talking about different applications (stats/firewalls/transactions) which you just by accident show in the same interface. It is not needed to implement back-end in the same way.

Likely the parts: invoices, firewall etc. will not have a huge load as I estimate based on your question.

Statistics surely do, and they need to be processed fast initially because they can slow down websites.

Now because you can separate stats out of the normal database you can think of the stats as a separate thing. Most statistics don't need a relational database (though it can work perfectly fine). You don't need transactions, locking etc. So select a storage method which fits, could be a flat mysql, mongodb, etc.

Also rethink: Why would you do this yourself, couldn't you use for example Google Analytics or Piwik and use their API to show the same results to the customer?

Building reports from it is something you can do later so don't try to do smart things when logging the view. Just keep that part simple. Then you will have the best chance of a fast response.

So don't see everything as the same, see each application separately and work up to the best solution for that issue.

About you idea of sharding (database per client) see:

http://highscalability.com/blog/2009/8/6/an-unorthodox-approach-to-database-design-the-coming-of-the.html

It's a lot of work to get it right so be sure you need it.

Title: How should I structure my database to gain maximum efficiently in this scenario?

Body:
I'm developing a PHP script that analyzes the web traffic of my clients websites. By placing a link to a javascript on the clients website (think of Google Analyses), my script harvests information like: the visitors IP address, reference link, current page link, user agent, etc. Now my clients can view these statistics via a control panel that I have build. These clients can also adjust profile settings, set firewall rules, create support tickets and pay invoices.

Currently all the the traffic is stored in one table. You can imagine that this tabel would become very large as some my clients receive thousands of pageviews per day. Furthermore, all the traffic data of each client would be stored in the same table, creating a mess. This is the same for the firewall rules currently, and the invoice and support system. I'm looking for way to structure my database in a more organized way to hold large amounts of data of multiple users.

This is the first project that I'm developing that deals with so much data, and would like to hear suggestions and tips. I was thinking of using multiple databases to structure the data. The main database will store users data (email,pass,id,etc) admin/website settings. Than each client will have an unique database labeled prefix_userid, which carry tables holding their traffic, invoice, and support ticket data.

Would this be a solution, and would it slow down or speed up overall performances (that is spreading the data over muliple databases). I have a solid VPS, but would like to safe and be as effient as possible.


答:
Here's a rule of thumb when it comes to deciding between object-oriented and procedural programming:

Use object-oriented programming (OOP) when the overall complexity of a system exceeds the complexity of its individual algorithm.

If the control flow of your application is highly complex, switch to OOP. Else, stay with procedural programming.


问:
UML class diagram - can aggregated object be part of two aggregated classes?
Some sources say that aggregation means that the class owns the object and shares reference. Lets assume an example where a company class holds a list of cars but departments of that company has list of cars used by them.

```<code>class Department
{
   list<Car> listOfCars;
}

class Company
{
   list<Car> listOfCars;
   //initialization of the list
}
</code>```

So in UML class diagram, I would do it like this. But I assume this is not allowed because it would imply that both company and department own the objects..

```<code>[COMPANY]<>------[CAR]
[DEPARTMENT]<>---|        //imagine this goes up to the car class
</code>```

答:
Mind the difference between aggregation and composition!

While in an aggregation instances of both associated classes may exist without a relation (e.g. a restaurant and a chair), for a composition the one can not exist without the other (e.g. a child without a parent).

In your case an aggregation seems more appropriate to me, and than it is no problem to model a aggregation between Department and Car AND Company and Car.

Besides that: such a situation is often modeled like Company <*>-- Department <>-- Car (where only Department stores the list of cars).


问:
Naming a release
OS X 10.9 not just called 10.9 but also Mavericks.

iOS7 is just called iOS7.

Android releases are named after sweets.

What is the rationale of giving a name to a release version?

What are the benefits if any?

Most apps simply increment the number as they push new releases.

Is naming a release (Mavericks, Kit kat etc...) just for marketing purpose?

答:
Naming a release makes it easier to remember and reference, in particular for the lay-person.

Instead of a string of numbers, you just have a name to use.

Of course - this is mostly useful for marketing - but when people talk about a release, it makes it easier to know exactly which one is being referenced.

Title: Naming a release


答:
Once you've worked on various bits of code for years, calling releases by their unfriendly version number becomes problematic, so internal projects are given friendly names. Sometimes its relevant to what you're trying to accomplish in the project, but the privilege tends to go with the lead developer, so it can just be whatever is on their mind at the time.

Long story short, it's for fun and convenience.

Title: Naming a release

Body:
OS X 10.9 not just called 10.9 but also Mavericks.

iOS7 is just called iOS7.

Android releases are named after sweets.

What is the rationale of giving a name to a release version?

What are the benefits if any?

Most apps simply increment the number as they push new releases.

Is naming a release (Mavericks, Kit kat etc...) just for marketing purpose?


答: Since you are lucky enought to have someone who can change your code to be better, learn from him. Look for patterns in what he changes and what he does not. Try to use the same methods he used when doing your next piece of code. Ask him for advice on how to learn to be better. When he changes things, ask him why he changed them and what situations his improvements are appropriate in and when they would not be.  Ask him if instead of changing the code, he would sit with you and guide you into changing the code, so that you learn what to do. </p>

Title: how to write good programming logic?

Body:
recently I got job as a java developer,
and now I have assigned project too.

I want to know what is a good logic?
when I check in the code my team lead is saying that
its a good code.

But when it comes to my project manager he is saying that its a bad code.
And he is changing my code, after his changes if I see his code
its really very very good and even simple.

can you please tell me how to develop the good program, good logic?
what is the best way to structure a problem in terms of code?


答:
Naming a release allows to do the development (using that name for in things like VCS branches) without waiting for the marketing to decide how they should be numbered and without having to explain to newcomers that the branch V107 is used for the release 2.

Then the name leaks, marketing want to control it, and changes it 2 days before the release.

Title: Naming a release


答:
It protects you from Marketing, to some degree. How many times has marketing decided that something was going to be called "Foo 2.0" when the 2.0 designation makes no sense from a technical perspective?  If you treat build and revision numbers as separate from the marketing name (1.8.14 -> "Foo 2.0"), and assign an easy-to-remember name to the milestone version that is going to get released under marketing's label, you can discuss both concepts without concern for how often "Foo 2.0" might get renamed or if an extra minor revision is included.  Consider:

<s>1.8.14</s> -> 1.8.21 -> Cornwall -> <s>"Foo 2.0"</s> -> NuFoo).

You can talk about Cornwall for weeks while you fix minor issues that crop up and push your minor version numbers up, and while the marketroids argue over what the most market-forward name is.

Title: Naming a release


问:
How do I improve my coding skills?
Here's a bit information about me, before starting with the question.

I am a Computer Science Undergraduate, Java being my primary coding language.

The basic problem in my University are the teaching standards. No one is concerned about teaching coding knowledge to students, rather than just theoretical knowledge.

The effect being, most of my fellow college mates don't understand programming at all.

Even I haven't been able to come out of the traditional programming environment, which limits my coding to an extent.

What are the possible ways by which I can develop and expand my programming/coding skills.

Also, can you suggest the sources for the same?

Edited:
Sources suggesting development of coding skills.

答:
The general consensus is "write more code", which I agree with, but I'll add to that advice that you should write a lot of different kinds of code. Java is fine as far as languages go, but you should definitely write code both up and down the power spectrum. In addition to Java, I'd suggest doing a few small projects in a lower level language (C is a good one for this), a scripting language (I prefer perl, but python is also a good choice), a functional language (lisp is the general recommendation here, but OCaml and Haskell are also good choices), and at least get comfortable with reading an assembly language.

Also, pick some variety in the types of programs you implement. Look at implementing at least a Desktop GUI, a rich web application, a network client and server, a driver, a data munging utility, a ray tracer, and a physics simulation.

All of the projects should be relatively small in scope, the goal isn't to have a featurefull program, but to get a wide breadth of experience in the different domains you might run into, to learn the different types of thinking needed for each, and to find out where your interests are.

Once you've done that, you'll have a good idea of what type of programming you're the most interested in, and you can find or start an open source project to get experience working on large code bases, dealing with long-term projects, and working with other developers.

Title: How do I improve my coding skills?


问:
Where Are Multiple JUnit Test Methods Typically Placed in Code?
I've just read the Vogella JUnit tutorial and found it very helpful in understanding how to use JUnit. However, I'm a bit confused about what the convention is for placing multiple test methods in code. The tutorial only places one test method in a class, then describes how you can use a test suite to group multiple test classes together.

Does this mean that it's common practice for each test class to only have one test method and then test suites are used to chain them together? Or was that just unintended and instead common practice is to put multiple test methods in a class?

答:
It is absolutely common practice to have multiple test methods in a class.

JUnit methods are usually invoked via reflection by collecting all methods in a class (or, as you say, in an entire suite), not by a human application programmer choosing methods from a well-defined API. Therefore, there is no point in keeping each individual test class short or readable; people aren't supposed to understand a class that holds unit tests as a cohesive unit in the same the way that a class of production code should be cohesive. Having one class per test just puts unnecessary strain on your file system (dealing with thousands of files in a directory is often non-linearly expensive).


答:
@KilianFoth is right regarding putting multiple test methods in one test class. However, I think in some cases it can be important to run single test classes or even single test methods. That's what IDEs are for, to simplify this approach. For instance, if a test invokes a computationally intensive routine, that takes a few minutes to finish, you might want to skip this most of the time.

Structuring your tests

Every class in your source folder gets an according unit test class in the test folder.

Let the following directory tree be your source folder:

```<code>src/
  |- persistance/
  |   |- DatabaseConnection.java
  |
  |- entities/
      |- Apple.java
      |- Orange.java
</code>```

Then the folder for unit tests should have the same structure (like when applying the factory method pattern):

```<code>test/
  |- persistance/
  |   |- DatabaseConnectionTest.java
  |
  |- entities/
      |- AppleTest.java
      |- OrangeTest.java

</code>```

For every method of a class in src you write at least two tests in the according unit test class - one positive and one negative test. These tests can be quite simple as testing the behaviour of a method that accepts one parameter of type integer and a string is passed. In this case you could tell the test to expect a certain exception, e.g., IllegalArgumentException:

```<code>@Test(expected=IllegalArgumentException.class)
public void testIfExceptionIsThrown { ... }
</code>```

You can also test more complex procedures (e.g., test the output of a black box algorithm against a log file).

Title: Where Are Multiple JUnit Test Methods Typically Placed in Code?

Body:
I've just read the Vogella JUnit tutorial and found it very helpful in understanding how to use JUnit. However, I'm a bit confused about what the convention is for placing multiple test methods in code. The tutorial only places one test method in a class, then describes how you can use a test suite to group multiple test classes together.

Does this mean that it's common practice for each test class to only have one test method and then test suites are used to chain them together? Or was that just unintended and instead common practice is to put multiple test methods in a class?

Body:
@KilianFoth is right regarding putting multiple test methods in one test class. However, I think in some cases it can be important to run single test classes or even single test methods. That's what IDEs are for, to simplify this approach. For instance, if a test invokes a computationally intensive routine, that takes a few minutes to finish, you might want to skip this most of the time.

Structuring your tests

Every class in your source folder gets an according unit test class in the test folder.

Let the following directory tree be your source folder:

src/
  |- persistance/
  |   |- DatabaseConnection.java
  |
  |- entities/
      |- Apple.java
      |- Orange.java

Then the folder for unit tests should have the same structure (like when applying the factory method pattern):

test/
  |- persistance/
  |   |- DatabaseConnectionTest.java
  |
  |- entities/
      |- AppleTest.java
      |- OrangeTest.java

For every method of a class in src you write at least two tests in the according unit test class - one positive and one negative test. These tests can be quite simple as testing the behaviour of a method that accepts one parameter of type integer and a string is passed. In this case you could tell the test to expect a certain exception, e.g., IllegalArgumentException:

@Test(expected=IllegalArgumentException.class)
public void testIfExceptionIsThrown { ... }

You can also test more complex procedures (e.g., test the output of a black box algorithm against a log file).


答:
Both. I found myself repeatedly starting with a simple, procedural thing which ended up as a fully-fledged OOP solution consisting of several classes and interfaces. In my opinion, this is just how things are evolving in the real world and there's nothing bad about it. It happens due to enhanced requirements, due to better insight in the problem domain, or other reasons.

The most crucial point during this process is to develop an inner sense, some kind of mental detector, or maybe a certain rule (like "if it goes beyond 2000 lines, refactor it into classes") which rings the alarm bells right at the point, where the code starts becoming too complex for procedural but still is easy enough to refactor into classes.

Once you got to that point, the rest is easy.


问:
How to test issues in a local development environment that can only be introduced by clustering in production?
We recently clustered an application, and it came to light that because of how we're doing SSL offloading via the load balancer in production it didn't work right. I had to mimic this functionality on my local machine by SSL offloading Apache with a proxy, but it still isn't a 1-to-1 comparison. Similar issues can arise when dealing with stateful applications and sticky sessions. What would be the industry standard for testing this kind of production "black box" scenario in a local environment, especially as it relates to clustering?</p>

答:
It can be expensive and painful, but in the end you need to have a local "cluster".  Trying to simulate race conditions, contention and the like are very hard on a single PC (my interpretation of "local development environment").

From past experience I would suggest:

Push very hard to get a production level cluster into your test/dev environment, You can scale back some to keep costs down, but the profile needs to be representative of your production environment.

Get periodic (weekly?) refreshes of your production DB into your dev/test environment

Instrument your code for high amounts of logging in this environment

Implement remote debugging on this cluster as best you can given your particular language and deployment stack

I have used this strategy in the past and was able to identify the underlying bug.  Then I was able to develop locally and unit test locally before deploying the fix to the test cluster.