问:
How to parse different number types with LALR(1)
Consider a LALR(1) parser for a file format that allows integer numbers and floating point numbers.

As usual, something like 42 shall be a valid integer and a valid float (with some automagic conversion in the background).

There might be parsing rules where a floating point number or an integer number is expected, and other rules where only an integer number is expected, e.g.:

```<code>foo1
    : bar FLOAT buzz
    | bar INT buzz
    ;

foo2
    :  some INT other stuff
    ;
</code>```

Now consider something like

```<code>foo3
    : bar FLOAT xyz FLOAT abc FLOAT buzz
    ;
</code>```

but at each position in this rule, instead of FLOAT, also INT shall be allowed.

Turning this rule into 8 rules (one rule for each combination of FLOAT and INT) isn’t an option. (Consider a rule having 4 or 5 numbers...)

Using a rule like

```<code>float_or_int : FLOAT | INT;
</code>```

won’t help, because in general, this rule will reduce all INT to float_or_int, and rules like foo2 no longer can be parsed. (Because with a grammar large enough, the one token lookahead cannot avoid the shift-reduce-conflicts resulting from this rule.)

When the lexer sees a number without a decimal point, it cannot decide whether the parser currently expects an int or a float-or-int.

How can this be handled in an elegant way?

答:
What is typically done is that the numeric constant is "parsed" in the lexer, with "number type" (int, float, base, ...) information made available to the parser.  You use the simple int_or_float rules in the grammar, and then the associated semantic actions are responsible for verifying that you have legal number types in each place, and declaring an error if you don't.

The parser will apparently succeed in parsing the file, but you will still have flagged the errors, and you can refuse to generate the result based on those errors.


问:
Distribute load evenly across many cycles
I am looking for a solution that is fast and still readable. I have to do this in C but the code will be static in the end, which mean I could use any tool to generate it.

I have a task which runs cyclically and calls different functions also cyclically

Main functions cycle is 1.

Function a has also cycle 1. So the main function calls it every time

Function b has cycle 2. Main function calls it every second cycle.

Function c has cycle 2.

Function d has cycle 4.

Function e has cycle 4.

Function f has cycle 4.

Function g has cycle 4.

... many more ....

Function X has cycle 500.

If we have many functions that run in the same cycle we try to distribute them.

So for function b we would call it in one cycle and c in the other. Distributing the load evenly.

Similar for d to g. Each one gets called in a different cycle.

My current implementation is very bad.

Every developer who had to add their task to main function calculates their on modulo and calls when needed. This leads to a very unreadable code.

I was thinking of doing something like:

```<code>uint32 mod4 = timer % 4;
bool fourth_1 = mod4 == 0;
bool fourth_2 = mod4 == 1;
bool fourth_3 = mod4 == 2;
bool fourth_4 = mod4 == 3;
bool second_1 = fourth_1 || fourth_3;
bool second_2 = fourth_2 || fourth_4;
/* and so on*/
</code>```

But this seems cumbersome and too error prone specially as the highest cycle time is too high.

How to approach this kind of problem?

-Edit-

As asked i have compiled some information about the number of functions that i have:

32 functions cycle 1

9 functions cycle 2

14 functions cycle 4

3 functions cycle 8

3 functions cycle 32

3 functions cycle 40

2 functions cycle 200

2 functions cycle 400

答:
The starting point for solving this problem is a data structure. The elements of the structure are nodes (structs) containing:

how to call one of the functions: a function pointer, any arguments, any other information

when to call a function: the 'cycle' as you put it.

You can use a flat list created at compile time, but then the entire list will have to be searched at least once for each cycle, and that may not be good for performance. A better approach is to add each node to a data structure designed to minimise searching. I would suggest a list for each cycle modulo, so 8 lists for the data given.

At initialisation time add each function node to the list(s) on which it should be called.

On each cycle simply go down the list(s) that are applicable, calling each function in turn.

Simple, testable, maintainable, extensible.

ContentLicense: CC BY-SA 3.0


问:
Sense of unit tests without TDD
We have new (quite big) project starting, that we planned to develop using TDD.

The idea of TDD failed (many business and non-business reasons), but right now we have a conversation - should we write unit tests anyway, or not.
My friend says that there is no (or close to zero) sense in writing unit tests without TDD, we should focus only on integration tests. I believe the opposite, that there is still some sense in writing plain unit tests, just to make code more futureproof. What do you think?

Added:
I think that this is not a duplicate of >>this question<< - I understand difference between UT and TDD. My question is not about differences, but about sense of writing Unit Tests without TDD.

答:
TDD is used mainly (1) to ensure coverage, (2) and to drive maintainable, understandable, testable design. If you don't use TDD, you don't get guaranteed code coverage. But that in no way means that you should abandon that goal and blithely live on with 0% coverage.

Regression tests were invented for a reason. The reason is that in the long run, they save you more time in prevented errors than they take in additional effort to write. This has been proven over and over again. Therefore, unless you are seriously convinced that your organization is much, much better at software engineering than all the gurus who recommend regression testing (or if you plan on going down very soon so that there is no long run for you), yes, you should absolutely have unit tests, for exactly the reason that applies to virtually every other organization in the world: because they catch errors earlier than integration tests do, and that will save you money. Not writing them is like passing up free money just lying around in the street.

Title: Sense of unit tests without TDD


问:
what licence for an application that uses libraries with different licences
I have developed .NET software that uses two libraries:

Library A has MIT licence and I'm using my own version of it as I edited the source code of this library.

Library B has Ms-PL licence and I use only the binaries.

My application will be distributed freely in binaries and source code(including the source code of library A).

Since it is my first time to distribute a public application I want to know few things:

what Licence I should choose.

a part from the to-be-chosen licence Eula presented to the user in the installation , what else I should do for the binaries ?

for the source code, what should I include exactly ?

答:
Barts answer is perfectly correct. I just want to add my own rider.

Please read the licence and make your own decision. If you don't understand it, then perhaps you should pay a lawyer to read it for you.

The basic breakdown is this. If you make no money out of it and harm no-one then just do your best, acknowledge the work of others and don't worry too much.

If you make money out of your product or services and/or if you may cause damage to someone or compete with someone who does, then you must take competent legal advice. That won't protect you, but it will warn you about the risks and possibly help to mitigate them. You are at risk, even if you don't realise it, and the more money you make the bigger the risk.


问:
Proper sequence of actions for merging using SVN?
I'm totally new to CVCS (SVN specifically). I find that most resources focus very strongly on the command-line commands (irrelevant; we use Windows Explorer + TortoiseSVN) and about structure (e.g. projects inside our outside TTB, etc). Here's a thing about merging I'm not sure of:

Based on my sandbox testing, I got the understanding that merging should be done sort-of backwards? I first thought that merging meant going from the branch into the trunk, but it now seems to me that I should actually merge the trunk into my branch, fix conflicts there, and only then commit my branch into trunk?

Let's say I'm responsible for merging a branch into the mainline, and everybody has committed their local changes into that branch. I now switch my working copy to the branch and merge the trunk into my working copy in order to resolve any conflicts locally and tidy things up. When complete, I commit my working copy into the trunk (there should be none or hardly any conflicts in this step).

This is basically also what one needs to do when committing changes from one's working copy into the branch.

Right? Wrong? Am I missing some steps? It seems complicated but makes sense.

Let's not argue about how merging in SVN is harder than in DVCS's because that's not something I can change.

Also, I'm not asking about the specific command-line actions because we're using TortoiseSVN.

答:
Yes, that is a best practice for how SVN is designed.  You have your branch, you merge the trunk into your branch, because there are always issues to deal with (it is easier if you periodically do this while working on your branch), and when it's good you merge it back into the trunk. At this point there shouldn't be any conflicts unless someone else has come and checked in updates in between.

It does seem complicated but after doing it a few times it becomes easier.


问:
What metadada to save and whether to include it inside the binary file?
I'm wondering if there are any general guidelines or best practices regarding when to split data into a metadata format, as oppose to directly embedding it within the data. (Specific example below).

My understanding of metadata is that it describes data (without the need to actually look at the data), allowing for data to be quickly search/filtered for easy access.

Let's take for example a simple 3D model format. The actual data file is itself a binary file containing vertices and colors. Things like creation date, modified data and author name would be things that describe the binary data, so I would say these belong as metadata (outside of the binary file).

But the following questions arise:

What if the application had no need to search or filter by these fields?

Would it be acceptable to embed these fields directly into the binary data itself?

Could they be duplicated in both the binary data and the meta data, or would this be considered bad practice?

What about more ambiguous fields such as the model name, which could be considered part of the data itself, but also as data describing the binary data?

How do you decide which data to embed in the actual binary file, as opposed to separating into a more flexible metadata format?

Thanks!

答:
I think saving the metadata inside the binary file and providing a specification so anyone can program an API has its advantages.

