问:
What is the better way of getting different outputs for two very simmilar related workflows
I have a very concrete scenario but I think it would be quite frequent.

I'm trying to come up with a solution that it's "better" by better meaning more scalable, reusable and not smelly.

I'm working with very old crufty legacy code and Trying to improve it one thing at a time.
Right now I have a process in which users can "vote" "things".

The process to determine if a user can vote is a complex logic and it may be subject to change.

Right now I have two functions, one (call it can_vote() ) takes the user and the thing and returns true or false. It's used to know what to display to the user.

The other one is vote() It takes user and thing and and checks again all the logic that can_vote checks but instead of returning false on error throws an error that gets displayed to the user.

The system tries to never get to a state in which the user would be able to use "vote" if the result would be an exception but it still happens.

I know there may be a thousand flaws to the whole system but right now the only thing I'm trying to do is unite the logic of the two functions in one single place in the better way (as defined above).

Ideally I would like to be able to call can_vote in my controllers just getting a true or false and acting acordingly.
can_vote, being a binary question should return true|false.
I could be able to use the logic inside can_vote in places where I want to display information about "false" responses.

I've got multiple ideas about how to solve that but all of them seem... weird:

Possible solutions

a) use can_vote(user,thing,verbose)

verbose beeing true false
if true returns string with the error
if false returns true|false

```<code>public can_vote ( user, thing, verbose ) {
    if( user is not logged ) {
        if( verbose ) {
            throw user is not logged
        }
        return false
    }

    //all other checks
    return true
}
</code>```

cons

returns different types depending on parameters

you get an if else that you carry arround the function

b) use a private function to determine if can vote. Use two helper functions

that would be something like that:

```<code>private can_really_vote ( user, thing ) {
    if( user is not logged ) {
        throw user is not logged
    }

    //all other checks
    return true

}

public can_vote ( user, thing ) {
    try {
        can_vote = this->can_really_vote( user, thing )
    }
    catch {
        //can_really_vote threw an exception
        return false
    }

    return false
}

public vote ( user, thing ) {
    if ( this->can_vote( user, thing ) ) {
        //do the vote thingy
    }
    else {
        // you shouldn't even get here. The user should have gotten an exception from can_really_vote
    }
}
</code>```

c) what php usually does

That would be something like

```<code>public vote () {
    if ( can_vote() ) {
        //carry on with the voting
    }
    else {

        display can_vote_last_error()
    }
}
</code>```

Things like that are usually seen in database access classes and the like.
Really? Keeping an internal trace of all those silly errors like "you are not logged", "the contest is over" and all that stuff just to be able to recall them?
It seems quite complicated.

Of course It can be done better, for example with dedicated exceptions... doesn't seem right dough.

I'm sure I can get more solutions... the problem boils down to wanting to get a true/false result in one case and some text on another for the same logic.

I guess the problem is quite common so there should be some software pattern for this.
Or maybe I'm being unable to abstract from it so I see the obvious elegant solution.

What would you do?

Thanks!

PS I'm almost sure it doesn't matter but I'm working in vanilla PHP 5.3

答:
I would recommend your solution c), but I would change it a bit: I would not display some error from the can_vote function. The user should know long before that if he can vote or not and why. The vote() function only checks if the user can vote as a safeguard, because as you said yourself there should be NO WAY your program ever calls vote() when the user is not allowed to vote. So just throw an exception or log an error.

A few comments on your other solutions:

I consider a) do be very confusing and very risky, because if you accidentally flip the verbose parameter and check the result for true/false you might think everything went fine even though you got an error message. Try debugging that problem - a nightmare!

Solution b) is just bloating of code, because all you do is wrap one function call in a try/catch and make it another function. Also, imagine a new developer in your project wants to know if a user can vote. He finds the two functions can_vote and can_really_vote - which one is he supposed to call? Needlessly confusing and no advantage whatsoever.


问:
What an application should do when there is an error notifying the user of an error?
Say I have an application, and it writes (among other things) errors to a log file. It's the first place the user would be asked to go and look if there was a problem. Let's also assume that this is a critical application that cannot be allowed to just crash.

For the sake of argument we'll also say that all exceptions are being logged as well. I'm in the boat that says this is a bad idea, but we'll run with it for the sake of this fictional problem.

How should the application ideally handle the situation where it is completely unable to write to its log? Say due to a lack of disk space, exhausted file handle count, or a permissions/access issue.

Depending on the exact reason of course it may be able to partially resolve itself. If there was no free disk space for example it might be reasonable for it to trash the current log to write out a line saying its out of disk space, at the expense of losing the log contents. Although this is very unhelpful if something external is eating space and would just result in logs being emptied.

It may be prudent for it to log to an internal buffer, so that once the situation is resolved no data is lost - but left too long this'd simply munch on memory.

So what do you think, how should a fault notifying the user of an error be handled? Especially with an eye to headless/unattended applications that'd normally just monitored by looking at their logs.

答: Replace the last log entry with a new log entry that says the log is full, and silently discard any subsequent log entries.  This preserves the rest of the log, and provides an indicator of what happened to the lost log entries.</p>

ContentLicense: CC BY-SA 3.0


问:
Is TDD the best software development philosophy?
Question:

Why not just use BDD-style acceptance tests and do away with unit tests, integration tests, functional tests and all other tests?

I have been doing some research into the differences between different types of testing as well as different software development philosophies.

Looking at the list of software development philosophies and reading about different types of testing lead me onto a research to find out why we use unit tests, integration tests and functional tests if we have acceptance tests.

I hate to be the one to kill the holy cow but I feel like if you have acceptance tests in a BDD-style environment like given, when, then then you don't really need any other tests since you will be delivering exactly what functionality is required by the business anyway with just acceptance tests.

Some arguments for using unit, integration and functional tests:

Unit tests ensure things at a lower level are correct: Who cares? if the business is happy with what they've received, then what difference does it make?

Unit tests speed up long-term development time: Is it really worth the trade-off?  Where there is code there are bugs, tests are not exempt from this. The time spent writing and debugging 5,000 unit tests vs. the time debugging 50 acceptance tests seems like an obvious choice?

Unit tests/TDD ensures good architecture: No they don't. Each unit test is a design decision and is extremely vulnerable to overall poor architecture.

Integration tests ensure the overall performance is accurate and optimal: Why can't this be just another Then condition in acceptance tests? Why not just use selenium within the acceptance tests?

Functional tests ensure that output is as expected: Acceptance tests enhance this by providing an answer at build time rather than waiting for potentially incorrect results.

TDD ensures that when changes are implemented, nothing else breaks in the process: Acceptance tests will ensure this too since everything's based on business requirements/features.

答:
BDD actually started at the class level. JBehave was originally intended to be a replacement for JUnit.

The only meaningful difference between JBehave and JUnit back in 2004 was the removal of the word "test", and the use of "should" to drive out different aspects of behaviour of the class and encourage questioning of those aspects of behaviour ("should it?").

Conversations between Dan North (who created BDD) and Chris Matts (at the time, an analyst who was learning more about code and particularly mocks) revealed that the same patterns could be used at a system level, and the idea of using "Given, When, Then" to automate reusable steps was born.

Rather than thinking of them as "tests", Dan encouraged developers to think of them as examples of how a class behaved, or how a system behaved. We called the system ones "scenarios" mostly to differentiate from the class-level "examples", but both are synonyms.

The steps were created according to the context in which the class or system worked (the Given) and how those contexts changed the outcome (the Then). This pattern let people question whether the contexts needed to be considered, or whether an outcome was desired or not.

The different levels are appropriate for different audiences. Conversations about class behaviour typically take place between developers, while conversations about system behaviour involve the "three amigos" of tester, developer and business expert.

It's often the case that developers are aware of more requirements than the business, from stakeholders who aren't always present. For instance, devs will normally consider maintainability, performance, the APIs of third-party or legacy systems, themselves coming back to the codebase in 3 years time or new joiners coming on board, etc.

All of these are parts of the requirements which the business often don't care about, as long as it works. Talking through the behaviour at a technical level can give the devs a chance to question whether what they're doing is appropriate.

Some small projects may not see much benefit from this, particularly if the functionality of the project is simple and predictable. Large projects benefit from the "Test Pyramid": few full-stack system tests, more integration tests, and even more class-level tests. See Lisa Crispin and Janet Gregory's "Agile Testing" for more information.

As an example, this is a class-level test which uses "Given, When, Then" in comments to illustrate the waiter's responsibilities and behaviour (C# but readable).

I find it helps if I don't think of them as tests, though. I think of them as examples which illustrate how the system behaves. The idea isn't to catch bugs; it's to prevent bugs from scenarios which haven't been considered from being there in the first place. Any regression bug is usually a symptom of poor design, and benefits from class-level refactoring and testing instead of shoving another system-level scenario into the mix.


问:
Stopping asynchronous systems
I have a system which has 5 services. Each are run in a separate process. They resemble an assembly line, in the sense that they mostly process messages in the same order 1->2->3->4->5, however it's not always the case that the processing is linear. There are cases where it can skip though the chain (e.g. 1->2->5) or go backwards (e.g. 1->2->4->3->1) or even zig-zag (1->2->4->3->5).

The system is fed payloads to process one by one, starting with service 1. Given the system is asynchronous and non-linear, I cannot really know when the whole system processed each of the payloads. On the other hand, I want the system to stop sometimes and the best choice is as soon as it processed all the payloads.

These solutions would not be too good:

A simple kill or using a poison-pill approach would not work (example https://stackoverflow.com/questions/13847963/akka-kill-vs-stop-vs-poison-pill), as then I could possibly end up not processing the payload completely (1->2->1 would fail, as 1 would have been stopped after 2 received the last payload)

I can do a timeout approach, however that would not work for two reasons: a) it will hinder development / testing (e.g. if I waited 1 minute for a 5-sec payload effective processing time), b) timeouts still can be too low, so I could run into issues if some services did not finishe processing

I was thinking about creating another service that would serve as a two-phase commit kind of stopper. The idea would go along these lines:

Any service could call stopper to say "I want the system to stop"

Stopper would call all other services and tell them "Stop has been requested"

Other services will wait till they process what they have in the queue and tell stopper "I can to be stopped"

Stopper will wait till it collects "I can be stopped" from all services, then broadcast "Perform stop"

All services will then go ahead and stop their respective processes in a graceful manner

I have issues with the above:

It looks overly complex for the benefit (compared to "just wait 1 minute then stop" and not adding any non-business code), not sure if worth the trade-off

I would have to infuse logic related to stopping into all services and it would have to be coupled to both the queuing mechanism (so it knows when there are not more payload messages) and business logic (so it knows when the payload itself is done processing)

There are still some edge cases to be considered. E.g. in 1->2->1 scenario, 1 could finish, 2 could be processing the last payload, when it's done it could send a message to 1 and to stopper service, stopper will send "Perform stop" and 1 could receive that message before it received a message from 2 that it should process, so it could stop prematurely

Are there best practices or examples of how to achieve this in some other way? Or maybe constraining the system in some other way that would make this problem disappear / easier to manage?

答:
The last time I had to do this, I designed the steps in the assembly line so that, if any of the steps received a null object from the previous step, that was an indication that there were no more items to process.

Then all you have to do is simply send a stop signal to the object that is feeding the assembly lines.  The object will then send null items to the assembly lines, effectively flushing them and shutting them down.  As each step shuts down, it sends a null object to the following step, shutting it down as well.

So here is the architecture of my last application:

```<code>File Reader --> Producer  --> Queue --> Filter --> --> Output Queue --> Consumer --> File Writer
                          \-> Queue --> Filter --> -/
</code>```

The producer splits the input file packets into channels, represented by the multiple queue-filter combinations (pipelines).  The filter conforms to input and output interfaces, making it swappable.  The entire architecture from producer to consumer (but excluding the file reader and file writer objects) is wrapped in a Controller class.  Each queue/filter combination is contained in a Pipeline class.  The Controller class has a Cancel method, which sends a Cancel signal to the Producer.  The producer, in turn, calls Cancel methods on each of the pipelines. There is a Flush() method on each of the queues, which is called by the pipeline objects.

When Dequeue() is called on a queue, it will do one of three things:

Return a packet from the queue, or

If the queue is empty, block until a packet is enqueued, unless

Flush() has been called on the queue, in which case the Dequeue() method will return a null object if the queue is empty.

So when no more packets are available, the filter will see a null packet.  The entire process is completed when the output queue returns a null object after it has been Flushed.  The controller will not flush the output queue until all pipelines have signalled that they are both Flushed and empty (having received the final null object from the input queue).

Now, if a loop condition exists, where an item returns to some previous stage, what you'll have to do is wrap your packet in another object, so that you can store some state information with the packet, such as how many times it's gone through stage 1, etc.

Title: Stopping asynchronous systems

Body:
The last time I had to do this, I designed the steps in the assembly line so that, if any of the steps received a null object from the previous step, that was an indication that there were no more items to process.

Then all you have to do is simply send a stop signal to the object that is feeding the assembly lines.  The object will then send null items to the assembly lines, effectively flushing them and shutting them down.  As each step shuts down, it sends a null object to the following step, shutting it down as well.

So here is the architecture of my last application:

File Reader --> Producer  --> Queue --> Filter --> --> Output Queue --> Consumer --> File Writer
                          \-> Queue --> Filter --> -/

The producer splits the input file packets into channels, represented by the multiple queue-filter combinations (pipelines).  The filter conforms to input and output interfaces, making it swappable.  The entire architecture from producer to consumer (but excluding the file reader and file writer objects) is wrapped in a Controller class.  Each queue/filter combination is contained in a Pipeline class.  The Controller class has a Cancel method, which sends a Cancel signal to the Producer.  The producer, in turn, calls Cancel methods on each of the pipelines. There is a Flush() method on each of the queues, which is called by the pipeline objects.

When Dequeue() is called on a queue, it will do one of three things:

Return a packet from the queue, or

If the queue is empty, block until a packet is enqueued, unless

Flush() has been called on the queue, in which case the Dequeue() method will return a null object if the queue is empty.

So when no more packets are available, the filter will see a null packet.  The entire process is completed when the output queue returns a null object after it has been Flushed.  The controller will not flush the output queue until all pipelines have signalled that they are both Flushed and empty (having received the final null object from the input queue).

Now, if a loop condition exists, where an item returns to some previous stage, what you'll have to do is wrap your packet in another object, so that you can store some state information with the packet, such as how many times it's gone through stage 1, etc.