Many binary types include the metadata inside the file itself, providing a public specification or API of how to access it. Examples could be the ID tags of the mp3 formats, the metadata of PDF files, the EXIF data of images, etc.

That asures the metadada travels with the file wherever it goes

Aplications have no problem reading that metadata to populate a database, or even updating the metadata in the file itself, like iTunes or Rhythmbox do on audio files.


答:
Before asking about merging, you should ask whether you need branch-based development.

The answer depends solely on your project and team. I personally believe in branches for application-level "feature" development, but am comfortable with changing trunk/master for utility projects and new development (bugfixes to released projects require branches, but that's a separate topic, and is motivated by different concerns).

If you're going with a branch-based development style, then it becomes imperative that the branches do not diverge too far from the trunk. That leads to merge pain (even with a DVCS), but more important, it means that your feature is developed without consideration for what's happening elsewhere.

The solution is simple: regular merges of the trunk into all outstanding branches. As I describe it, "changes get pushed upstream (from branch to trunk) but flow back downstream (from trunk to branches)." If you have multiple people on the team working on different features, I believe that every developer should merge the trunk into his/her feature branch whenever the trunk is updated (ie, whenever another feature is complete).

Title: Proper sequence of actions for merging using SVN?

Body:
I'm totally new to CVCS (SVN specifically). I find that most resources focus very strongly on the command-line commands (irrelevant; we use Windows Explorer + TortoiseSVN) and about structure (e.g. projects inside our outside TTB, etc). Here's a thing about merging I'm not sure of:

Based on my sandbox testing, I got the understanding that merging should be done sort-of backwards? I first thought that merging meant going from the branch into the trunk, but it now seems to me that I should actually merge the trunk into my branch, fix conflicts there, and only then commit my branch into trunk?

Let's say I'm responsible for merging a branch into the mainline, and everybody has committed their local changes into that branch. I now switch my working copy to the branch and merge the trunk into my working copy in order to resolve any conflicts locally and tidy things up. When complete, I commit my working copy into the trunk (there should be none or hardly any conflicts in this step).

This is basically also what one needs to do when committing changes from one's working copy into the branch.

Right? Wrong? Am I missing some steps? It seems complicated but makes sense.

Let's not argue about how merging in SVN is harder than in DVCS's because that's not something I can change.

Also, I'm not asking about the specific command-line actions because we're using TortoiseSVN.


答:
This might be because I cut my teeth on SVN but we handle big DCVS merges the same way. Moreover most open source projects I see encourage you to follow a similar pattern with pull requests -- don't send one until you've made your patches work with the latest and greatest.

So I'm not sure if this is limited to SVN but yeah, the practice you describe in your question isn't merging backwards and it really is a better approach when there are multiple hands in the repo.


问:
Debugging memory corruption
First off, I do realize this is not a perfect Q&A style question with an absolute answer, but I can't think of any wording to make it work better. I don't think there is an absolute solution to this and this is one of the reasons why I'm posting it here instead of Stack Overflow.

Over the last month I have been rewriting a fairly old piece of server code (mmorpg) to be more modern and easier to extend/mod.
I started with the network portion and implemented a 3rd party library (libevent) to handle stuff for me.
With all the re-factoring and code changes I introduced memory corruption somewhere and I have been struggling to find out where it happens.

I can't seem to reliably reproduce it on my dev/test environment, even when implementing primitive bots to simulate some load I don't get crashes any more (I fixed a libevent issue which caused some stuff)

I have tried so far:

Valgrinding the hell out of it - No invalid writes until the thing crashes (which may take 1+ day in production.. or just an hour) which is really baffling me, surely at some point it would access invalid memory and not overwrite stuff by chance? (Is there a way to "spread out" the address range?)

Code-Analysis tools, namely coverity and cppcheck. While they did point out some.. nastiness and edge cases in the code there was nothing serious.

Recording the process until it crashes with gdb (via undodb) and then working my way backwards. This /sounds/ like it should be doable, but I either end up crashing gdb by using the auto-complete feature or I end up in some internal libevent structure where I get lost since there's too many possible branches (one corruption causing another and so on).
I guess it would be nice if I could see what a pointer originally belongs to/where it was allocated, that would eliminate most of the branching-issues. I cant run valgrind with undodb though, and I the normal gdb record is unusably slow (if that even works in combination with valgrind).

Code review! By myself (thoroughly) and having some friends look over my code, though I doubt it was thorough enough. I was thinking about maybe hiring a dev to do some code review/debugging with me, but I cant afford to put too much money in it and I wouldn't know where to look for someone who'd be willing to work for little-to-no money if he doesn't find the issue or anyone qualified at all.

I should also note: I usually get consistent backtraces.
There are a few places where the crash happens, mostly related to the socket class becoming corrupted somehow. Be it an invalid pointer pointing to something which isn't a socket or the socket class itself becoming overwritten (partially?) with gibberish. Although I suspect it's crashing there the most since that's one of the mostly used parts, so it's the first corrupted memory which gets used.

All in all this issue has had me busy for nearly 2 month (on and off, more of a hobby project) and is really frustrating me to the point where I become grumpy IRL and think about just giving up.
I just can't think about what else I am supposed to do to find the issue.

Are there any useful techniques I missed?
How do you  deal with that? (It can't be that common since there isn't much information about this.. or I'm just really blind?)

Edit:

Some specs in case it matters:

Using c++(11) via gcc 4.7 (version supplied by debian wheezy)

The codebase is around 150k lines

Edit in response to david.pfx post: (sorry for the slow response)

Are you keeping careful records of crashes, to look for patterns?

Yes, I still have dumps of the recent crashes lying around

Are the few places really similar? In what way?

Well, in the most recent version (they seem to change whenever I add/remove code or change related structures) it would always get caught in an item timer method. Basically an item has a specific time after which it expires and it sends updated info to the client.
The invalid socket pointer would be in the (still valid as far as I can tell) Player class, mostly related to that.
I am also experiencing loads of crashes in the cleanup phase, after the normal shutdown where it's destroying all the static classes that haven't been explicitly destroyed (__run_exit_handlers in the backtrace). Mostly involving std::map of one class, guessing that's just the first thing that comes up though.

What does the corrupt data look like? Zeros? Ascii? Patterns?

I haven't found any patterns yet, seems somewhat random to me. It's hard to tell since I don't know where the corruption started.

Is it heap-related?

It's entirely heap-related (I enabled gcc's stack guard and that didn't catch anything).

Does the corruption happen after a free()?

You're going to have to elaborate a bit on that one.
Do you mean having pointers of already free'd objects lying around?
I'm setting every reference to null once the object gets destroyed, so unless I missed something somewhere, no. That should show up in valgrind though which it didn't.

Is there something distinctive about the network traffic (buffer size, recovery cycle)?

The network traffic consists of raw data. So char arrays, (u)intX_t or packed (to remove padding) structs for more complex things, each packet has a header consisting of an id and the packet size itself which is validated against the expected size.
They are around 10-60bytes with the biggest (internal 'bootup' packet, fired once at startup) having a size of a few Mb.

Lots and lots of production asserts. Crash early and predictably before the damage propagates.

I once had a crash related to std::map corruption, each entity has a map of it's "view", each entity that can see it and vice versa is in that.
I added a 200byte buffer in front and after, filled it with 0x33 and checked it before each access. The corruption just magically vanished, I must've moved something around which made it corrupt something else.

Strategic logging, so you know accurately what was happening just before. Add to the logging as you get closer to an answer.

It works.. to an extend.

In desperation, can you save state and auto-restart? I can think of a few pieces of production software that do that.

I somewhat do that. The software consists of a main "cache" process and some other worker ones which all access the cache to get and save stuff. So per crash I don't lose much progress, it still disconnects all the users and so on, it's definitely not a solution.

Concurrency: threading, race conditions, etc

There's a mysql thread to do "async" queries, that's all untouched though and only shares information to the database class via functions with all lock.

Interrupts

There's an interrupt timer to prevent it from locking up that just aborts if it didn't complete a cycle for 30 seconds, that code should be safe though:

```<code>if (!tics) {
    abort();
} else
    tics = 0;
</code>```

tics is volatile int tics = 0; which is increased each time a cycle is completed. Old code too.

events/callbacks/exceptions: corrupting state or the stack unpredictably

Lots of callbacks are being used (async network I/O, timers), but they shouldn't do anything bad.

Unusual data: unusual input data/timing/state

I've had a few edge cases related to that. Disconnecting a socket while packets are still being processed resulted in accessing a nullptr and such, but those have been easy to spot so far since every reference gets cleaned up right after telling the class itself it's done. (Destruction itself is handled by a loop deleting all the destroyed objects each cycle)

Dependency on an asynchronous external process.

Care to elaborate? This is somewhat the case, the cache process mentioned above.
Only thing I could imagine off the top of my head would be it not finishing quick enough and using garbage data, but that's not the case since that's using network too. Same packet model.

答:
It's a challenging problem but I suspect there are a lot more clues to be found in the crashes you've already seen.

Are you keeping careful records of crashes, to look for patterns?

Are the few places really similar? In what way?

What does the corrupt data look like? Zeros? Ascii? Patterns?

Is there any multi-threading involved? Could it be a race condition?

Is it heap-related? Does the corruption happen after a free()?

Is it stack-related? Does the stack get corrupted?

Is a dangling reference a possibility? A data value that mysteriously changed?

Is there something distinctive about the network traffic (buffer size, recovery cycle)?

Things we have used in similar situations.

Lots and lots of production asserts. Crash early and predictably before the damage propagates.

Lots and lots of guards. Extra data items before and after local variables, objects and mallocs() set to a value and then checked often.

Strategic logging, so you know accurately what was happening just before. Add to the logging as you get closer to an answer.

In desperation, can you save state and auto-restart? I can think of a few pieces of production software that do that.

Feel free to add details if we can help at all.

Can I just add that seriously indeterminate bugs like this are not all that common, and there are not many things that can (usually) cause them. They include:

Concurrency: threading, race conditions, etc

Interrupts/events/callbacks/exceptions: corrupting state or the stack unpredictably

Unusual data: unsual input data/timing/state

Dependency on an asynchronous external process.

These are the parts of the code to focus on.

Title: Debugging memory corruption


问:
Assigning responsibility for cancelling an Order
During a conversation with our Domain Expert we can across this feature:

A Customer service agent can cancel an Order by decreasing its quantity. To cancel an Order we decrease its quantity by the specified quantity, change its status to either partially cancelled or fully cancelled and log the CS agent who issued the cancellation.

We are currently split between several solutions (assuming we have the agentId, orderId and cancelledQty from the request):

Solution 1:

```<code>CSAgent agent= (CSAgent) EmployeeRepository.getById(agentId);
Order order= OrderRepository.getById(orderId);
agent.cancel(order,cancelledQty);
</code>```

vs

Solution 2:

```<code>CSAgent agent= (CSAgent) EmployeeRepository.getById(agentId);
Order order= OrderRepository.getById(orderId);
order.cancel(cancelledQty,agent);
</code>```

Solution 1 really captures what the requirements is conveying and reads really well; however, solution 2 seems more natural when coding the implementation of the cancel() method as I'll mostly update fields in the Order class itself which I hope to keep private with no public setters.

The only thing I need from the CSAgnet class is a snapshot of its data to be stored with the Order.

So how should I determine the best course of action from a DDD perspective?

答:
If agent changes state then a combination is required. If not, then Solution 2 with the following twist:</p>

```<code>order.cancelByAgent(cancelledQty,agent.snapshot);
</code>```

Body:
If agent changes state then a combination is required. If not, then Solution 2 with the following twist:

order.cancelByAgent(cancelledQty,agent.snapshot);


问:
Open source from protected rights
I've got a question about author rights and open source software.

Assuming I want to build a software. A similar software is already created, but isn't free at all and owned by a company. Can a build an open source clone of the proprietary software from scratch? Or is it forbidden by intellectual property?

And what if I use proprietary ideas in the open source software?

答:
You are certainly permitted to build compatible software and there is nothing in Copyright Law that prevents you from doing so. Ideas cannot be protected, only their expression in a particular software 'work'.

The same is not true of Patent Law. This protects an 'invention' and you cannot use a patented invention in anything without permission of the patent holder.

You should bear in mind that there could be considerable financial risk in doing this. Companies that have invested money in developing software are often prepared to invest money in lawyers to protect it, and finding out whether they are right or not can be very expensive.

The odd thing is: if you try and fail, they'll probably leave you alone. The greatest risk comes when you succeed.

If you head down this path then sooner or later you will need competent legal advice, and that also costs money. By all means give it a go, but be aware of the risks.

You might have thought that releasing the source code or even making it public domain would protect you, but it does not. Any legal action would be about the damage you did to a company by your actions and releasing the source code might even aggravate the situation.

Title: Open source from protected rights


问:
Is it actually worth unit-testing an API client?
This is something that's been troubling me for a while now. Is it actually worth unit-testing an API client?

Let's say you're creating a small class to abstract-away the calls to a petshop REST API. The petshop is a very simple API, and it has a basic set of methods:

listProducts()

getProductDetails(ProductID)

addProduct(...)

removeProduct(ProductID)

In testing this, we'd have to either create a mock service or mock the responses. But that seems overkill; I understand that we want to make sure that our methods don't stop working through typo/syntax errors, but since we're writing functions that call remote methods and then we're creating fake responses from those remote methods, it does seem like a waste of effort and that we're testing something that can't really fail. Worse, if the remote method changes our unit tests will pass while production use fails.

I'm pretty sure I'm missing something, or I've got the wrong end of the stick, or I'm not seeing the wood for the trees. Can someone set me on the right track?

答:
The job of a remote API client is to issue certain calls - no more, no less. Therefore, its test should verify that it issues those calls - no more, no less.

Sure, if the API provider changes the semantics of their responses, then your system will fail in production. But that isn't your client class's fault; it's something that can only be caught in integration tests. By relying on code not under your control you have given up the ability to verify correctness via internal testing - it was a trade-off, and this is the price.

That said, testing a class that consists only of delegations to another class may be low-priority, because there is comparatively little risk of complex errors. But that goes for any class that consists only of uniform one-liners, it has nothing to do with calling out into another vendor's code.


答:
Short answer:

All methods should be unit-tested.

Long answer:

Yes. It is worth it.

These are some things unit tests on those API-calling methods should test:

That you are passing well-formed or correct parameters to the API calls.

That you are responding accordingly to certain types of data returned by the APIs (mocked or not), for example maybe when the API returns an empty string your method should return a default value (just an example)

That the caller methods behave correctly when API calls produces an error

Those are things the called method do that can be isolated my mocking the API service, and that by testing them well, assure you that errors are not originated by an error in the client code that calls the API.

Body:
This is something that's been troubling me for a while now. Is it actually worth unit-testing an API client?

Let's say you're creating a small class to abstract-away the calls to a petshop REST API. The petshop is a very simple API, and it has a basic set of methods:

listProducts()

getProductDetails(ProductID)

addProduct(...)

removeProduct(ProductID)

In testing this, we'd have to either create a mock service or mock the responses. But that seems overkill; I understand that we want to make sure that our methods don't stop working through typo/syntax errors, but since we're writing functions that call remote methods and then we're creating fake responses from those remote methods, it does seem like a waste of effort and that we're testing something that can't really fail. Worse, if the remote method changes our unit tests will pass while production use fails.

I'm pretty sure I'm missing something, or I've got the wrong end of the stick, or I'm not seeing the wood for the trees. Can someone set me on the right track?


答:
These would not be unit tests because you are testing the input and output of your system, more like limited integration tests.

Be very cautious when you say "it does seem like a waste of effort and that we're testing something that can't really fail" - it can fail, it will fail, it will probably fail in ways that you cannot anticipate, the failures will be worse if you don't  have tests in place.

The mistake you are making here is to do with the invention of wheels: Making calls to remote services and APIs is a very common scenario so there are some pretty good tools to help you test that. Last time I was working on an application that connected to remote services I used SoapUI which could look at a service and either make mock calls to that service or behave as a local copy of the server that you can make test calls against and track through the requests and responses. It took minutes to set up and likewise was very quick to update if the remote interface changed. I haven't used it in a REST scenario, but even if it doesn't work well for that ( or perhaps someone is reading this answer in the future when better tools exist ) you should be able to find a tool that can mock up a service for you and when it comes time to deploy your code, you will be glad that you did.

Title: Is it actually worth unit-testing an API client?


问:
Scaling Azure across subscriptions
I've been doing a lot of work with VMs recently and can see the massive advantages of the platform.

It's my understanding that subscriptions are a very good way of managing your clients. For example I have some software which requires a VM, a SQL database and two websites. When I sign up a new client I create a new Subscription which they pay for and deploy my various components to it - they get the hosting bill.

However, it seems to me that where Azure doesn't scale particularly well is when I have a hundred or a thousand clients. It's my understanding I'd need a publish profile for each one and every time we do a release I'd need to deploy 400 components.

Is there a better way of managing multiple instances of your solution?

答:
I agree about the advantages. I can see a lot of smaller hosting sites looking at prices and features they just can't match. We're certainly headed in that direction.

When you create an Azure subscription with a VM you are in effect rolling out another PC, so you are in the same territory as people who distribute desktop, tablet or phone apps. They are separate instances of the running software and they have to be updated separately.

The solution as I see it is that your software needs updating capability, exactly as it would for any other app. It is the responsibility of each VM and the instance of the software to execute an update and all that entails. To support that you need an update server, which sits right alongside your licensing server and your logging server, and which you will also host on Azure!

Title: Scaling Azure across subscriptions


答:
'Proprietary  ideas' the bizarre concept that it is, is often covered by patent law.

You can however clone to your hearts content. Bear in mind that clones sufficiently close in appearance can land you in legal hot water. The sufficiency of that is determined by the pursuant, the judge, and/or the jury.

One thing that is important, you can not use any assets that you do not have a right to use. Assets include sound and graphics.

You may also be hit with a cease and desist. These are about as friendly as it gets. They ask you to stop or they will pursue legal claims. Sometimes these C&D orders have a good standing, other times they are threats on shaky ground.

Title: Open source from protected rights

Body:
I've got a question about author rights and open source software.

Assuming I want to build a software. A similar software is already created, but isn't free at all and owned by a company. Can a build an open source clone of the proprietary software from scratch? Or is it forbidden by intellectual property?

And what if I use proprietary ideas in the open source software?


答:
Your Solution 2 is the better option because of single responsibility and encapsulation concerns.

```<code>CSAgent agent= (CSAgent) EmployeeRepository.getById(agentId);
Order order= OrderRepository.getById(orderId);
order.cancel(cancelledQty,agent);
</code>```

While an Agent invokes the cancel(), a cancellation is meaningless outside the context of an oder.  What you have described is a classic example of an Actor acting upon an Object, invoking a method of the Object.

Putting the 'cancel()method withinCSAgentmoves theCSAgent` object a little closer to becoming a "god object."

Your additional comment of:

The only thing I need from the CSAgnet class is a snapshot of its data to be stored with the Order.

reinforces why the responsibility of cancelling an order belongs to the Order itself.

Your additional or potential requirement of a partial cancellation adds to the justification for the second solution.  A partial cancellation will likely have additional logic beyond a complete cancellation.  That's business logic that belongs to the Order and is something that the CSAgent should be blissfully ignorant of.

Title: Assigning responsibility for cancelling an Order

Body:
During a conversation with our Domain Expert we can across this feature:

A Customer service agent can cancel an Order by decreasing its quantity. To cancel an Order we decrease its quantity by the specified quantity, change its status to either partially cancelled or fully cancelled and log the CS agent who issued the cancellation.

We are currently split between several solutions (assuming we have the agentId, orderId and cancelledQty from the request):

Solution 1:

```<code>CSAgent agent= (CSAgent) EmployeeRepository.getById(agentId);
Order order= OrderRepository.getById(orderId);
agent.cancel(order,cancelledQty);
</code>```

vs

Solution 2:

```<code>CSAgent agent= (CSAgent) EmployeeRepository.getById(agentId);
Order order= OrderRepository.getById(orderId);
order.cancel(cancelledQty,agent);
</code>```

Solution 1 really captures what the requirements is conveying and reads really well; however, solution 2 seems more natural when coding the implementation of the cancel() method as I'll mostly update fields in the Order class itself which I hope to keep private with no public setters.

The only thing I need from the CSAgnet class is a snapshot of its data to be stored with the Order.

So how should I determine the best course of action from a DDD perspective?

Body:
Your Solution 2 is the better option because of single responsibility and encapsulation concerns.

CSAgent agent= (CSAgent) EmployeeRepository.getById(agentId);
Order order= OrderRepository.getById(orderId);
order.cancel(cancelledQty,agent);

While an Agent invokes the cancel(), a cancellation is meaningless outside the context of an oder.  What you have described is a classic example of an Actor acting upon an Object, invoking a method of the Object.

Putting the 'cancel()method withinCSAgentmoves theCSAgent` object a little closer to becoming a "god object."

Your additional comment of:

The only thing I need from the CSAgnet class is a snapshot of its data to be stored with the Order.

reinforces why the responsibility of cancelling an order belongs to the Order itself.

Your additional or potential requirement of a partial cancellation adds to the justification for the second solution.  A partial cancellation will likely have additional logic beyond a complete cancellation.  That's business logic that belongs to the Order and is something that the CSAgent should be blissfully ignorant of.


问:
Checked vs unchecked exception when validating documents in this service
I have a service that allow users to add dynamic content to a repository. So basically I have a generic Document class that contains a list of property for that specific object depending on what type of document the user is adding (eg. an invoice document has an invoice number property while a wiki document has an author property, and so on).

The service is comprised of different layers and at some point I have a class that has to check if the document to be added is compliant to the rules configurer, evaluating if all required properties are provided, if they are all of the right type, etc. If any of these validations fail I want to throw a custom exception which contains the validation status.

The question is:

Should my ValidationException be checked or unchecked?

I read a lot of best practice on how to decide what kind of exception use. I was thinking about using RuntimeException but in this case the exception is not caused by error in coding or stuff like that but just by the user input...

On the other hand using a checked exception would mean to propagate the "throws" syntax in all the above layer of the application and probably in the 90% of the service's methods making the code much less readable and maintainable.

答:
I think Oracle docs can't be more explicit about it:

Generally speaking, do not throw a RuntimeException or create a subclass of RuntimeException simply because you don't want to be bothered with specifying the exceptions your methods can throw.

Here's the bottom line guideline: If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception.

Source here...

You are the only one who knows if this is the case.

I believe that the real problem is not whether it should be a checked or an unchecked exception, but the many levels you would have to propagate the checked exception (as you said) for it to be properly handled. Perhaps you could tell us a little bit more about your design.


答:
What you are looking for is likely an accumulation branch.

If you have just two branches - the development branch and mainline, and main line has changed since the development branch was made and merges from mainline to the development branch wasn't done during the process:

```<code>+------.----.------dev-------------------------...
   /     /     /
  /     /     /
+------------------mainline--------------------+
</code>```

Then its just a 'simple' matter of doing another merge from the mainline to dev, fixing up the merge in dev, and then merging back to mainline (which should have had minimal changes between the two merges) and closing the branch.

```<code>+------.-----.-----dev---.-----.+
   /     /     /             /   \
  /     /     /             /     \
+------------------mainline--------------------+
</code>```

The key here is that you really should avoid having a broken build from mainline.  Mainline is in most places a 'this is good' and what people branch from.  If you instead did just a 'merge to mainline and fix':

```<code>+-----.-----.------dev----+
   /     /     /           \         finally fixed
  /     /     /             \        v
+------------------mainline--.---.---.---------+
</code>```

It is possible that the someone could have branched from the broken state in mainline during before those were ready and stable.  This is often seen as a bad thing.

One of the bits with this that can cause some issue is that after development is done on the dev branch, its role/policy changes.  Development happens in a dev branch... but after development is done, you're doing something else in the dev branch.  This goes against some patterns of branching.  You may not use this pattern, but its something to be aware of and consider.

The above case was the simple one - one dev branch.  But what if you have two dev branches that need to be reconciled?

I point to Advanced SCM Branching Strategies by Stephen Vance (a really good read when it comes to branching and central version control style systems (rather than distributed - though if you read it you can certainly find parallels between this document and things like git flow).

Vance identifies specific roles that a branch can be.  You've got mainline, development, maintenance, accumulation, and packaging.  In this case, we're dealing with mainline, dev, and ultimately accumulation.

The problem with working with two branches and trying to do the merge into the mainline is that it can make it even more of a mess for a longer period of time.

The solution is to introduce an accumulation branch.

In the previous example, accumulation role was also part of the mainline branch (which has the mainline role too).  It looked like this:

But, when things get hairy with merges, having the mainline and accumulation roles both on the mainline branch can lead to issues.  So instead, create an accumulation branch to which you merge the development branches which can then be reconciled there and merged back into the mainline.  This particular image also shows a packaging branch, but thats not at issue here.

One important bit to note here is that everything branches from mainline (not some other branch), has a life cycle, merges back into mainline, and then is closed.  There are no long running branches (other than mainline).  Each branch has a specific role to fulfill and when that is complete the branch is removed.

Body:
What you are looking for is likely an accumulation branch.

If you have just two branches - the development branch and mainline, and main line has changed since the development branch was made and merges from mainline to the development branch wasn't done during the process:

+------.----.------dev-------------------------...
   /     /     /
  /     /     /
+------------------mainline--------------------+

Then its just a 'simple' matter of doing another merge from the mainline to dev, fixing up the merge in dev, and then merging back to mainline (which should have had minimal changes between the two merges) and closing the branch.

+------.-----.-----dev---.-----.+
   /     /     /             /   \
  /     /     /             /     \
+------------------mainline--------------------+

The key here is that you really should avoid having a broken build from mainline.  Mainline is in most places a 'this is good' and what people branch from.  If you instead did just a 'merge to mainline and fix':

+-----.-----.------dev----+
   /     /     /           \         finally fixed
  /     /     /             \        v
+------------------mainline--.---.---.---------+

It is possible that the someone could have branched from the broken state in mainline during before those were ready and stable.  This is often seen as a bad thing.

One of the bits with this that can cause some issue is that after development is done on the dev branch, its role/policy changes.  Development happens in a dev branch... but after development is done, you're doing something else in the dev branch.  This goes against some patterns of branching.  You may not use this pattern, but its something to be aware of and consider.

The above case was the simple one - one dev branch.  But what if you have two dev branches that need to be reconciled?

I point to Advanced SCM Branching Strategies by Stephen Vance (a really good read when it comes to branching and central version control style systems (rather than distributed - though if you read it you can certainly find parallels between this document and things like git flow).

Vance identifies specific roles that a branch can be.  You've got mainline, development, maintenance, accumulation, and packaging.  In this case, we're dealing with mainline, dev, and ultimately accumulation.

The problem with working with two branches and trying to do the merge into the mainline is that it can make it even more of a mess for a longer period of time.

The solution is to introduce an accumulation branch.

In the previous example, accumulation role was also part of the mainline branch (which has the mainline role too).  It looked like this:

But, when things get hairy with merges, having the mainline and accumulation roles both on the mainline branch can lead to issues.  So instead, create an accumulation branch to which you merge the development branches which can then be reconciled there and merged back into the mainline.  This particular image also shows a packaging branch, but thats not at issue here.

One important bit to note here is that everything branches from mainline (not some other branch), has a life cycle, merges back into mainline, and then is closed.  There are no long running branches (other than mainline).  Each branch has a specific role to fulfill and when that is complete the branch is removed.


答:
I have a relevant anecdote from something that's going on right now for me.  I'm on a project that does not use TDD.  Our QA folks are moving us in that direction, but we're a small outfit and it has been a long, drawn-out process.

Anyways, I was recently using a third-party library to do a specific task.  There was an issue regarding the use of that library, so it's been put on me to essentially write a version of that same library on my own.  In total, it ended up being about 5,000 lines of executable code and about 2 months of my time.  I know lines of code is a poor metric, but for this answer I feel it's a decent indicator of magnitude.

There was one particular data structure I needed which would allow me to keep track of an arbitrary number of bits.  Since the project is in Java, I chose Java's BitSet and modified it a bit(I needed the ability to track the leading 0s as well, which Java's BitSet doesn't do for some reason.....).  After reaching ~93% coverage I started writing some tests that would actually stress the system I had written.  I needed to benchmark certain aspects of the functionality to ensure they would be fast enough for my end requirements.  Unsurprisingly, one of the functions I had overridden from the BitSet interface was absurdly slow when dealing with large bit sets(hundreds of millions of bits in this case).  Other overridden functions relied on this one function, so it was a huge bottle neck.

What I ended up doing was going to the drawing board, and figuring out a way to manipulate the underlying structure of BitSet, which is a long[].  I designed the algorithm, asked colleagues for their input, and then I set about writing the code.  Then, I ran the unit tests.  Some of them broke, and the ones that did pointed me exactly to where I needed to look in my algorithm in order to fix it.  After fixing all of the errors from the unit tests, I was able to say that the function works as it should.  At the very least, I could be as confident that this new algorithm worked as well as the previous algorithm.

Of course, this is not bullet proof.  If there's a bug in my code that the unit tests aren't checking for, then I won't know it.  But of course, that exact same bug could have been in my slower algorithm as well.  However, I can say with a high degree of confidence that I don't have to worry about the wrong output from that particular function.  Pre-existing unit tests saved me hours, perhaps days, of trying to test the new algorithm to ensure it was correct.

That is the point of having unit tests regardless of TDD - that is to say, unit tests will do this for you in TDD and outside of TDD all the same, when you end up refactoring/maintaining the code.  Of course, this should be paired with regular regression testing, smoke testing, fuzzy testing, etc, but unit testing, as the name states, tests things on the smallest, atomic level possible, which gives you direction on where errors have popped up.

In my case, without the existing unit tests, I would somehow have to come up with a method of ensuring the algorithm works all of the time.  Which, in the end...sounds a lot like unit testing, doesn't it?

Title: Sense of unit tests without TDD

Body:
We have new (quite big) project starting, that we planned to develop using TDD.

The idea of TDD failed (many business and non-business reasons), but right now we have a conversation - should we write unit tests anyway, or not.
My friend says that there is no (or close to zero) sense in writing unit tests without TDD, we should focus only on integration tests. I believe the opposite, that there is still some sense in writing plain unit tests, just to make code more futureproof. What do you think?

Added:
I think that this is not a duplicate of >>this question<< - I understand difference between UT and TDD. My question is not about differences, but about sense of writing Unit Tests without TDD.


问:
What to do about "stopping point syndrome"?
I have noticed a behavior in myself that I call "stopping point syndrome"... it means I cannot stop working until I reach a "stopping point" (or I become exhausted).  In other words, if I am in the zone working on getting a feature completed, I won't stop until I get it done.   If it's a bug I'm working on, this effect is even more pronounced... I won't stop until it's fixed, or at the very least, understood.  Even though I work from a laptop and have VPN and could easily go home and pick up on the work later that evening, that fact does not help me leave work... it's like I'm afraid I will die before I find the bug.

I have a very hard time explaining this to family members who can't figure out why I never come home from work at the same time and sometimes come home at 11 pm.  Even managers have been mystified by this, and I've noticed that most of my colleagues have no trouble working the same hours every day.

Does anyone else have this problem?  Should I be worried about this and/or try to change it?  If so how?

答:
I have a stopping point - it's at about 1:30am. I can stay awake past then, but there's no way I will get any work of any sort done. And I'll be worth nothing the next day unless I sleep in.

So, I've learned to stop working after 11 and go to bed. That way I am fresh the next day and can work steadily through the week.

ContentLicense: CC BY-SA 2.5


问:
How to use Option Type Pattern in a language that doesn't support generics?
I've been interested to start using the Null Object / Option Type approach for replacing my old habits of null-checking multiple parts of my code. But, from the many examples I saw out there, it seems it's a pattern used in conjunction with generics.

Great for C#, Java, Haxe and a bunch of others I'm sure.

NOT so great for ActionScript 3.0 (which is what I was intending to use it for).

So, am I under the wrong impression that this won't be possible / worthwhile in a language that doesn't support Generics? Or is there a way to pull it off without them?

NOTE: It seems the "Null Object" pattern shown in this PatternCraft tutorial doesn't quite resemble the way Option Type was described in some of the other discussions in this stackexchange site. His way looks more like a factory class that spits out a Null??? derivative of some base-class. I was hoping there could be a way to create Null-Objects without needing to write a dedicated class for every single case scenarios where a null-check would typically be used.

EDIT: My apologies, I didn't realize "Null Object" and "Option Type" were two completely different beasts. For the sake of keeping the answers and feedback relevant to the question though, I only changed the title (was originally How to use Null Object / Option Type Pattern in a language that doesn't support generics?).

答:
Option is isomorphic to a collection which can only ever be empty or contain a single element. So, you implement it the same way you would any other collection.

If you can implement a list, set, map, tree, heap, stack, array, trie, treap etc. in your language, then you can implement Option in exactly the same way.

See AS3Commons Collections for a comprehensive collections framework implemented in ActionScript. (Note: it doesn't seem to be as powerful as Scala's, but it can at least compare favorably with Java's.) In essence, without parametric polymorphism, you lose type-safety, the same way you do with Java 1.1, .NET 1.0, C, Pascal, Modula, etc.


问:
UML: Queue processor in a sequence diagram
Which is the appropriate way of representing a queue processor in a UML sequence diagram?

I want to represent in the same diagram two systems that are loosely coupled through a queue. The first system, performs some actions and insert an action in a queue, the other system is always listening and when the event appears in the queue, it dequeues the event and processes it, generating new actions. They are both in the same diagram because it is to represent an integration use case that shows how both work together.

Should I create a life line representing the queue?

Should I represent the "listening" process as a loop box containing: a self-message with the label "listen", self-message for processing message and messages representing the sending of the outcome.

答:
As the queue is an important component in the sequence you are presenting, it should most definitely be present with a lifeline.

As the consumer explicitly listens for events from the queue, I would start the diagram with the listen call from the consumer to the queue.

After that, the producer can insert its event into the queue (possibly with an indication that a considerable amount of time can elapse between the listen call and the insertion of the event). At this point, I would use two return arrows. One back to the producer to indicate completion of the insert call, and the other to the consumer to indicate completion of the listen call.

This is under the assumption that the producer and consumer execute asynchronously.

It would look like this:

```<code>+---+       +---+       +---+
| P |       | Q |       | C |
+---+       +---+       +---+
  |           |   listen  |
  |           | <|------- |
  |           |           |
  |  insert   |           |
  | -------|> ++          |
  | < - - - - ++  event   |
  |           ++ - - - - >++
  |           |           ++
  |           |           ++
  |           |   listen  ++
  |           | <|------- ++
  |           |           |
</code>```

To indicate that the consumer is always listening, I have ended the diagram again with a listen call that hasn't returned.

Body:
As the queue is an important component in the sequence you are presenting, it should most definitely be present with a lifeline.

As the consumer explicitly listens for events from the queue, I would start the diagram with the listen call from the consumer to the queue.

After that, the producer can insert its event into the queue (possibly with an indication that a considerable amount of time can elapse between the listen call and the insertion of the event). At this point, I would use two return arrows. One back to the producer to indicate completion of the insert call, and the other to the consumer to indicate completion of the listen call.

This is under the assumption that the producer and consumer execute asynchronously.

It would look like this:

+---+       +---+       +---+
| P |       | Q |       | C |
+---+       +---+       +---+
  |           |   listen  |
  |           | <|------- |
  |           |           |
  |  insert   |           |
  | -------|> ++          |
  | < - - - - ++  event   |
  |           ++ - - - - >++
  |           |           ++
  |           |           ++
  |           |   listen  ++
  |           | <|------- ++
  |           |           |

To indicate that the consumer is always listening, I have ended the diagram again with a listen call that hasn't returned.


答:
The purpose of option types is to let the compiler know exactly where a value may be empty in a language where null simply does not exist (typical for any language that supports algebraic data types).  This way, the compiler can give compiler-time errors if you forget to check for emptyness.  Here's an example in Rust, an imperative language with a C-like syntax:

```<code>let my_number : Option<int> = read_number();
return 2 * my_number;          // compile error
</code>```

This won't compile because my_number does not contain an int but an Option<int> (i.e. an int that may be empty).  The compiler does not allow multiplication on Option<int>.

Therefore, it is necessary to pattern match my_number in order to extract its value:

```<code>let my_number : Option<int> = read_number();
match (my_number) {
    Some(my_actual_number) =>
        return Some(2 * my_actual_number);  // ok
    None ->
        return None;                        // fallback if it's empty
</code>```

The pattern matching forces the programmer to handle every possible scenario: in this case, the None case.  This is how it prevents the typical "forgot to check for null" problem.

However, in a language where null is pervasive, option types aren't nearly as useful since everything can already be null anyway.  Therefore the compiler can't really help you (since inferring the nullability would require solving the halting problem).

The null object pattern not the same thing, but it is a related concept.

In languages that have first-class support for option types, one is permitted to apply an operation to the internal object inside the option type, if it exists.  It serves as a shortcut for the common activity of "do X to the object, but if it's empty then forget about it".  This process is typically called "mapping" and this operation can be defined for all option types.

The map operation takes an option object (Option<T>), as well as a function that can act on the internal object (T -> R), and produces the result of the function wrapped in an option object (Option<R>).

In fact, the example above can be rewritten much more succinctly using a map combined with an anonymous function:

```<code>let my_number : Option<int> = read_number();
return my_number.map(
    // use anonymous function to specify what to do
    |my_actual_number| 2 * my_actual_number
);
</code>```

Map can be thought of as a recipe that:

If the option object is not empty, then apply the provided function and return the result in a Some(...).

If the option object is in fact empty, then do nothing and just return None.

In the null object pattern, one uses a "null" class that possesses same methods as the non-null class (they could be siblings in the inheritance hierarchy), but arranged in such a way that the operations on the "null" class simply do nothing.  Hence, the null object pattern is similar in that it accomplishes this same goal, albeit in a different, more object-oriented/duck-typing way.

Title: How to use Option Type Pattern in a language that doesn't support generics?

Body:
I've been interested to start using the Null Object / Option Type approach for replacing my old habits of null-checking multiple parts of my code. But, from the many examples I saw out there, it seems it's a pattern used in conjunction with generics.

Great for C#, Java, Haxe and a bunch of others I'm sure.

NOT so great for ActionScript 3.0 (which is what I was intending to use it for).

So, am I under the wrong impression that this won't be possible / worthwhile in a language that doesn't support Generics? Or is there a way to pull it off without them?

NOTE: It seems the "Null Object" pattern shown in this PatternCraft tutorial doesn't quite resemble the way Option Type was described in some of the other discussions in this stackexchange site. His way looks more like a factory class that spits out a Null??? derivative of some base-class. I was hoping there could be a way to create Null-Objects without needing to write a dedicated class for every single case scenarios where a null-check would typically be used.

EDIT: My apologies, I didn't realize "Null Object" and "Option Type" were two completely different beasts. For the sake of keeping the answers and feedback relevant to the question though, I only changed the title (was originally How to use Null Object / Option Type Pattern in a language that doesn't support generics?).

Body:
The purpose of option types is to let the compiler know exactly where a value may be empty in a language where null simply does not exist (typical for any language that supports algebraic data types).  This way, the compiler can give compiler-time errors if you forget to check for emptyness.  Here's an example in Rust, an imperative language with a C-like syntax:

let my_number : Option<int> = read_number();
return 2 * my_number;          // compile error

This won't compile because my_number does not contain an int but an Option<int> (i.e. an int that may be empty).  The compiler does not allow multiplication on Option<int>.

Therefore, it is necessary to pattern match my_number in order to extract its value:

let my_number : Option<int> = read_number();
match (my_number) {
    Some(my_actual_number) =>
        return Some(2 * my_actual_number);  // ok
    None ->
        return None;                        // fallback if it's empty

The pattern matching forces the programmer to handle every possible scenario: in this case, the None case.  This is how it prevents the typical "forgot to check for null" problem.

However, in a language where null is pervasive, option types aren't nearly as useful since everything can already be null anyway.  Therefore the compiler can't really help you (since inferring the nullability would require solving the halting problem).

The null object pattern not the same thing, but it is a related concept.

In languages that have first-class support for option types, one is permitted to apply an operation to the internal object inside the option type, if it exists.  It serves as a shortcut for the common activity of "do X to the object, but if it's empty then forget about it".  This process is typically called "mapping" and this operation can be defined for all option types.

The map operation takes an option object (Option<T>), as well as a function that can act on the internal object (T -> R), and produces the result of the function wrapped in an option object (Option<R>).

In fact, the example above can be rewritten much more succinctly using a map combined with an anonymous function:

let my_number : Option<int> = read_number();
return my_number.map(
    // use anonymous function to specify what to do
    |my_actual_number| 2 * my_actual_number
);

Map can be thought of as a recipe that:

If the option object is not empty, then apply the provided function and return the result in a Some(...).

If the option object is in fact empty, then do nothing and just return None.

In the null object pattern, one uses a "null" class that possesses same methods as the non-null class (they could be siblings in the inheritance hierarchy), but arranged in such a way that the operations on the "null" class simply do nothing.  Hence, the null object pattern is similar in that it accomplishes this same goal, albeit in a different, more object-oriented/duck-typing way.


问:
Why is cleverness considered harmful in programming by some people?
I've noticed a lot of questions lately relating to different abstraction techniques, and answers saying basically that the techniques in question are "too clever." I would think that part of our jobs as programmers is to determine the best solutions to the problems we are given to solve, and cleverness is helpful in doing that.

So my question is: are the people who think certain abstraction techniques are too clever opposed to cleverness per se, or is there some other reason for the objection?

EDIT: This parser combinator is an example of what I would consider to be clever code. I downloaded this and looked it over for about half an hour. Then I stepped through the macro expansion on paper and saw the light. Now that I understand it, it seems much more elegant than the Haskell parser combinator.

答: If the 'clever' solution is difficult to figure out, then it shouldn't be used. For instance, if through side effects you can contract a complex calculation to one line, it's clever. But if it takes an hour for someone else to figure out what in the world you did, it's too clever.</p>


问:
Aggregation vs Composition
I understand what composition is in OOP, but I am not able to get a clear idea of what Aggregation is. Can someone explain?</p>

答:
Semantically, all sets are made of subsets, right? Therefore:

The aggregation is when those subsets exists independently of the father set. As a monitor can be unplugged from the computer to be connected to another.

The composition is when those subsets depends of the existence of the father set. As a leaf is a part of a tree or liver is a part of a body.

These concepts talks about the kind of dependency between two objects or classes, conceptually. Directly in a program, in an aggregation, when the father object disposes, the aggregate objects should be disposed too. In the same scenario for a composition, composite son objects will persist then the father object dispenses.

Title: Aggregation vs Composition


问:
Implied precision of floating-point functions
While reviewing another programmer's implementation of a function to calculate the normal distribution CDF, I made a suggestion to either replace the entire implementation with Python's built-in functions or use SciPy, a common scientific library.

Another programmer pointed out that neither math.erfc() nor scipy.stats.norm.cdf() provides any precision guarantees in their documentation.  Therefore, I should be more cautious about replacing an approximation algorithm (which was taken from a respected source, and which had documented error bounds).

To be honest, the thought to doubt the accuracy and precision of a built-in or library function had never crossed my mind.  After all, I've been calling functions like sin() and sqrt() for years without much thought — why should math.erf() or scipy.stats.norm.cdf() be any different?

But now, I'm concerned.  My questions are:

In general, if the documentation makes no special mention, is it implied that these kinds of functions are completely accurate to the last decimal place, within the precision offered by IEEE double-precision floating-point?

Is that true for Python's math.erf() or SciPy's scipy.stats.norm.cdf() in particular?  How can you tell?

This man page for sin() says…

These functions may lose accuracy when their argument is near a multiple of pi or is far from 0.0.

Why should such caveats exist, when the sine function is periodic and symmetrical?  There seems to be a burden placed on the caller to canonicalize the input to obtain optimal accuracy.

On the other hand, Mozilla's documentation for Math.sin() says nothing about accuracy or precision.  Does that mean that it is fully accurate, or is it "common knowledge" that Math.sin() would only be accurate in certain circumstances in JavaScript, like everywhere else?

答:
If the documentation makes no special mention, is it implied that these kinds of functions are completely accurate to the last decimal place, within the precision offered by IEEE double-precision floating-point?

I wouldn't make that assumption.

Where I work we deal with telemetry data, and it's common knowledge that two different math libraries can produce two different results, even if they both conform to the IEEE floating point standards.  This has implications when you're trying to repeat a calculation and compare two results for equality.

There seems to be a burden placed on the caller to canonicalize the input to obtain optimal accuracy.

That's a fair assessment.  What makes it acceptable is that the documentation states that, so that there are no surprises.

On the other hand, Mozilla's documentation ...

I suggest that you try a few calculations in each library or programming language (especially near the boundary areas like sin() near a multiple of pi), and compare them.  This should give you a fair idea of what sort of behavior you can expect from each one.


答:
If you mean how to draw sequence diagram for something similar to this scenario..

..then I would also model the central "tuple space" or the central multi-agent coordination structure as dumb unconscious, but living actor which would metabolize some scent molecules whenever it "eats" something through the membrane. Agents in the neighborhood then might "smell" it and metabolize further..

wearing your shoes

..(1) yes I would create a lifeline representing the queue

..(2) and I would also draw the "queue changed" signal broadcasted by the queue upon receiving new token same as @bart-van-ingen-schenau

..(3) and yes I would represent the "listening" process as loop box on its own lifeline as you suggest

..and I would also consider drawing the scenario (use case) from different perspectives with different focus using different diagrams, mainly I would think about drawing an overview diagram using the language "BPMN 2.0 - Business Process Model and Notation" (cheat sheet is available here: http://www.bpmb.de/images/BPMN2_0_Poster_EN.pdf)

Title: UML: Queue processor in a sequence diagram

Body:
Which is the appropriate way of representing a queue processor in a UML sequence diagram?

I want to represent in the same diagram two systems that are loosely coupled through a queue. The first system, performs some actions and insert an action in a queue, the other system is always listening and when the event appears in the queue, it dequeues the event and processes it, generating new actions. They are both in the same diagram because it is to represent an integration use case that shows how both work together.

Should I create a life line representing the queue?

Should I represent the "listening" process as a loop box containing: a self-message with the label "listen", self-message for processing message and messages representing the sending of the outcome.


问:
Lazy-initialization vs preprocessing in image-based iOS app
I am making an iOS app which contains museum gallery: user can look through exhibits and then select info about it, etc.

On the exhibit screen I have two ScrollViews that are different, but are being fed by common data source. Each view is a set of images. Two top grey squares are the previews and the bottom one (where you can see the picture) is the main view. All scrollviews are lazy-loaded.

The question is, do I need to transform preview images to proper size on-the-go, or somehow pre-process them to make loading faster and use that pre-processed and resized images in previews? The benefit of lazy-initialisation here is lower waste of resources, but slower loading, compared to ready images. On the other side, preprocessed images will waste storage and network (images will be loaded only once and then will be stored locally). What would be better from performance point of view, taking in account that app has to run smoothly down to iPhone 4 (oldest with iOS 7)? Is it good idea to combine two methods and cache lazy-loaded images?

答:
I am sure you are familiar with this idea, but as I was reading your question, this quote from Donald Knuth popped into my head:

Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered. We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%.

This very may well be a case that falls within the critical 3% mentioned, but you should probably try the easiest of the approaches you mentioned and make sure. Then, if you can measure a problem, you can optimize for it.


问:
Is the inconvenience about merging in SVN prior to v1.5 obsolete by now when lack of metadata isn't the case anymore?
I'm getting started with SVN and so many sources say that merging is very difficult in SVN as compared to DVCS tools. The most recent questions I could find here on SE are from 2012.

Sometimes there's a mention that the reason is that SVN prior to v1.5 didn't have metadata, but SVN is at version 1.8.9 now.

Given that SVN is now much more mature than v1.5, and especially the fact that we didn't use SVN 1.5 so we don't suffer from the mentioned lack of metadata -- is there still much validity in those arguments against SVN?

I understand that DVCS has a completely different approach which is often more desirable, but for those who "must" ues SVN for whatever reason, merging isn't really "hell" anymore, is it?

答:
From my experience, merging in SVN was 'fixed' in version 1.6. I work in both Mercurial and SVN, and since version 1.6 of SVN, merging seems to be about the same amount of work on both platforms. The one exception might be that you have to remember to provide the --reintegrate option when merging from a branch back into the trunk using SVN.

This is only my operational experience. I don't know anything about the internals of SVN.


答:
It works fine if you stick to the simple cases, but there are some complex ones that don't.

The limitations I can think of:

It can only find the most recent ancestor if it is on one of the branches involved. So if you create branches/this and branches/that both from trunk and then try to merge branches/this to branches/that, it will not know what to do. Which means you can only merge branch to or from it's parent. You may run into this if you start two feature branches and later realize that the features are interdependent and need to combine them.

While it claims it can track renames, merging branches when files were moved on one side and modified on the other does not always find the right files to merge and manually fixing it up is somewhat tedious as it does not leave the necessary information around anywhere at hand.

Added files sometimes cause spurious conflicts on later merges.

Since subversion does not have separate concept of branch, you can merge only a subtree of a project and that can lead to big mess pretty quickly. It is strongly recommended to take care to always merge complete branches. Unfortunately for some reason sometimes the merge info properties appear on subdirectories even if they seem superfluous and the merge was correctly done to the whole branch.

Last but not least it is slow. Merges on a project of any serious size often takes minutes where most DVCS can do it under a second.

Title: Is the inconvenience about merging in SVN prior to v1.5 obsolete by now when lack of metadata isn't the case anymore?

Body:
I'm getting started with SVN and so many sources say that merging is very difficult in SVN as compared to DVCS tools. The most recent questions I could find here on SE are from 2012.

Sometimes there's a mention that the reason is that SVN prior to v1.5 didn't have metadata, but SVN is at version 1.8.9 now.

Given that SVN is now much more mature than v1.5, and especially the fact that we didn't use SVN 1.5 so we don't suffer from the mentioned lack of metadata -- is there still much validity in those arguments against SVN?

I understand that DVCS has a completely different approach which is often more desirable, but for those who "must" ues SVN for whatever reason, merging isn't really "hell" anymore, is it?


问:
Sense of unit tests without TDD
We have new (quite big) project starting, that we planned to develop using TDD.

The idea of TDD failed (many business and non-business reasons), but right now we have a conversation - should we write unit tests anyway, or not.
My friend says that there is no (or close to zero) sense in writing unit tests without TDD, we should focus only on integration tests. I believe the opposite, that there is still some sense in writing plain unit tests, just to make code more futureproof. What do you think?

Added:
I think that this is not a duplicate of >>this question<< - I understand difference between UT and TDD. My question is not about differences, but about sense of writing Unit Tests without TDD.

答:
You can break code roughly into 4 categories:

Simple and rarely changes.

Simple and frequently changes.

Complex and rarely changes.

Complex and frequently changes.

Unit tests become more valuable (likely to catch important bugs) the further down the list you go.  In my personal projects, I almost always do TDD on category 4.  On category 3 I usually do TDD unless manual testing is simpler and faster.  For example, antialiasing code would be complex to write, but much easier to verify visually than writing a unit test, so the unit test would only be worth it to me if that code changed frequently.  The rest of my code I only put under unit test after I find a bug in that function.

It's sometimes difficult to know beforehand what category a certain block of code fits into.  The value of TDD is you don't accidentally miss any of the complex unit tests.  The cost of TDD is all the time you spend writing the simple unit tests.  However, usually people experienced with a project know with a reasonable degree of certainty what category different parts of code fit into.  If you aren't doing TDD, you should at least try to write the most valuable tests.

Title: Sense of unit tests without TDD


问:
How should I design the class for a database entity?
I'm studying OOP PHP and have watched two tutorials that implement user login\registration system as an example. But implementation varies. Which way will be more correct one to work with data such as this?

Load all data retrieved from database as array into a property called something like _data on class creation and further methods operate with this property

Create separate properties for each field retrieved from database, on class creation load all data fields into respective properties and operate with that properties separately?

Then let's say I want to create a method that returns a list of all users with their data. Which way is better?

Method that returns just an array of userdata like this:

```<code>Array([0]=>array([id] => 1, [username] => 'John', ...),
      [1]=>array([id] => 2, [username] => 'Jack', ...),
      ...)
</code>```

Method that creates a new instance of it's class for each user and returns an array of objects

答:
#2: Create separate properties for each field retrieved from database, on class creation load all data fields into respective properties and operate with that properties separately?

This second option (using a class) is far better in the long term. A common PHP anti-pattern is to pass around everything as key-value arrays, and it eventually becomes a huge pain in the ass. Having a private $this->__data object is the first step down that perilous path.

It will be much easier to understand, debug, and refactor, and as a bonus your IDE can usually help you autocomplete things.

ContentLicense: CC BY-SA 4.0


答:
The best solution is not always the most clever solution. Sometimes simple solutions are equally good.

In Software you always need to think in terms of maintainability. If a piece of code is too clever for someone who is going to maintain it, I would say that cleverness is not worth it.

Title: Why is cleverness considered harmful in programming by some people?

Body:
I've noticed a lot of questions lately relating to different abstraction techniques, and answers saying basically that the techniques in question are "too clever." I would think that part of our jobs as programmers is to determine the best solutions to the problems we are given to solve, and cleverness is helpful in doing that.

So my question is: are the people who think certain abstraction techniques are too clever opposed to cleverness per se, or is there some other reason for the objection?

EDIT: This parser combinator is an example of what I would consider to be clever code. I downloaded this and looked it over for about half an hour. Then I stepped through the macro expansion on paper and saw the light. Now that I understand it, it seems much more elegant than the Haskell parser combinator.


问:
How to route messages between clients using a central server in Python
I've got three Raspberry Pis sitting around. I want 2 of them to be able to chat while the 3rd routes the messages (acts as a server between them).

The general flow of events should be something like this:

Server starts running on Pi #1

Pi #2 starts running and connects to the server (who's IP will be static I guess) with a name he chooses. Pi #3 does the same as #2.

Pi #3 can then, knowing the name of Pi #2, send a message to Pi #2 using ... something.

This is the general outline of what I want to achieve.

I'm not sure what the server that runs on Pi #1 should be (I've heard of webserver frameworks like Flask but I don't have enough knowledge to determine if they fit my needs).

I'm also not sure on what I should be using for the client side (Pi #2,3). I could probably use sockets but I assume there is a better / easier way.

答:
You should look into message brokers like RabbitMQ and ActiveMQ. There are AMQP and STOMP client libraries in Python you can use with them.

Alternatively, if you want more control you could use ZeroMQ, but that will also require more work from you on the server side.


答:
Back in the days of old, one would indeed work and worry about sockets and the down and dirty of network programming.  Those days are gone for nearly every modern language - there are easier solutions.

The essence of what you are describing is that of a message queue.  These are things that have been built and can be used with rather little additional overhead.

There are two approaches to working with a message queue one to one messages and a publish/subscribe model (often written 'pubsub').

The essence of the one to one is that you connect to the server running a message queue and put a message on the queue, and you're done.  Likewise, you can connect to the queue and get a message from the queue.

With a publish/subscribe model you've got something that is pushing messages out to one or more subscribers.

There are many more advanced topics that can be built on top of these with queues forwarding to other queues, or selectively accepting messages, or doing a remote procedure call on another system.  The message queue is the underlying framework for a lot of different technologies.

Looking at RabbitMQ (just one of many - there are lots more out there) there is a specific tutorial for a single producer and consumer written in Python (and Java, and Ruby, and Php, and C#) that can be found under their tutorials page.

Pi #2 starts running and connects to the server (who's IP will be static I guess) with a name he chooses. Pi #3 does the same as #2.

Having a static server is indeed the easiest approach.  The server is there and you're connecting to it.

However, if you want to go down the 'its not always a static IP address' because your dhcp server isn't cooperating nicely with that, there are technologies for this too.

Realize that I'm saying this in that I've never used RabbitMQ (ActiveMQ? WebSphere? yes... Rabbit, nope - but I've heard good things about it) nor been more than a user of this technology under other names...

Zeroconf.  This is one implementation of the technologies that are known as zero configuration networking.  The idea is your Pi #1, when it starts up its server would announce to the network "I have this service available!" and everyone else can then use it.  It doesn't matter where Pi #1 is located on the network - its there.

This is how some IP cameras work - you look on your local network and you'll see the camera broadcasting its address and a program that can discover it on the network, can do so.

Specifically for RabbitMQ, I've stumbled across this gist which appears to announce to the network that "hey, there's an MQ server here" and now your Pi #1 can show up anywhere on the network and things will work.  <s>Because they always work the first time you deal with a technology</s> Though it may take some additional understanding and delving into this.  I wouldn't suggest doing this as a first step - get it working with some message queue server working on a static ip address, and then start seeing about making it broadcast from the static IP address, and then let it grab a dynamic address.  Don't go down that particular heh rabbit hole at first.

Title: How to route messages between clients using a central server in Python

Body:
I've got three Raspberry Pis sitting around. I want 2 of them to be able to chat while the 3rd routes the messages (acts as a server between them).

The general flow of events should be something like this:

Server starts running on Pi #1

Pi #2 starts running and connects to the server (who's IP will be static I guess) with a name he chooses. Pi #3 does the same as #2.

Pi #3 can then, knowing the name of Pi #2, send a message to Pi #2 using ... something.

This is the general outline of what I want to achieve.

I'm not sure what the server that runs on Pi #1 should be (I've heard of webserver frameworks like Flask but I don't have enough knowledge to determine if they fit my needs).

I'm also not sure on what I should be using for the client side (Pi #2,3). I could probably use sockets but I assume there is a better / easier way.


问:
Testing for Authentication loop holes / bugs
We've got a web application which is 99% complete prior to public beta, were currenlty securing the site from security perspective, locking down the server, db etc, one thing I'm concerned about but not really sure of the best way to test is for authentication loop holes.

For instance there a 3 user types - System admin, Staff, Client.

There are urls on the site that I would only want the System admin to be able to access, and there are other pages, that I would want everyone to access, but not view all of the data. System admin would be able to see everything, but Staff and Client would be able to see some content, similarly some pages would be read only for some users.

Is there a workflow for testing this, at the moment I've come up with a script that has the credentials of a each user type and goes to every url on the site trying to access pages and reports if it's able to see a page it's not meant to, similarly it checks for read / write data on the page.

Is there anything else I should be checking for to make my authentication tests more robust ?

答:
Sounds like you have the ordinary stuff covered. If you know all the root entry points to the site, a simple spider can easily retrieve the set of pages reached by each of 4 kinds of users (don't forget anonymous), and determine whether the page retrieved has the extra sensitive content.

You don't have to worry too much about mistakes that wrongly block access. Your users will find those for you, and no harm is done. You need something extra for the admin pages and sensitive content that might be accessible when they shouldn't.

At some point you have to switch hats and try to break into your own site. The rules are: you get full access to the source code and a set of user names, but not the passwords. Can you retrieve any sensitive data or do any damage? If there are such vulnerabilities, you won't find them by dumb pounding on the keyboard. You have to think like a hacker.


答:
KISS Principle

Keep it simple, stupid. Clever solutions are great, but often the simplest straight forward solution is best.

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”

Brian Kernighan


问:
Best practices for geolocation
We are writing a flight search engine. We want to pre-fill the departure airport for mobile users with the closest one to their location. To do that, our plan is to

Find a list of airports with their latitude/longitude.

Find the user geolocation using something like HTML5 Geolocation (which asks the user for permission).

Calculate the distance between the user's location and every airport to find the closest one.

Fill the departure form.

Is this a standard way of proceeding? I am a junior programmer and I am not used to this kind of problem. Is there any obstacle I should bear in mind while developing my solution? I have the feeling the algorithm to calculate the distance between one point and 300 locations might get a bit heavy.

答:
I'll assume that 'closest' means that you will present a list of nearby airports and allow the user to pick one. You might need up to (say) 10 airports in the list.

No, the calculation is not terribly complicated. It involves a bit of floating path maths, but not so it should cause any great stress. You can very easily reduce the number of actual calculations by pre-sorting the list and/or filtering on raw lat/lon.

One degree of latitude is obviously around 111 km. [Why? Because the original standard for the meter was 10 million meters = 90 degrees of latitude, through Paris.] One degree of longitude is anywhere from 0 to about the same. [Why? Obviously zero at the poles.]

So a first pass calculating values for locations plus/minus 10 degrees could be a useful heuristic for improved performance, and find anything within 1000 km or so. [In Australia, that wouldn't fill your list of 10.]

The great circle distance can be found here: http://en.wikipedia.org/wiki/Great-circle_distance, but I'm sure you knew that.

Title: Best practices for geolocation