问:
Is it reasonable to assume that any physical quantity can be represented by a 64-bit integer without overflow or underflow?
The original binary search algorithm in the JDK used 32-bit integers and had an overflow bug if (low + high) > INT_MAX (http://googleresearch.blogspot.com/2006/06/extra-extra-read-all-about-it-nearly.html).

If we rewrote the same binary search algorithm using (signed) 64-bit integers, can we assume that low + high will never exceed INT64_MAX because it's physically impossible to have 10^18 bytes of memory?

When using (signed) 64-bit integers to represent physical quantities, is it reasonable to assume underflow and overflow can't happen?

答:
Is it reasonable to assume that any physical quantity can be represented by a 64-bit integer without overflow or underflow?

Not exactly.  There are plenty of numbers that are both bigger and smaller than that, which is why we have floating point numbers.  Floating point numbers trade off lesser precision for better range.

In the specific example that you cited, it is highly unlikely that you would ever need a number that's larger than that.  64 bits corresponds to roughly 18 quintillion elements.  But never say never.


答:
The short answer is no.  However, for some applications your assumption might be correct.

Assuming a signed int, 2^63, with commas added for some clarity, = 9,223,372,036,854,775,808.  So it's roughly 9 * 10^18.  10^18 is an "Exa".

Wikipedia says "As of 2013, the World Wide Web is estimated to have reached 4 zettabytes.[12]", which is 4000 Exabytes.  Therefore, the WWW is roughly 400 times larger than 2^63 bytes.

Therefore, there is at least one physical quantity that is much larger than a signed (or unsigned) 64 bit integer.  Assuming that your units are bytes.  If your units were something much larger, like GigaBytes, then you'd be o.k., but your precision of measurement would be low.

For another example, consider far away galaxies.  The Andromeda Galaxy is actually one of the close ones, and it is 2.5 * 10^6 light years away.  If your units were miles, that would be 14.5 * 10^18, more than a 64 bit signed integer.  Now, obviously it depends on the units you use for your measurements, but some galaxies are way further away than Andromeda. (The furthest known one is 13 * 10^9 L.Y. away.)  Depending on the precision you want for your measurement, it could overflow a 64 bit integer.

(Added)  Yes, miles are a lousy unit for astronomical distance.  A more normal unit might be an Astronomical Unit, roughly 93 million miles.  Using that unit of measurement, the furthest known galaxy is roughly 10^15 A.U. (if my math is right), which would fit into a 64 bit int.  However, if you wanted to also measure the distance to the Moon, to nearby orbiting satellites, that unit is too large.

One more example from electronics: the Farad (F), a unit of capacitance.  Large capacitors range up to 5kF.  And this number will likely increase over time as Hybrid cars, "smart grids", etc. improve.  Once can measure capacitance as small as 10^-18 F.  So the overall range in "real" capacitance that we can measure today is 5*10^21, larger than a 64 bit integer.

Title: Is it reasonable to assume that any physical quantity can be represented by a 64-bit integer without overflow or underflow?

Body:
The original binary search algorithm in the JDK used 32-bit integers and had an overflow bug if (low + high) > INT_MAX (http://googleresearch.blogspot.com/2006/06/extra-extra-read-all-about-it-nearly.html).

If we rewrote the same binary search algorithm using (signed) 64-bit integers, can we assume that low + high will never exceed INT64_MAX because it's physically impossible to have 10^18 bytes of memory?

When using (signed) 64-bit integers to represent physical quantities, is it reasonable to assume underflow and overflow can't happen?


问:
Software development costs and influences
I had a conversation with a friend who is a civil engineer the other day regarding (lack of) planning in software projects. I said that before the construction phase of a software development project, before developers sit down in front of a computer to actually write code, a serious and thorough planning phase should take place (requirements, design). Bad software development project end up doing construction twice or more (due to lack of planning). As an analogy I asked what would have happened if a skyscraper project would have carry out the construction phase twice, in a goal to show how bad, costly and not acceptable this is, just as (it should be) in software, or in any other field for that matter.

Although agreeing with me, he replied that software is "engineering without foundation" (as oppose to construction projects), that software construction costs are low/cheap relatively to "classic" engineering projects (meaning producing a "real" physical product, or in this case, a building), and that in software development there aren't many influenced factors, like in other fields, especially like in construction (regulations, environment, landscape to name a few).

This left me thinking:

1. What are the costs figures of a big software construction project? Any published large scale well known projects documentation I can get numbers from?

Edit: I'm  mainly looking for information about total project costs of past software development processes, known projects/software preferred.

答:
What are the costs figures of a big software construction project? Any published large scale well known projects documentation I can get numbers from?

There are a large number of projects that have various levels of publicly available information regarding project costs.  The documentation is commonly available in the form of case studies.   The project management institute has a library of case studies and many other similar documents are available via a web search for say "case studies in IT projects".

What are a software project influences (besides the goal of the project itself)?

This is a broad question but focussing on cost the two largest impacts for cost over runs in my experience are:

Poor control over the scope of the project and,

Over optimistic self assessment of team members/sub contractors ability to estimate effort.

Good scope control is about establishing an agreed statement of work and, importantly, having a disciplined method for adjusting time tables and cost estimates when the client requests changes to the statement of work.  The causes for the change requests are normally valid and will always carry a time and money cost.

Over optimistic self assessment is usually mitigated by utilising a PERT analysis where the cost estimate is based on the weighted average of three separate estimates being the optimistic, realistic and pessimistic.

With that there are other influences on the costs depending on the scale of the project which each have well established mitigation methods.  Some of these include, in no particular order:

inflation

foreign exchange rates

company failures (Arthur Anderson as example)

reorganisations within the client company

loss of key personal (resignation, reassignment, illness, death)

low skilled team members

ineffective cost control mechanisms

poor product development techniques

fraud


问:
Is it wise for a high level developer to spend time studying assembly?
It is clear that the knowledge of low level stuff is very important in our work.

But in a situation where you're already developing commercial software on a high level, and when you already have a chosen direction but don't have any assembly skill, isn't it more reasonable to focus on studying stuff related to your direction? Or is there a reason you should spend some time to learn the low-level basics anyway?

When is it too late, and when it is not? And if it is not too late, then how would one go about learning optimally (in the sense of not spending excessive time to get some depth and understanding)?

答: I think you reach a point of diminishing returns as your career advances.  That is, you should learn assembly language early, as it leads to a better fundamental understanding of everything that comes after.  However, if you're already in the work force and have a few years of experience, you'll already have a feel for the way things work, so learning the actual nuts-and-bolts details may not provide you with as much insight.</p>

ContentLicense: CC BY-SA 2.5


问:
When to use a SortedMap interface?
The rule in programming is to use the most general interface possible. But the difference between Map and SortedMap is more subtle.

They compile to the same bytecode [I think]

No additional safety is provided since the compiler can't tell any of the differences between the two

SortedMap does offer an extra non-compiler-checked post condition, namely that the iterator is sorted

I would prefer code to write

```<code>SortedMap<K, V> m = new TreeMap<>();
</code>```

if the sorted-ness is used. But I'm not sure if this rule only makes sense when there's compiler checks.

I'm hoping for a somewhat rigorous (e.g. type-theoretical) answer, but since this pertains to a real code review I'm doing right now, how this applies in practice is definitely important. I've yet to find an effective example of why one matters, besides a readability bump that circularly depends on programmers following this theory.

答:
You use it when you want to guarantee that you are getting sorted data in a parameter, or when you want to assure the consumer of your method that you are providing sorted data.

Parameters

If you need your data to be sorted, ask for it to be sorted by asking for the interface that matches what you need. It is likely less expensive for the method sending you the data to give it to you sorted (they get it from a database and can trivially order it, but instead tossed it in a HashMap because thats what everyone uses) than it is for you to take the Map and then add it to a SortedMap.

If you need it to be sorted, ask for it to be sorted.

Return values

This one is a bit more controversial... there are differing schools of thought here.

If you are producing sorted data by the nature of your method and don't have a reason not to, return back the interface that lets the consumers of your data work with it in the most ways without giving them a concrete implementation (that locks you down).

That 'that locks you down' is part of the controversial bit.  Some people will argue that you should return the least constraining (most general) interface when possible (Map rather than SortedMap and Collection rather than Set or List).

I'd go with return the most useful thing for the consumers.

I will put the caveat here that if this is something that will always be sorted, its one thing... if someone might want to subclass it later and not have it be sorted... thats another story.  So you need to consider this as part of designing for extension.  If it isn't going to be extended ever you get one answer from this deliberation... if it may be extended, there's another possible answer.

why is this bit above a mess and people say things about it in code review?

Well, Liskov substitution principle points out that this would make it harder to subclass later.  Method parameters cannot be strengthened by a subtype and return values cannot be relaxed.

By returning a SortedMap one cannot make a later subclass of the class return a weaker type.  Furthermore, if you decide to return a weaker type later for some reason it makes it a major refactoring (changing all of the SortedMaps out there to Maps and possibly changing the calls that make use of that interface into code that has to deal with the weaker type).

Your internals

It really doesn't matter what you are doing inside your own code.  Use a sorted map whenever you've got sorted data and want it in a sorted order. Especially if you are doing put calls on it at various times and want it to remain in sorted order.

Now, you might be tempted to use a LinkedHashMap instead if you get all the sorted data in one go, insert it in sorted order, and then only do remove calls against it if you do any modifications.  It's not wrong, and it has some advantages (O(1) lookups rather than O(log n) lookups).  But, its rather inflexible only providing the get and put and none of the other fun method calls that SortedMap can give you.

Btw, NavigableMap

Actually, SortedMap doesn't give you that much more over Map.  But there is something else...

SortedMap has been replaced by NavigableMap since Java 1.6 (the two standard classes that implement SortedMap also implement NavigableMap).  NavigableMap has all sorts of fun method calls that are often quite useful.

Title: When to use a SortedMap interface?


问:
Are deadlines Agile?
For clarity, a deadline is:

A time limit or deadline is a narrow field of time, or particular point in time, by which an objective or task must be accomplished.

From wikipedia

My whole software development career I have been doing "Agile" which everywhere has seemed to mean at least the following practices were adhered to:

Weekly or Bi-Weekly sprints

Retrospectives Sprint planning

A product owner

Scrum

User Stories

However, every project I have ever been on has insisted on setting a deadline. Given that Agile attempts to focus on adaptive planning, flexibility and change; are deadlines Agile?

My own opinion is that they are not, as I see deadlines leading to a lack of flexibility and lack of quality. Instead, I think it provides more value to focus on Sprints and early deliveries. It seems however, in every circle I have been in, this is not the case, and deadlines are viewed to go hand in hand with Agile development.

答:
Arbitrary deadlines that have no consequences if missed aren't very agile, but there are situations where for reasons outside of the development team's control deadlines must be set and kept. Fortunately, if the deadlines are reasonable there are plenty of ways to invert the equation and handle deadlines in an Agile way.

Deadlines aren't always wrong. As we all learned from Obi-Wan:

"Only the Sith deal in absolutes"

It's fair to say that in most cases deadlines are silly, unnecessary, and certainly not Agile, but it would take a fool to say that this is always the case. The architypal case is software needed for a time-sensitive use, such as election tracking software. If the software isn't ready in time for the election, it wouldn't make sensible nor practical to postpone the election, and it doesn't seem to be very customer oriented to just say 'Sorry, looks like we took too long. I know this software you're paying us for is completely worthless, but deadlines aren't Agile so how can you really hold it against us for not meeting them?'

It's not very Agile to tell your customers to shove it for wanting something that is time-sensitive, and at the end of the day somebody is going to have to build these things. So how can we still make the customers happy and still deliver seemingly reasonable solutions to things which are time sensitive? Well, if developing software takes an uncertain amount of time, and the deadline isn't variable, something else will just have to be variable to handle that uncertainty...

If the delivery date is held constant, some other aspect becomes a variable. As we all know, there can be a wide amount of uncertainty in software projects. Something has to be made variable in response to this uncertainty if you want success in your project, and usually that is the delivery date. It seems natural enough, anyways. But that isn't your only option. If you're stuck committing to a hard deadline, the way to handle that is to make your delivered features variable. Prioritize a list of features, communicate clearly that there is uncertainty in the amount of features that can be delivered by that date. Work with the customer to prioritize these features so that the most important ones will have a higher likelihood of being shipped. Then, it's business as usual, only when the deadline rolls around you ship whatever is ready to be shipped. In this model, shipping more features is the analog to shipping at an earlier date, and shipping fewer features is the analog of shipping at a later date.

Title: Are deadlines Agile?


问:
Exception versus return code in DAO pattern
After reading a lot about the abusive use of exceptions in Java and how you should let an exception bubble up through the different layers of an application, I've come to a point where I don't know what I am supposed to do with the potential errors my application can have.

Basically, I have a webservice which uses the DAO pattern to access data in my database. All of the database actions can throw a SQLException.
As of today, I'm using a try catch to catch the SQLException and then thow a specific defined exception called ExceptionDAO that will be handle by the webservice to return a correct message to the users (a mobile application) of my webservice.

After reading a lot about how exception should be exceptional and should not be used in control flow, I've come up with a mixed understanding of what I should do to handle any errors:

Use return codes for anything that is likely to happen (e.g. username already exists) and therefore, to comply with the DAO pattern, pass my business objects as parameters instead. I could also use a specific pair which would return the code + the business object instead. The webservice would then use the return code to display a specific message.

Use checked exceptions for anything that I can't predict will happen and let them bubble up to the webservice to handle and return a message to the users. (e.g. SQLException that I can't predict : connection aborted)

Let unchecked exceptions bubble up aswell and display a sort of 404 error in this case.

I also had a look at the null pattern but I don't think it suits this particular situation really well.
I'm also concerned to not give too much information to the users, but rather useful and straight to the point information. Indeed, the messages returned by the webservice will be used by a mobile application to then display a message to the end-user.

I hope that I was clear enough about the problem I'm having, and I'm looking forward to your answers !

N.B. : This a repost of a subject I posted on stackoverflow, which relates to programming and not a language specific problem.

答:
On the conventional wisdom that states that you should only throw an exception when a condition occurs that you can't handle in your code we tend to focus on the definition of an exceptional condition but ignore the definition of what it means to handle the exception. A process may encounter a exception communicating with a particular node and may handle the exception by redirecting its request to a backup node instead. However, if a call to create an entry in a table fails because of a unique constraint as a result of a value entered by a user the only entity that can truly handle the condition is the user. Therefore this is grounds for throwing an exception as you cannot handle it in your code.

The semantics of your code also matters. Sticking with the example of creating a new user entry; a call to isAvailableUserName(userName) should not throw an exception if the userName already exists in the database whereas a call to createNewUser(userName) probably should. One reason to throw an exception instead of using a return code is that return codes in themselves provide inadequate information and require a lookup table or an enum that then becomes a dependency magnet. Exceptions on the other hand are more descriptive and contain a message, a backtrace, nested exceptions, etc. Exceptions also make your code cleaner. Instead of having lots of nested ifs that exist solely to test conditions, your code can function the way its supposed to and halt execution when a bad thing happens. Robert C. Martin in his book "Clean Code" states that error handling should not obscure the logic of the code. The cleanliness of the code also applies to the caller as return codes force the callers of the code to have to check for errors immediately after the call. Since this can easily be forgotten or ignored, its not a good approach. However, an exception cannot be ignored but must be handled.


答:
Well, depending on who you believe, Healthcare.gov cost between $70 million and...let's call it an even $100 quintillion-Simoleans (I think we can all agree that it's definitely somewhere in there.)

In older news, IRS modernization projects cost somewhere from "hundreds of millions" to $4 billion. I haven't built very many skyscrapers personally, of course, but that sounds like a pretty large amount of money to me.

Personally, from talking to executives of some local insurance companies I'm told a "mid-sized" legacy modernization project can cost $40-50 million over a number of years, and that's just for their own company to use. These systems sometimes replace - I kid you not - scrolls. (I'm serious - some old insurance documents are on climate-controlled rolls of paper locked in tubes in the basement because they are updated infrequently but still active for that many years.)

A mid-sized company can easily have a development staff numbering in the thousands, and all are expected to work on inter-linking pieces of software - with relatively few working truly in isolation.

What happens when you screw stuff up?

Well, how about killing people? Is killing people bad? (Therac-25 killed people with radiation overdoses in the 1980's). Knight Capital lost approximately $440 million in 45 minutes from a software problem - can you buy a sky-scraper for that much? Well, maybe a little one...

Rockets have blown up in mid-air, like the Ariane 5. Edward Snowden's NSA leaks were made possible by problems in the implementation of security systems. Heck, the whole system of global surveillance that has made so many headlines (and probably not enough of them) is, in fact, probably a far larger software effort than any of these previously mentioned projects. Whether or not it counts as a screw-up shall be left to the opinion of the reader.

But honestly, getting into a skyscraper-measuring contest is rarely a productive way to spend one's time. The fact remains that some software projects are big and need planning - sometimes more or less than was actually done - and other projects? Well, they aren't so big, and so it isn't always necessary to have a big waterfall-style requirements/planning phase.

As always, it's not the size of the budget that counts...

Title: Software development costs and influences

Body:
I had a conversation with a friend who is a civil engineer the other day regarding (lack of) planning in software projects. I said that before the construction phase of a software development project, before developers sit down in front of a computer to actually write code, a serious and thorough planning phase should take place (requirements, design). Bad software development project end up doing construction twice or more (due to lack of planning). As an analogy I asked what would have happened if a skyscraper project would have carry out the construction phase twice, in a goal to show how bad, costly and not acceptable this is, just as (it should be) in software, or in any other field for that matter.

Although agreeing with me, he replied that software is "engineering without foundation" (as oppose to construction projects), that software construction costs are low/cheap relatively to "classic" engineering projects (meaning producing a "real" physical product, or in this case, a building), and that in software development there aren't many influenced factors, like in other fields, especially like in construction (regulations, environment, landscape to name a few).

This left me thinking:

1. What are the costs figures of a big software construction project? Any published large scale well known projects documentation I can get numbers from?

Edit: I'm  mainly looking for information about total project costs of past software development processes, known projects/software preferred.


答:
One of the biggest influences in agile software design is actually the reason why software cost estimates are so hard to come by: reusable software.

The tricky thing about reusable software is that it tends to fit in an uncomfortable middle ground between "packaged, ready to go solutions" and "build from scratch."  It is not always obvious how to determine whether software can be reused or not.  In fact, in many cases I have found that it is cheaper to actually go out and build the product than it is to get a reasonable error bound on how long it would take!

It would be ideal for us to build up a decent repertoire of packaged solutions, but the software world moves so mind numbingly fast compared to other worlds (such as civil engineering).  Over time, we have found that the cost of packaging up a solution properly rarely pays for itself because the package becomes obsolete too fast.

So this leads to the interesting difference between civil and software engineering: software engineering's influences are positive, while civil's appear to be negative.  We rarely have a large landscape of constraints to work with.  We don't have as many regulations or environmental limitations to consider.  However, we have a mindblowing number of positive influences to consider.  If a project fits well with a particular partially-packaged block of goodness from a previous project, I can cut serious time off.  If it doesn't fit, I have to build it from scratch.

I have actually given this estimate to my boss once: "the job will take somewhere between 1.5 days and 18 man-months."  He was not pleased, needless to say, but when I ran down my logic of why the variability was there, he was forced to agree.  (The solution, by the way, was that we went agile: we spent the 1.5 days to try to do it the easy way.  If we failed, we'd come back and discuss if it was worth our dollars to do it the hard way.  In the end, the easy way worked).

There is a lot of opinion in the agile world that planning actually works against you.  Requirements work both ways.  It tells the contractor what to produce, but it also gives them a powerful piece of paper to say "this is all you told me to do, so I did it."  This double edged sword often forces customers to write requirements that prevent the 1.5 day solution.  Even government contracts are transitioning away from big-up-front requirements planning because the benefits outweigh the rewards.


答:
The most relevant physical quantity for your question is computer RAM.

Windows Server 2012 supports up to 4 TB of physical memory. That's 242 bytes. If RAM capacities continue doubling about every year, then in only 17 years from now "Windows Server 2032" will support 262 bytes of physical memory, at which time your low + high will reach 263 - 2 and kiss the max signed 64-bit integer.

I hope no safety-critical systems fail, having assumed 64 bits will always be enough.

For a slightly more general use, the most relevant physical quantity is memory address space. (It's useful to have much a larger address space than physical memory, e.g. to put many stacks in memory, all with room to grow.) Current x86-64 implementations support 48 bit virtual addresses, so we have only 14 years before these CPUs reach the 262 byte address-space limit.

And then there's distributed shared memory "where the (physically separate) memories can be addressed as one (logically shared) address space".


答:
In addition to Jerry101's answer, I would like to offer this very simple and practical test for correctness:

Suppose you allocate some memory via malloc, in an 64-bit OS. Let's suppose the memory allocator decides to return to you a valid memory block, of the size you requested, but where the 63-th bit is set.

In other words, let's suppose there exists some programming environements where 0xFFFFFFFFxxxxxxxx are legitimate memory ranges that might be returned from a call to malloc.

The question is, will your code still work as intended?

When the analogous situation occurs to 32-bit operating systems, some software did not operate correctly if they are given memory addresses "in the upper half". Originally, such memory addresses were thought to be only available to the privileged code (operating systems, device drivers, and peripheral hardware), but because of the 32-bit address space crunch, OS vendors decided to make part of that reserved space available to applications that ask for it.

Fortunately, this situation is quite unlikely to happen for 64-bit programs for a while, at least not in a decade.

When that situation finally happens, it means that 128-bit addressable processors and operating systems would have become mainstream by that time, and that they would be able to provide a "64-bit emulation environment" to allow those "legacy applications" to operate under assumptions similar to today's 64-bit operating systems.

Finally, note that this discussion only focuses on memory addresses. A similar issue with timestamps must be taken with more precaution, because certain timestamp formats allocate a lot of bits of precision to the microseconds, and therefore leaves fewer bits available to represent time in the future. These issues are summarized in the Wikipedia article on Year 2038 problem.


答:
Your assumption won't handle physical quantities that can only be represented by floating point numbers.  And even if you decided to scale all numbers, say by multiplying all numbers by 10000 (so the values are still integers but can be represented in ten-thousandths), this scheme still fails for numbers very close to zero, for example the electron mass (9.1094 * 10⎻³¹ kg).

That is a very real (and extremely small) physical quantity, here are some more you're going to have trouble with.  And if you argue that's not a real physical quantity (even though it is in kg), consider:

```<code>10 kg (obviously physical quantity)
1 kg (same)
10⎻² kg (1/100 kg, or about 1/3 ounce) (also quite real)
</code>```

So you see where I'm going with this.  The last one you can't handle either.

Of course, you could have a special field within the number to scale an integer portion up or down by a variable multiplier; gee now you just invented floating point.

Body:
Your assumption won't handle physical quantities that can only be represented by floating point numbers.  And even if you decided to scale all numbers, say by multiplying all numbers by 10000 (so the values are still integers but can be represented in ten-thousandths), this scheme still fails for numbers very close to zero, for example the electron mass (9.1094 * 10⎻³¹ kg).

That is a very real (and extremely small) physical quantity, here are some more you're going to have trouble with.  And if you argue that's not a real physical quantity (even though it is in kg), consider:

10 kg (obviously physical quantity)
1 kg (same)
10⎻² kg (1/100 kg, or about 1/3 ounce) (also quite real)

So you see where I'm going with this.  The last one you can't handle either.

Of course, you could have a special field within the number to scale an integer portion up or down by a variable multiplier; gee now you just invented floating point.


问:
How to respond to a rude bug report?
I just received a quite rude bug report. The user basically says that we're doing it all wrong using capital letters here and there, although he's in fact just pointing at one bug.

On one hand, I care a lot about our users and want to maintain a good relationship and a good rating of our app. On the other I'd feel like a complete sell out if I would reply overly polite.

What's a decent way of responding? What should I keep in mind? What mindset should I have?

It should be added that the user seems to be a 24 year old CS student, and our product is an Android app that we're giving away free of charge.

答:
The Fog Creek blog suggests LATTE (for dealing with angry people, but I think it's also applicable here):

Listen: more of a phone step, they suggest prompting the client with the last thing the client said

Acknowledge: make the person feel heard

Tell: people want to see that they are in a process, they want to see evidence of movement

Take Action: "I’m going to do something about that"

Explain: your immediate first response will be to explain, it is the worse possible thing that you can do (i.e. leave this till last)

my own experience can verify this list (and the acronym reminds you to get your Starbucks on)

Title: How to respond to a rude bug report?


问:
Confused about ASP.NET Ajax, jQuery and JavaScript
Yesterday, I read couple of chapters on ASP.NET Ajax and jQuery from my ASP.NET 4 book and I found those frameworks pretty interesting and decide to learn more about them.

Today, I borrowed some books from library on Ajax and JavaScript. It seems ASP.NET Ajax is different from Ajax and jQuery seems like the "new" JavaScript.

Does it mean that I can skip JavaScript and learn jQuery directly?

On the other hand, the non-ASP.NET Ajax book I borrowed seems to apply to the client side web programming only and looks quite different from what I learned from ASP.NET Ajax. If I'm an ASP.NET developer, I guess I should stick with ASP.NET Ajax instead of client side Ajax right? What about PHP? Is there a "PHP Ajax" similar to ASP.NET Ajax?

It's not that I'm lazy to learn other tools, but I just want to focus on the right ones.

答:
Here's the deal in a nutshell

(1) JQuery does not replace Javascript, It is a library that simplifies common JavaScript tasks. Any JQuery code is actually written in JavaScript.

(2) AJAX is just JavaScript code that manipulates the web page client-side and often pulls content down from the server without forcing a reload of the whole page. There are not multiple "Versions" of AJAX.

(3) Notwithsdanding #2 above. Both ASP.NET and JQuery have libraries or tools that simplify the task of creating AJAX driven websites. There are also a number of other tools that do this too. It is all the same AJAX, just different ways of generating it, or libraries encapsulating it.

(4) Which library should you learn? That's mostly a preference thing. If you like using Web controls a lot and the drag-drop web development with ASP.NET you might prefer just using the ASP.NET controls to do the same. I also am an ASP.NET developer, but I prefer using JQuery because it often gives me more fine grained control, even if it can be slightly more work sometimes. Also, if you learn the JQuery way, you can use it on any platform including PHP.


问:
PHP extended class method requires same signature including object class requirement?
Not sure exactly how to phrase the question succinctly for the title.

I have a collection class that extends another collection class.

The parent collection-class has a method addMember(someClass $obj) that adds an object to the collection.

The child collection-class groups objects of the child class of someclass, someClassChild. I thought that a child class's method's signature would pass muster as long as the signature was the same or required children of the classes the parent required.

E.g. addMember(someClassChild $obj)

But I tried it and I'm getting a warning about strict standards.

So then, how to I implement a collection class as a child of another collection class to provide functionality for parent/child base objects?

答:
It's a violation of Liskov's substitution principle.
Basically any class that uses SomeClass must be able to rely on the fact that addMember accepts a SomeClass or any of its subclasses.
In your example, that's no longer true, because now you have an implementation of SomeClass that will cause a fatal error whenever a SomeClass object is passed that is not a SomeClassChild.

Some languages allow contravariance of method arguments, meaning that a subclass may accept a more general type (rather than more specific like in your example), which makes sense, because any SomeClass is also a SomeSuperClass if SomeClass extends SomeSuperClass. PHP however doesn't support this.


问:
Laravel relationships and database schema
I am playing around with Laravel and came across something in the documentation for defining 1 to 1 relationships between tables that I would like to get feedback on.

The example in the docs is between a user table phone table, where each user can have one phone entry.

Here is the Laravel example:

```<code>class User extends Eloquent {
    public function phone()
    {
        return $this->hasOne('Phone');
    }

}
</code>```

If you get a single user with the ORM, the following query is run:

```<code>select * from users where id = 1
</code>```

And if you try access the phone number in the result, the framework will see the relationship and run

```<code>select * from phones where user_id = 1
</code>```

That's where my question comes up. I would have had a phone_id column in  the user table schema, and the query to get the phone number would be:

```<code>select * from phones where id = [user.phone_id]
</code>```

Thinking about it while typing the post, the 'Laravel' way seems to have the benefit of being able to become a 1 to many relationship, which got me wondering if I've been doing it wrong all this time?
How do you normally structure your schema for 1 to 1 and 1 to many relationships?

答:
Since you didn't follow the convention of Eloquent you have to type it manually and
it's easy just specify the second and third arguments into the hasOne or other functions of ORM obj.

i.e:

```<code>class User extends Eloquent {
   public function phone()
   {
      return $this->hasOne('Phone','id','phone_id');
   }

}
</code>```

Where the second argument is the column name of the first argument (the table you want to make relation with, in this case Phone) and the third argument is the column name in this model User

Body:
Since you didn't follow the convention of Eloquent you have to type it manually and
it's easy just specify the second and third arguments into the hasOne or other functions of ORM obj.

i.e:

class User extends Eloquent {
   public function phone()
   {
      return $this->hasOne('Phone','id','phone_id');
   }

}

Where the second argument is the column name of the first argument (the table you want to make relation with, in this case Phone) and the third argument is the column name in this model User


答:
It is unreasonable to assume a 64 bit integer can hold all numbers. Multiple reasons:

The max and min 64 bit integer are finite numbers. For every finite number a larger and smaller finite number exists.

Calculations with 128 bit and 256 bit numbers are currently used in various places. Many processors have specific instructions that operate on 128 bit integers.

20 years ago, a 1 GB disk was considered "large". Today a 1 TB disk is considered small. 20 years ago average desktops had about 16 MB RAM. My current desktop has more than 16 GB RAM. Harddisk space and RAM has grown exponentially in the past, and is predicted to grow exponentially in the future. Unless somebody can come up with a very good reason why it should stop growing, it doesn't make sense to assume it will stop.


问:
How to store sensitive information (e.g. clients' credentials) accessibly for everybody inside?
I just discovered that we store sensitive information on our SharePoint. Since it's accessible by a password alone (and we all know that others click for the password/user name to be automatically pre-filled, even at an outside computer), I feel that we're not managing it securely enough.

The problem is that before I can make some waves regarding it, I need to suggest a better solution. This far, I suggested that we e-mail it to each other (since that requires at the very least login to Exchange, something that needs to be provided at each synchronization).

It's faaar form perfect, of course.

I've googled for the issue and I get a lot of general hints and generic suggestions. I'd like to add to them the info on actual software that can be used or a pattern that's practically declared.

We're sitting on SharePoint, Windows 7/8, Exchange Server and use windows authentication via AD when accessing our computers.

Often, we're working at the office (65%) or at home (25%) but some portion is done in a coffee shop or such (10%). The information to be stored is just a text file (or a set of text files) and needs not necessarily to be compartmentalized inside our organization (we trust all the employees). The information needs to be centrally available (as it can change on occasion) but there's no need to encrypt it.

Thoughts?

答:
Store it in Excel or similar, and encrypt the document so the user has to download and then enter a different password to view it. Not only does this get around your problem of stored browser passwords, it also reinforces the user's attitude to the information as it requires some effort to access - ie. helping to remind them that it is sensitive and requires a little more care.

Of course, you now have a problem of having a sensitive excel document sitting on people's laptops...


答:
If you have a circular queue of services (eg 1 -> 2 -> 1) so you cannot send any form of "stop" message, then you will have to stop the entry to the entire system and wait for them all to complete processing.

Only when they are all quiet will it be safe to turn them all off, so you'll need a monitor to see every service's processing status. Alternatively it might be possible to count entries and exits to the system if every message sent in comes out. Then you can stop entry to the system and the last process in the chain (a special one that is always placed last and performs little processing, maybe just reporting) is the one that can tell when the system has completed all processing - if that is appropriate for your system.

Title: Stopping asynchronous systems

Body:
I have a system which has 5 services. Each are run in a separate process. They resemble an assembly line, in the sense that they mostly process messages in the same order 1->2->3->4->5, however it's not always the case that the processing is linear. There are cases where it can skip though the chain (e.g. 1->2->5) or go backwards (e.g. 1->2->4->3->1) or even zig-zag (1->2->4->3->5).

The system is fed payloads to process one by one, starting with service 1. Given the system is asynchronous and non-linear, I cannot really know when the whole system processed each of the payloads. On the other hand, I want the system to stop sometimes and the best choice is as soon as it processed all the payloads.

These solutions would not be too good:

A simple kill or using a poison-pill approach would not work (example https://stackoverflow.com/questions/13847963/akka-kill-vs-stop-vs-poison-pill), as then I could possibly end up not processing the payload completely (1->2->1 would fail, as 1 would have been stopped after 2 received the last payload)

I can do a timeout approach, however that would not work for two reasons: a) it will hinder development / testing (e.g. if I waited 1 minute for a 5-sec payload effective processing time), b) timeouts still can be too low, so I could run into issues if some services did not finishe processing

I was thinking about creating another service that would serve as a two-phase commit kind of stopper. The idea would go along these lines:

Any service could call stopper to say "I want the system to stop"

Stopper would call all other services and tell them "Stop has been requested"

Other services will wait till they process what they have in the queue and tell stopper "I can to be stopped"

Stopper will wait till it collects "I can be stopped" from all services, then broadcast "Perform stop"

All services will then go ahead and stop their respective processes in a graceful manner

I have issues with the above:

It looks overly complex for the benefit (compared to "just wait 1 minute then stop" and not adding any non-business code), not sure if worth the trade-off

I would have to infuse logic related to stopping into all services and it would have to be coupled to both the queuing mechanism (so it knows when there are not more payload messages) and business logic (so it knows when the payload itself is done processing)

There are still some edge cases to be considered. E.g. in 1->2->1 scenario, 1 could finish, 2 could be processing the last payload, when it's done it could send a message to 1 and to stopper service, stopper will send "Perform stop" and 1 could receive that message before it received a message from 2 that it should process, so it could stop prematurely

Are there best practices or examples of how to achieve this in some other way? Or maybe constraining the system in some other way that would make this problem disappear / easier to manage?


答:
You say that you are giving your app away free of charge. From personal experience, I'd be annoyed if an app is worth a lot less than I paid for it. The conclusion is that the person complaining thinks your app is worth a lot less than nothing.

What do you think caused that person to write a very rude bug report? Do you think it is because that person is naturally rude, or is there a possibility that a bug in your app caused them so much aggravation that they were absolutely annoyed? For example, a free app providing a bus time table that gives the wrong times and I miss an important appointment because of that bug would be very, very annoying.

If your app has bugs that bring out the worst in people, then maybe you have a bigger problem than rude bug reporters. If your free app can really annoy people, then it can create a negative backslash against your company, so you better figure out what is going wrong there.

Title: How to respond to a rude bug report?

Body:
I just received a quite rude bug report. The user basically says that we're doing it all wrong using capital letters here and there, although he's in fact just pointing at one bug.

On one hand, I care a lot about our users and want to maintain a good relationship and a good rating of our app. On the other I'd feel like a complete sell out if I would reply overly polite.

What's a decent way of responding? What should I keep in mind? What mindset should I have?

It should be added that the user seems to be a 24 year old CS student, and our product is an Android app that we're giving away free of charge.


问:
What methods are there to avoid a stack overflow in a recursive algorithm?
Question

What are the possible ways to solve a stack overflow caused by an recursive algorithm?

Example

I'm trying to solve Project Euler problem 14 and decided to try it with a recursive algorithm. However, the program stops with a java.lang.StackOverflowError. Understandably. The algorithm indeed overflowed the stack because I tried to generate a Collatz sequence for a very large number.

Solutions

So I was wondering: what standard ways are there to solve a stack overflow assuming your recursive algorithm was written correctly and would always end up  overflowing the stack? Two concepts that came to mind were:

tail recursion

iteration

Are ideas (1) and (2) correct? Are there other options?

Edit

It would help to see some code, preferably in Java, C#, Groovy or Scala.

Perhaps don't use the Project Euler problem mentioned above so it won't get spoiled for others, but take some other algorithm. Factorial maybe, or something similar.

答: Another solution would be to simulate your own stack and not rely on the implementation of the compiler + runtime. This is not a simple solution nor a fast one but theoretically you'll get StackOverflow only when you're out of memory.</p>


问:
Object creation: when should I expose a factory vs wrapping class?
I am having some problems trying to figure out when to use a factory or to wrapper class. This question is slightly geared towards C# I guess, so I'm not sure if this is the correct place to ask.

Say there is a library that exposes an interface called IStackExchangeClient, and multiple differing implementations of that interface (private implementations that clients will not know about), HttpStackExchangeClient, UdpStackExchangeClient.

We can allow the object creation of the interface through a factory, like StackExchangeClientFactory.Create(), or we can wrap it up in another class like StackExchangeClient that internally does the same thing, but simply proxies.

In the second instance, clients can write new StackExchangeClient() which feels much more natural to me. So my question is, when do I choose one or the other? What are some of the considerations that I should be taking in?

I've thought hard about it and I can't choose between one style or the other, but clearly some parts of the .NET framework use factories, and some parts don't, even when there are multiple underlying implementations of its interface.

Thank you!

答:
A Factory allows for the creation of objects that require complex setup code, or shared setup code across implementations. You should not perform the same complex setup in a constructor as a constructor is only intended to initialize an object and get it ready for use by setting member variables.

The StackExchangeClientFactory.Create() implementation allows you to perform setup-code that is shared across implementations, determine the most appropriate implementation for use and then return that implementation. In the wrapper you would have to perform the same setup-code in your constructor, which should be avoided.

Also, if you are using DI (Dependency Injection), which you should, you would use a Factory to return the appropriate implementation to the DI framework. The Create-method on the Factory applies logic to determine the appropriate implementation and returns that.


答: One way is to maintain a 'system' log file that usually is kept empty, but is pre-allocated. Then, for truly serious errors, you write the error to this location that is guaranteed to be available (well...) and has enough space for messages to be written to it. This should use a different, and very simple, error-logging system to avoid errors in the logging subsystem.</p>

Title: What an application should do when there is an error notifying the user of an error?

Body:
Say I have an application, and it writes (among other things) errors to a log file. It's the first place the user would be asked to go and look if there was a problem. Let's also assume that this is a critical application that cannot be allowed to just crash.

For the sake of argument we'll also say that all exceptions are being logged as well. I'm in the boat that says this is a bad idea, but we'll run with it for the sake of this fictional problem.

How should the application ideally handle the situation where it is completely unable to write to its log? Say due to a lack of disk space, exhausted file handle count, or a permissions/access issue.

Depending on the exact reason of course it may be able to partially resolve itself. If there was no free disk space for example it might be reasonable for it to trash the current log to write out a line saying its out of disk space, at the expense of losing the log contents. Although this is very unhelpful if something external is eating space and would just result in logs being emptied.

It may be prudent for it to log to an internal buffer, so that once the situation is resolved no data is lost - but left too long this'd simply munch on memory.

So what do you think, how should a fault notifying the user of an error be handled? Especially with an eye to headless/unattended applications that'd normally just monitored by looking at their logs.


答:
How should the application ideally handle the situation where it is completely unable to write to its log? Say due to a lack of disk space, exhausted file handle count, or a permissions/access issue.

If you cannot write to the logs, you cannot write to the logs. It's pretty much out of your control until the cause is identified and it's resolved. In the meantime, for the sake of notification and falling back to at least some sort of logging, write what you'd typically log out to somewhere else.

As a temporary measure, maybe write to a remote store? You rely on a connection to this remote location, which could present another set of problems, but at least you (hopefully) escape a lot of the issues that might be in the way of you logging locally (disk space, permissions etc.). Alternatively (last resort), notify the relevant people of exceptions via e-mail. I would do this anyway the instant there's an issue logging exceptions.


问:
Is it premature optimization to add database indices?
A colleague of mine today suggested that we go through all of the queries in our application and to add indices accordingly.

I feel this is premature optimisation because our application is not even released yet. I suggested to monitor for slow queries once we go live and then add indices accordingly.

What is the general consensus when designing your database, should you add a matching index every time you write a new query? Or is it better to just monitor and see how it goes?

答:
Premature optimization is "optimizing" something because of a vague, intuitive sense that, y'know, this will probably be slow, especially to the detriment of code readability and maintainability.  It doesn't mean willfully not following well-established good practices regarding performance.

Sometimes that's a difficult line to draw, but I'd definitely say that not adding any indices before you go live is too-late optimization; this will punish early adopters--your most eager and most important users--and give them a negative view of your product, which they will then spread around in reviews, discussions, etc.  Monitoring queries to find pain points that need indexing is a good idea, but I'd make sure to do that no later than the beta.


答:
monitor for slow queries once we go live

because nothing says quality like making your users suffer for a lack of design!

You should know which queries need indexes when you design the tables, you know which columns are being queried on in where clauses and joins. These should be indexed already because what might not be apparent in a live environment may quickly become apparent when the load or data stored increases. What you don't want to be doing when this happens is slapping indexes on every 'slow' query, you'll end up with an index on everything.

Title: Is it premature optimization to add database indices?

Body:
A colleague of mine today suggested that we go through all of the queries in our application and to add indices accordingly.

I feel this is premature optimisation because our application is not even released yet. I suggested to monitor for slow queries once we go live and then add indices accordingly.

What is the general consensus when designing your database, should you add a matching index every time you write a new query? Or is it better to just monitor and see how it goes?


问:
Multithreading message broker in a transactional system - Best Practice
We have 2 systems that exchange tickets base on a transactional flow, they have an order in the ticket statuses if one status does not reach one system all the flow is stuck. The problem is that we use a multi threaded, load balanced message broker between this systems and we might have cases when an update1 status can be processed faster than a create, or update2 faster than update1.

I'm look for a best practice for this kind of integration.

答:
Your issue really is that you have a message brokering application that your application communicates with asynchronously, however your technical requirements are such that messages must be processed in a synchronous order.

Depending on what software package you are using as your Message Broker, many such products will have built in features for message delivery to recipients based on an appropriate sequence order.  This is a subject of debate, but I feel that using or relying on such a feature in a message brokering application is a sign of a "design smell".

What do I mean when I say "design smell", well basically it is a feature provided to you in this tool that helps hide the fact that your application logic cannot handle such scenarios where messages are received out of order.  You have clear user requirements that these messages must be processed in a certain order however you are relying not on the code of your application to ensure this but on a feature of a tool that now is tightly coupled to the proper functioning of your application.  I am not saying that this feature of your message brokering tool is a bad thing altogether though, it can be incredibly useful for ensuring that the recipient end of a message broker receives messages in the proper order if the recipient application is a more brittle vendor product or older legacy product to where you do not have the time or capacity to improve the design of said application.

So if you agree with my assessment that using such a feature in a message brokering tool introduces tight coupling to your application, OR if you simply have no control to change or affect your message broker tool, then one way I have seen this handled is to build a staging area in your database for messages to be received in.  This need not be a normalized relational database structure, the important point is that you receive messages from your broker, persist them into a staging area and designate the proper processing sequence at this time.  In a separate application thread or scheduled task you can routinely check the staging tables for messages eligible to be processed by your application.

The important point to remember is that your application as the recipient does not ACTUALLY REQUIRE messages to be received in proper order, it requires them to be PROCESSED in proper order.  Furthermore the responsibility of the message broker is NOT to guarantee to the sending system that the receiving system has successfully processed the message.  The message broker's only responsibility here is to guarantee that the message was successfully delivered to the recipient application(s) and that they returned an Acknowledgement message stating that they received it and will ATTEMPT to process it.


问:
Need help identifying a league scheduling algorithm
I am trying to create a sports league scheduler.  I am having trouble identifying an algorithm to help me efficiently fill in each slot.

Sample data to build the schedule would be:

10 teams

Each team plays each other 1 time (45 total games
required)

Each team plays no more than 1 time per day

In my testing I am using 9 days with 5 slots per day.

Combo Table (contains 45 combos)

ID

Team1ID

Team2ID

bitAssigned

Schedule Table (contains 45 time slots)

scheduleID

homeTeamID

awayTeamID

GameDate

GameTime

Right now my existing procedures fill about 90% of the slots leaving 10% of my slots empty to a scheduling conflict based off the rules above.

I loop over my schedule table in ascending date/time order.

My first slot could be Saturday at 8am.

I query a list of teams that haven't been scheduled yet.  I then make an array of possible combinations of those teams.  I then use that array to pull 1 random record from my combinations table from of combinations that haven't been scheduled yet and I place those teams on the schedule.  I then set that combination as used.

I repeat the loop over and over again and each time my list of available teams gets smaller and my array as a result is smaller too.

I'm finding that some days go fine, and on other days my final last 2 remaining teams have already played in a previous week so they are not added to the schedule again.

The only thing I havent tried yet is to "reset" the conflict days and try them over again to see if I get better placements.

Does anyone have any suggestions?

答:
Here's an algorithm I invented myself. I don't know if it already exists or is actually the round robin implementation:

```<code>1 4    1 5   1 6   1 3   1 2
2 5    4 6   5 3   6 2   3 4
3 6    2 3   4 2   5 4   6 5
</code>```

basically you start with

and always keep the 1 in the same position and rotate the rest.

That way you will always get a schedule of unique matches.
This is extremely easy to implement and scales with any number of opponents, even or uneven. If you have an uneven number of opponents, just don't place a team in the 1 position and they have a free round.

Body:
Here's an algorithm I invented myself. I don't know if it already exists or is actually the round robin implementation:

1 4    1 5   1 6   1 3   1 2
2 5    4 6   5 3   6 2   3 4
3 6    2 3   4 2   5 4   6 5

basically you start with

and always keep the 1 in the same position and rotate the rest.

That way you will always get a schedule of unique matches.
This is extremely easy to implement and scales with any number of opponents, even or uneven. If you have an uneven number of opponents, just don't place a team in the 1 position and they have a free round.


答:
While it is true that in programming it is a good idea to always

"use the most general interface possible"

it is even more true that

"everything should be as simple as possible --but not simpler".

So, if your code only needs to work with a Map, then your code should be given a Map and nothing more specific than that.  This way, your code will work when either a Map or a SortedMap is passed to it, and the code which uses your code will be able to pass it whichever best suits its own needs.

However, if your code needs to work with a SortedMap, then you cannot somehow work around that and continue using a Map under the false pretense of keeping things simple; your code will, of course, need to be given a SortedMap. This will happen if your code needs to make use of any methods which are specific to SortedMap (do not exist in Map) i.e. comparator(), subMap(), headMap(), tailMap(), firstKey(), and lastKey().

The above was fairly self-explanatory, so I suspect that the reason why you are asking the question is because you have misunderstood what SortedMap is.  So, here are a few notes about your question:

They compile to the same bytecode [I think]

SortedMap is an interface without any default methods, it does not really compile into bytecode.  The .class file of that interface contains nothing but function declarations.  Now, some of the methods declared by SortedMap are also declared by Map, and the reason why these declarations were duplicated in SortedMap was probably to add new documentation to them, but many methods of SortedMap do not exist in Map.

(Useful reading: What is the difference between declaration and definition in Java?)

No additional safety is provided since the compiler can't tell any of the differences
  between the two

Huh?  This does not make any sense.  I am not sure what you are thinking, but whatever it is, it is probably wrong.

SortedMap does offer an extra non-compiler-checked post condition, namely that the iterator is sorted

SortedMap begins with a doc comment which is about 4 kilobytes long, explaining what it is and how it differs from Map.  I am not sure it is a good idea to summarize this as "does offer an extra non-compiler-checked post condition".  Perhaps you should read the documentation.

Title: When to use a SortedMap interface?

Body:
The rule in programming is to use the most general interface possible. But the difference between Map and SortedMap is more subtle.

They compile to the same bytecode [I think]

No additional safety is provided since the compiler can't tell any of the differences between the two

SortedMap does offer an extra non-compiler-checked post condition, namely that the iterator is sorted

I would prefer code to write

```<code>SortedMap<K, V> m = new TreeMap<>();
</code>```

if the sorted-ness is used. But I'm not sure if this rule only makes sense when there's compiler checks.

I'm hoping for a somewhat rigorous (e.g. type-theoretical) answer, but since this pertains to a real code review I'm doing right now, how this applies in practice is definitely important. I've yet to find an effective example of why one matters, besides a readability bump that circularly depends on programmers following this theory.


问:
Proper use of model in asp mvc
What is a proper use of model in asp mvc?

Should a model contain only data that will be accessed inside a form, or it is also a good idea to put data that are static and would be used in a view, but not inisde a form of a view.

I am asking this because I am curious about relevance of a model size on performance. If this static data should not be kept in a model, what would be a proper place for it - maybe ViewData, ViewBag or TempData. I still find it easy to store it in a model, and specify what data not to update on model update.

I would appreciate your comments..

答:
Your confusion is understandable. Different MVC frameworks have different approaches when it comes to models: some encourage lightweight models, while others welcome models which contain a lot of information.

In ASP.NET MVC, the common practice I observe in many projects is the following:

Business logic:

Models don't contain business logic. In other words, they don't perform anything which involves business rules—this is the task of a controller and the classes used by the controller.

Models don't contain elementary logic which is usually performed by a view. For instance, if the page should display a date in a human-friendly format, such as “November 15th, 2013”, the controller will just use DateTime object which will remain as-is in the model. It's the view which will be in charge of the conversion from DateTime to a specific format (eventually using localization).

Database access:

Models don't access database directly. This may be obvious for you, but the predecessor—ASP.NET, had often SQL queries within the templates, something which would be considered insane by most developers today.

Models can contain lazy IEnumerable<T> elements which lead to the database; neither the model, nor the view are defining how the data is loaded, but still, the loading itself is postponed until the view is displayed, instead of being done by the controller.

Data:

Remains obviously the data itself, that is the data loaded by the controller and transferred to a view through a model. In a model, you'll generally find all the data required to generate the page, except the static content. For instance, on a web page which displays the products, the model could contain:

The list of products (in a form of a lazy IEnumerable<T>),

The information about the user, if the user is authenticated,

The cart,

and won't contain elements such as:

The logotype of the website,

The site menu,

The footer with copyright notice,

since they can reside in the view.

Model, ViewData, ViewBag and TempData

Since the release of the DLR, it is not unusual to see data in places other than the model, resulting in more confusion. Even the ASP.NET MVC demo project moves a lot of info from the model into the ViewBag.

The choice here is basically the same as the choice between static typing and dynamic typing, with one difference.

What is the same is that you can if you want move everything to ViewBag and avoid using a model, or keep everything in the model and avoid using the ViewBag: in a case of a model, you have the benefit of static typing, which often means stricter code with bugs found at compile time instead of run time.

For instance, if you are expecting a date in your model and your view is extracting information from this date (such as displaying it in humanly readable format), the controller cannot possibly pass a string. Visual Studio will alert you by underlining the string, and if you try to compile, you'll see an error.

If, instead, the date is in the ViewBag and instead of a DateTime object, you put a string, Visual Studio will be fine with that, and the compiler won't tell you anything. You'll have to wait until you run the page to see that something went wrong.

The difference, on the other hand, is that ASP.NET MVC's views have a special status of being second-class citizens. Since by default, they are not compiled, it's very easy to make mistakes in the views themselves (such as expecting a DateTime while the model indicates that it contains a string).

This means that there is nothing wrong to heavily rely on the dynamic aspects of ViewBag in ASP.NET MVC. If you know that your model and your view will change a lot, go with the dynamic ViewBag. If you know that the model is well defined and is not subject to change unless the requirements change, the real, statically typed model would probably be a better choice.

Another aspect which should be noted is performance. If you're building a large scale web application, you should definitively compare the performance of statically-typed models with the performance of the dynamic ViewBag. I remember that my benchmarks for a project which had nothing to do with ASP.NET MVC showed that switching between static types and dynamic has practically no difference in terms of performance, but still, make sure you do the actual benchmarks.

Title: Proper use of model in asp mvc


问:
Should I use multiple state machines for a layered protocol?
When implementing a layered communications protocol are layers commonly implemented as state machines?

I have an implementation of PMBus I am currently working on for an embedded device. I have a interrupt driven interface for handling how the I2C uses the H/W driver to receive and transmit bytes onto and off of a buffer, essentially serialising the data.

A state machine then implements the "middle" SMBus layer taking the data from the buffer and forming messages from it according to how the protocol dictates what to expect for a generic type message, this state machine may also adds information based on input from additional I/O lines.

A second state machine then caps the stack with the PMBus layer by checking the generic message as it applies to specific types of messages, again adding some further information from I/O lines and subsequently looks up a command handler according to the message and passes it data from the message.

The whole caboodle then works in reverse for transmitting responses from the command handler or requests by other parts of the system (which works from a third state machine) to change output on I/O lines, such as recording an error status requiring a request for service signal.

I'm coming from a low level embedded background with little real-job programming experience consequently having only a little idea of the expected and accepted design patterns for common problems. Thus my question; is such state machine layering a common or logically expected solution to implementing layered protocols?

It seemed effective and robust to me (a soft requirement is that the layer functionality be encapsulated) when I thought it up some time ago but looking back on it it begins to seem somewhat over-engineered and that there could be some simpler method.

EDIT: I remembered the main reason that I followed this route is so that the execution of the higher layers is de-coupled from the interrupt driven interface. This is handy because from an interrupt for data being received up to the return from the command handler for the transmitted data may (depending on how the implementation is to be used) may constitute a relatively long time in comparison to my device's main business logic which operates quite fast and has a worst-case execution time requirement. Thus my business logic state machine (the third one from above) is what calls the protocol state machines, from the top on down, when it has time to. This means that while data receipt and transmission is asynchronous, the processing of the data is not.

答:
You say you have little real-world experience in this area.  Learn from experts to become one.  Look around at what professionals of this field are doing to solve your problem.  Open source PMBus implementations are out there.  The Linux kernel is an example.  But Freescale also has a PMBus library out there that is likely better for your background.

General advice about how to think about these things:

If the levels of the protocols are complex enough, then layering is a very good way to handle this complexity.  However, layers create little mini-interfaces.  So you don't want too many of them. Each of these can break up possible optimizations or slow performance.  So, there is a balance to be made with  layering to reduce complexity vs. performance.  Remember that layering creates a rigid structure around the implementation.  This rigidness helps to make it understandable, but the price is that you have by definition less flexibility.

Another way of looking at the problem is whether layers have state or not.  In general, stateless layers are preferred.  Many interacting state machines can lead to problems with keeping them in sync with one another.  Don't take this too far.  Obviously some layers have inherent state (such as the lower hardware I2C level with a multi packet message).  But many layers do not.  Often times, state is inherent at the hardware level and at the application layer.  Try to keep any middle layers stateless.  In your case, those extra bits of info from the I/O lines in the middle state might be a bit troublesome.

The most overlooked and complex part of these kinds of designs is the error handling.  It may seem simple and straightforward at first, but then you realize all those little cases where the bus doesn't respond and you have to break out of that wait loop.  Or the buffer is full and you received data, etc.  This is where those interacting state machines can become a nightmare.  Make sure you watch for those paths or you will have a lot to deal with.


答:
"Premature optimization", in its derogatory sense, means costly optimization that might not be needed. It doesn't mean all optimization implemented before the latest possible point to prevent bankruptcy!

In particular, it's legitimate to optimize based on performance tests before going live, to ensure you can meet some sensible (albeit approximate) requirements for your app to not completely suck.

At a absolute minimum you should load up your database with a plausible amount of test data and check the responsiveness of your app. This isn't premature, since you know it's going to happen, and it will catch any queries that trigger absurdly slow scans. As A E says in a comment:

Use indexes to avoid a full table scan for any query which the
  end-user will commonly be doing in real-time

At least, for tables that are planned to grow in use.

Then as a shortcut to that, if you have significant experience with the database engine and you've already planned the tests when you write the first cut of the code, then often you'll know without even running it that the query you're writing will be too slow without an index. Of course you're free to pretend you don't know, and watch the test fail before adding the index to make it pass, but there's no reason for known faulty code (because unresponsive) to go live.


问:
Is it reasonable to assume that any physical quantity can be represented by a 64-bit integer without overflow or underflow?
The original binary search algorithm in the JDK used 32-bit integers and had an overflow bug if (low + high) > INT_MAX (http://googleresearch.blogspot.com/2006/06/extra-extra-read-all-about-it-nearly.html).

If we rewrote the same binary search algorithm using (signed) 64-bit integers, can we assume that low + high will never exceed INT64_MAX because it's physically impossible to have 10^18 bytes of memory?

When using (signed) 64-bit integers to represent physical quantities, is it reasonable to assume underflow and overflow can't happen?

答:
First I would answer the question what physical values can/should be represented by an integer number?

Integer is a representation of a natural number (and differences between them) in a computer system, so applying it to anything else is wrong. Thus, invoking distances or other quantities that belong to continuous domain is not an argument. For such quantities there are real number representations. And you can always chose an arbirarily large unit and fit any value with a given precision.

So what are physical values that are natural numbers and can they overflow 64bit integer?

I can think of two. Number of physical objects (like atoms), and energy levels that a quantum system can be in. Those are two things that are strictly integer.  Now, I know you can split an atom, but it still produces an integer amount and you cannot split it indefinitely. Both of those can easily surpass 64bit range of unsigned integer. Number of atoms is higher, and one atom can be in more than one energy state.

Whether information is physical or not, is very debatable. I would say it is not. Therefore, I wouldn't say amount of information is a physical thing. So is not the amount of RAM or anything like that. If you would allow this, then easily number of atoms surpasses this number, because you need more than one atom to store one bit with today's technology.


答:
When your application is released, it is too late.

But any proper development process should include performance testing.

Use the results of your performance tests to decide which indexes to add, and verify their effectiveness by repeating the performance tests.


问:
OAuth 2.0 - Should Client Identifier be unique for each user or only by client type?
Oauth 2 Specification, Section 2.2. (Client Identifier) states -

The authorization server issues the registered client a client
     identifier -- a unique string representing the registration
     information provided by the client. The client identifier is not a
     secret; it is exposed to the resource owner and MUST NOT be used
     alone for client authentication. The client identifier is unique to
     the authorization server.

My question is whether the Client ID should be unique for each end user or only by client type?

For example if I have a native app client, should each app install get it's own client ID or should all apps get the same client ID?

And is it the same for the client_secret?

答:
Your native app client should use one client ID. In fact for most services you will get only one ID for your registered client (app).

It seems like you have some problems with the terminology in OAuth 2.0 (client, resource owner, resource, authorization), so maybe read a tutorial e.g. this short one to get a grasp for it.