答:
It's the complexity that gets you: it's pretty easy to build a simple system that is 100% error-proof. The problem is, such system would not be very useful. As soon as you start piling up complexity, you start creating trouble.

Even the simplest web application requires interaction of many parts. Here is an oversimplified list:

The OS of the client

The browser on the client

The network I/O library on the client

The networking infrastructure of the client Internet provider

The networking infrastructure on the way to your server's internet provider

The networking infrastructure of the server's Internet provider

The OS of the server

The application server

The network I/O library on the server

Your server application

If you go deeper, you get more components: XML parsers, HTML renderers, image processing libraries, encryption libraries, database client libraries, RDBMS servers, disk I/O libraries, regular expression libraries, libraries for interacting with third-party components, and so on.

Now comes the bad part: a security problem in any of your multiple layer makes the overall system insecure. Let one component break - and the whole system comes down. Let's say each of your many components is 99.9% reliable - that's between "very good" and "incredible". Let's suppose that your system is composed of thirty such very reliable components. The overall reliability of your system is then 0.999 ^ 30, or about 97%. A good attacker would take these odds in a heartbeat!

So the answer to your question is that a sufficiently complex system contains its own undoing. You can greatly reduce the risk by lowering the complexity, but the usefulness of your system would come down as well.

Title: Are security exploits a fatality?


问:
Is Ken Thompson's compiler hack still a threat?

Ken Thompson Hack (1984)

Ken Thompson outlined a method for corrupting a compiler binary (and other compiled software, like a login script on a *nix system) in 1984. I was curious to know if modern compilation has addressed this security flaw or not.

Short description:

Re-write compiler code to contain 2 flaws:

When compiling its own binary, the compiler must compile these flaws

When compiling some other preselected code (login function) it must compile some arbitrary backdoor

Thus, the compiler works normally - when it compiles a login script or similar, it can create a security backdoor, and when it compiles newer versions of itself in the future, it retains the previous flaws - and the flaws will only exist in the compiler binary so are extremely difficult to detect.

Questions:

I could not find any answers to these on the web:

How does this relate to just-in-time compilation?

Are functions like the program handling logins on a *nix system compiled when they are
run?

Is this still a valid threat, or have there been developments in
the security of compilation since 1984 that prevent this from being a
significant issue?

Does this affect all languages?

Why do I want to know?

I came across this while doing some homework, and it seemed interesting but I lack the background to understand in a concrete way whether this is a current issue, or a solved issue.

Reference Material

Overview

Some code

答:
As a specific attack, it's as much of a threat as it ever was, which is pretty much no threat at all.

How does this relate to just-in-time compilation?

Not sure what you mean by that.  Is a JITter immune to this?  No.  Is it more vulnerable?  Not really. As a developer YOUR app is more vulnerable simply because you can't validate that it's not been done. Note that your as yet undeveloped app is basically immune to this and all practical variations, you only have to worry about a compiler that is newer than your code.

Are functions like the program handling logins on a *nix system compiled when they are run?

That's not really relevant.

Is this still a valid threat, or have there been developments in the security of compilation since 1984 that prevent this from being a significant issue?

There is no real security of compilation, and can't be.  That was really the point of his talk, that at some point you have to trust someone.

Does this affect all languages?

Yes.  Fundamentally, at some time or another, your instructions have to be turned into something the computer execeutes, and that translation can be done incorrectly.

OwnerDisplayName: Andrew

ContentLicense: CC BY-SA 3.0


问:
In python, is it considered better to define a module with functions or a module with a class that contains functions?
In my code either of the above two options would work equally well but I am interested in finding out the pros and cons of both approaches.</p>

答:
The basic question is if you need to have different instances of your module/class. a python module is like a singleton - you refer always to the same object, i.e. if you need instances, you need a class.

However, if you do not need instances you might still have the need to set up your "object" to an initial state. while this is clearly possible with a plain module, if the initializing is more complex than just setting some values, I would advice to have initializing code in a class's __init__ method for readability.


问:
Do people who get paid to write code but score low on the "programmer competency matrix" have an ethical duty to find a different kind of job?
People advise me to "find a job doing something that you are good at", but the truth is that I don't believe I am good with anything other than just writing code.  I don't think that I can design or structure a program though.

Don't get me wrong, I'm not the kind of programmer who can't write a fizz buzz program; I'm more like the kind of programmer who can't do much besides getting the job done. It's not that I have a "do the bare minimum" attitude; it's that I'm painfully aware of my limitations as a programmer.

For example, implementing a quick sort algorithm is something that I could do only by relying on rote memorization (that is, if I ever find the motivation to spend a good chunk of my day trying to commit the entire algorithm to memory and not just go like "umm, I see, that makes sense, I can see the logic...").

When it comes to structuring and designing an application I feel I'm just as helpless. Since I am unable to see the right answer at the start I just make a judgement call which nine times out of ten I later regret.

I still manage to obtain job promotions and praise from my coworkers, so others don't necessarily share my opinions.  Objectively speaking though, I simply have neither the learning ability nor the sheer brain power to realistically aspire to be anything more than a 'slightly above average' programmer.

I wonder whether I actually have an ethical duty to make room for more talented people and find myself another kind of job, even if I'm fairly confident that I would not find another role I am better qualified for.  My question then is "Do average programmers have a place on a team?"

答:
You are under no ethical obligations to excel at your work, only to do what you think it is you're getting paid to do.  You might be better off doing something else related to computers?  SDET work is probably something you'd be super comfortable with, as by and large the best way to show brilliance as an SDET is to show why other people's code is flawed.  This will give you insight into just how good you really are, and it will also take the burden of "Am I good enough" off of your shoulders.

As I read this, I wanted to make sure I was clear: Most coders are terrible. You're being far too hard on yourself and by working as an SDET you'll be slapped in the face with how more qualified you might be to do your job than you think.  Trust me, it changed my life. ;)


答:
I wonder whether I actually have an ethical duty to make room for more
  talented people and find myself another kind of job

No, you don't. If anything, you have an ethical duty to take care of yourself and your dependents. There's no shortage of jobs for talented developers, and there's no reason that you should put the interests of people that you probably don't even know ahead of your own.

Indeed, this very site is full of questions from employers about how to find qualified developers. Your employer may legitimately feel lucky to have you.

Title: Do people who get paid to write code but score low on the "programmer competency matrix" have an ethical duty to find a different kind of job?

Body:
People advise me to "find a job doing something that you are good at", but the truth is that I don't believe I am good with anything other than just writing code.  I don't think that I can design or structure a program though.

Don't get me wrong, I'm not the kind of programmer who can't write a fizz buzz program; I'm more like the kind of programmer who can't do much besides getting the job done. It's not that I have a "do the bare minimum" attitude; it's that I'm painfully aware of my limitations as a programmer.

For example, implementing a quick sort algorithm is something that I could do only by relying on rote memorization (that is, if I ever find the motivation to spend a good chunk of my day trying to commit the entire algorithm to memory and not just go like "umm, I see, that makes sense, I can see the logic...").

When it comes to structuring and designing an application I feel I'm just as helpless. Since I am unable to see the right answer at the start I just make a judgement call which nine times out of ten I later regret.

I still manage to obtain job promotions and praise from my coworkers, so others don't necessarily share my opinions.  Objectively speaking though, I simply have neither the learning ability nor the sheer brain power to realistically aspire to be anything more than a 'slightly above average' programmer.

I wonder whether I actually have an ethical duty to make room for more talented people and find myself another kind of job, even if I'm fairly confident that I would not find another role I am better qualified for.  My question then is "Do average programmers have a place on a team?"


答:
On the contrary, I think that the people who score highly on the programmer competency matrix (or any similar measure) have an ethical duty not to be elitist snobs.

Ted Neward wrote a fantastic blog post recently which I think is relevant here. Here's my favourite quote from it:

Not all of us writing code have to be artists. Frankly, in the world of painting, there are those who will spend hours and days and months, tiny brushes in hand, jars of pigment just one lumens different from one another, laboring over the finest details, creating just one piece... and then there are those who paint houses with paint-sprayers, out of cans of mass-produced "Cream Beige" found at your local Lowes. And you know what? We need both of them.

For what it's worth, only the former of those two characters would agonize over their own capabilities in the way that you have.


答:
There's a theoretical chance for this to happen. There is, however, a way of checking if a specific compiler (with available source code) has been compromised, through David A. Wheeler's Diverse double-compiling.

Basically, use both the suspected compiler and another independently developed compiler to compile the source of the suspect compiler. This gives you SCsc and SCT. Now, compile the suspect source using both of these binaries. If the resulting binaries are identical (with exception of a variety of things that may well legitimately vary, like assorted timestamps), the suspect compiler was not actually abusing trust.

Title: Is Ken Thompson's compiler hack still a threat?

Body:
Ken Thompson Hack (1984)

Ken Thompson outlined a method for corrupting a compiler binary (and other compiled software, like a login script on a *nix system) in 1984. I was curious to know if modern compilation has addressed this security flaw or not.

Short description:

Re-write compiler code to contain 2 flaws:

When compiling its own binary, the compiler must compile these flaws

When compiling some other preselected code (login function) it must compile some arbitrary backdoor

Thus, the compiler works normally - when it compiles a login script or similar, it can create a security backdoor, and when it compiles newer versions of itself in the future, it retains the previous flaws - and the flaws will only exist in the compiler binary so are extremely difficult to detect.

Questions:

I could not find any answers to these on the web:

How does this relate to just-in-time compilation?

Are functions like the program handling logins on a *nix system compiled when they are
run?

Is this still a valid threat, or have there been developments in
the security of compilation since 1984 that prevent this from being a
significant issue?

Does this affect all languages?

Why do I want to know?

I came across this while doing some homework, and it seemed interesting but I lack the background to understand in a concrete way whether this is a current issue, or a solved issue.

Reference Material

Overview

Some code


问:
Are security exploits a fatality?
I'm asking this here and not on the security related site because this one is a question about "software architecture" and "development methodologies", which are both covered by the FAQ.

EDIT: I'm talking about purely remote root / admin software exploits.  Not about insider physically breaking into labs / companies / houses / networks / machines.

Everytime there's a blog entry or article about a new security exploit there are lots and lots of people basically writing that: "No software shall ever be secure, every single software out there can be exploited. Give any pirate sufficient time and he'll eventually exploit any high-target software."  This is a constant. These type of comments always get modded like crazy and most people seems to accept it as a fact.

And it really gets me wondering: can any software architecture and development methodologies be used to ever come up with secure software or is there something technical that prevents us from writing secure systems/programs/OSes/servers?

The more I think about it, the more I don't understand why we couldn't, technically, build 100% secure software.  All the way down from the OS and then up: browsers, plugins...

Note that I'm not interested in a discussion here: I want to know if technically there is something or not that can prevent, say, an OS, to be written in a 100% secure way and why it's that way.

Now if there's nothing technically that prevents us from writing a 100% secure OS and from then writing a 100% secure server, why hasn't it been done?  Does it means our architecture and development methodologies (and tools?) are deeply flawed?

答:
I did more reading and realized that there is actually some hope. I edited the question to point out that I was talking only about purely software exploits, not about people pointing a gun to your head and forcing you to give your data.

There's actually right now, in production (and apparently already used in many devices: tens of millions if not hundreds of millions), a micro-kernel that has been formally proofed as being immune to:

buffer overflows / overruns

memory leaks

no null pointer exceptions

no arithmetic overflows

In other words: it is impossible for a security exploits to happen by using any of the above because it has been mathematically prooved (by using an automated theorem prover) that it its, for example, impossible to trigger a buffer overflow in that microkernel.

That's the first of its kind and it shows that there's a way out.  It is apparently a very active area of research and I'm willing to bet we're going to see more of this in the future.

The implication are huge: no userland process can do privilege escalation leading to control of the microkernel.

There can of course still exist other ways to pirate the system as of now: right now it's just the micro-kernel (and, partially, the compiler) that have been proved to be resilient to these attack vectors.  There may be other attack vectors which haven't been tested yet but still...

It shows that, technically, it is totally possible to not only create an OS that is immune to software exploits but that, moreover, it is possible to mathematically prove that the protection in place aren't exploitable.

I expect this area of research to gain more and more traction.

(*) The Isabelle / HOL theorem prover uncovered 160 bugs (which have been all fixed) in the 7500 lines of C and 600 lines of assembly in the seL4 microkernel, which is now guaranteed to make it impossible, for example, to have a buffer overflow (a typical attack vector) or a null pointer exception in the seL4 microkernel.

Title: Are security exploits a fatality?


问:
Sort algorithms that work on large amount of data
I am looking for sorting algorithms that can work on a large amount of data, i.e. that can work even when the whole data set cannot be held in main memory at once.

The only candidate that I have found up to now is merge sort: you can implement the algorithm in such a way that it scans your data set at each merge without holding all the data in main memory at once. The variation of merge sort I have in mind is described in this article in section Use with tape drives.

I think this is a good solution (with complexity O(n x log(n)) but I am curious to know if there are other (possibly faster) sorting algorithms that can work on large data sets that do not fit in main memory.

EDIT

Here are some more details, as required by the answers:

The data needs to be sorted periodically, e.g. once in a month. I do not need to insert a few records and have the data sorted incrementally.

My example text file is about 1 GB UTF-8 text, but I wanted to solve the problem in general, even if the file were, say, 20 GB.

It is not in a database and, due to other constraints, it cannot be.

The data is dumped by others as a text file, I have my own code to read this text file.

The format of the data is a text file: new line characters are record separators.

One possible improvement I had in mind was to split the file into files that are small enough to be sorted in memory, and finally merge all these files using the algorithm I have described above.

答:
I have just built some abstract structures called big queue and big array to simplify big data sorting and searching task on a single machine with limited memory. Basically, the algorithm used is similar to the one you mentioned above - external merge sort.

I can sort 128GB data(each item 100 bytes) in 9 hours on a single machine, and then binary search the sorted data with almost no time.

Here is a post about how to search big data by using my open source big queue and big array structures.


问:
What is an Anti-Corruption layer, and how is it used?
I'm trying to figure out what the Anti-Corruption layer really means. I know that it's a way to transition/work around legacy code or bad APIs. What I don't understand is how it works and what makes it a clean separation from the undesirable layer.

I've done some searching, but I can't find any simple examples or explanations, so I'm looking for someone who understands it and can explain it with simple examples. An answer that would satisfy my question should be simple (not necessarily short), and provide understandable examples of implementation and use.

See this question, for my use case.

答:
To quote another source:

Create an isolating layer to provide clients with functionality in
  terms of their own domain model. The layer talks to the other system
  through its existing interface, requiring little or no modification to
  the other system. Internally, the layer translates in both directions
  as necessary between the two models.

Eric Evans, Domain Driven Design, 16th printing, page 365

The most important thing is that different terms are used at each side of the anti corruption layer. I was once working on a system for transportation logistic. Rounds had to be planned. You had to equip the vehicle at a depot, drive to different customer sites and service them and visit other places, like a tank stop. But from the higher level this was all about task planning. So it made sense to separate the more general task planning terms from the very specific transportation logistic terms.

So an anti corruption layers isolation is not just about protecting you from messy code, it is to separate different domains and make sure that they stay separated in the future.


问:
How to avoid duplication between my issue tracker and the project specification documents?
I used to work for a professional consulting firm, and we worked under many different contract terms. When we could get a time & materials project, we ran it with SCRUM and tracked the backlog in our issue tracker system.

Most of the time, however, we had to deliver under a fixed price contract.  This required a specifications document as an appendix to the contract.  So we always ended up batch importing work-items (or worse, manually entering) from the spec.  Change orders took a lot of time to make sure everything was in sync, especially towards the end of the project.

Is there a methodology or software tool out there that keeps this whole process DRY?  I've done some searching and I'm apparently not using the right terms.  Most of my professional network doesn't do fixed price work.

I'd be open to:

Switching my bug tracker or buying plugins (currently using FogBugz).

Following a different development methodology

Writing software to manage the specifications and update the bug tracker and specification document (but this sounds like it will be a lot of work for dubious benefit)

Lastly, is this really worth solving?  It cost us quite a bit on some projects, but on other projects it didn't end up affecting us.

答:
I would export the spec as HTML, where every section and item has an anchor. Host that export on a web server, that is accessible to everyone that has to deal with it. Then you can link from your issues to that file and use the correct anchor for addressing a certain section or item.

To keep everything in sync, you should have your spec in version control and do the HTML export automated with every check in of the spec.

When the spec changes you could also scan your issues for links where the anchor doesn't exist anymore.

However this doesn't address the problem of newly created sections or items.


问:
Should a github maintainer rewrite author's in pull requests?
I'm not a programmer by profession, but I do some coding and have used github some.  I've run across what I find to be a surprising situation.  I'm very familiar with git.

There is a project which I found a (small) bug in that was affecting me.  I spent an afternoon finding and fixing it.  I forked the repository, commit the change, and issued a pull request.   After seeing that it was closed as "Merged into development branch" I figured all was well.

I was browsing the repo today getting ready to remove my branch, and I can't find where the commit was merged into the maintainer's repo at all.  After some time I realize it's been added as a commit, but the author is no longer me.

As far as I can tell the only way to do that would be to specifically use a rebase, amend, or other history rewrite to remove the original author.

This seems very wrong to me.  At best it's confusing, at worst the author of this repo is taking credit for everyone's commits and then the history of the original contributor is lost.  Again it's a small bug, I don't use this for my professional resume, it just seems dishonest.

Is this normal?
Should I say something about it?

Edit: The general feeling seems to be that I should go ask, so I'll do just that this morning.

As per the request below.  I've checked and my code exists and was applied exactaly as I wrote it (including the comment).  I verified that both the committer and author have been changed.  There was one additional change also added at the same time as my changes.  It's a single line, which would affect the patch as well as other code before it.  IE the one line addition is not related to the bug I was fixing.

Update
It seems the answer was that the author maintains a development branch and does not want to merge from his master branch into it.  He re-authored my commit to avoid a merge.  I wasn't concerned with the original branch b/c git's plenty powerful to cherry-pick, rebase, and merge commits around as needed.

Is this typical on github?

Should I be contacting the maintainer of a project to ask which branch to apply patches to?

答:
No they shouldn't, if avoidable. It's a problem that in my experience happens far too often. However I believe it is more to do with ignorance of how to use git correctly than somebody wanting to steal credit.

If they want to modify your change before applying it to their main branch, they can easily create a branch for your change. They can then add their own commit after yours and then merge the branch in.

If your pull request is not based on the latest version of their main branch, then they can issue a git rebase master. If there are conflicts they can either choose to fix the conflicts themselves (without changing author), or give you the chance to fix it.

I think Github could and should look for this kind of accidental credit stealing and educate maintainers on best practices when appropriate.


问:
Overcoming circular reference
I am working on an asp.net MVC web application which contains several projects.

One is BusinessObjects, which contains business logic / processes.

Another is EmailGeneration which is used to send marketing campaign / customer emails.

The EmailGeneration project references the BusinessObjects project because it need to generate templated emails based on Business Objects.

I need to be able to trigger emails from business objects so that I can, say, automatically send an invoice when an order is completed.

However, I can't add the reference as it would create a circular reference. This suggests that my design is flawed.

How can I change my design to reduce coupling between the components?

答:
I agree that it sounds like your design is flawed in the fact that your business objects themselves need to send emails.  It sounds like this should be done in some sort of domain layer not the objects themselves?  Hard to say.  Alternatively you could look at creating another project that contains various interfaces for your business objects.  Then your dependencies would look like:

```<code>Business.Objects => Business.Objects.Interfaces
                 => Business.Email

Business.Email   => Business.Objects.Interfaces
</code>```

However as pointed out you will probably get more help on SO, or Programmers.

OwnerDisplayName: Kryptonite

Title: Overcoming circular reference

Body:
I agree that it sounds like your design is flawed in the fact that your business objects themselves need to send emails.  It sounds like this should be done in some sort of domain layer not the objects themselves?  Hard to say.  Alternatively you could look at creating another project that contains various interfaces for your business objects.  Then your dependencies would look like:

Business.Objects => Business.Objects.Interfaces
                 => Business.Email

Business.Email   => Business.Objects.Interfaces

However as pointed out you will probably get more help on SO, or Programmers.

OwnerDisplayName: dreza


问:
How to name a variable when the word is both a noun and a verb
I have run into a corner-case problem with the general guidance of:

nouns for variables

verbs for functions

Specifically, I have a case where the word is ambiguous - it can be either a verb or a noun.  And in some cases when we're discussing the application, it will be used both ways in the same sentence.

My intent is to make sure the program will remain readable to future developers as well as myself when I return to sections of code months later.

One of the examples is with a battery.  A battery has a charge and you can also charge() a battery.

I think that having both Battery.Charge and Battery.Charge(value) will be confusing to future developers.

My current solution is to simply pick a different word for one or both of those cases (the variable and the function).  My problem with that approach is the Battery object's variable and function for charge won't align with design discussions involving the Battery.

My question is if there is another / better way to handle this conflict in naming convention?

Some additional reading on the subject.  None really addressed the particular of my question.

Meaningful concise method naming guidelines

Naming conventions for variables

The selected answer from https://softwareengineering.stackexchange.com/questions/14169/what-naming-guidelines-do-you-follow

答:
Just throwing this out there, but maybe the solution for this instance of naming ambiguity is to remove that functionality from the battery entirely. I've never seen a self charging battery and it would make more sense to me to have a BatteryCharger class. This would help keep your concerns more decoupled and make the action more explicit.

battery.Charge(50) vs batteryCharger.Charge(battery, 50)

To me, the second form is much more understandable and keeps all your "Charging" code in one place rather than sprinkling it throughout all your battery classes.


问:
Why is an interface in Java not allowed to have state?
There must be a good reason why Java designers didn't allow any state to be defined in interfaces . Can you please throw some light on this aspect of design decision ?</p>

答: An interface is a contract specifying what its implementer promises to be able to do. It does not need to specify state because state is an implementation detail and only serves to constrain implementers in how this contract is fulfilled. If you want to specify state you might want to rethink you use of interfaces and look at abstract base classes instead.</p>


答:
Avoid Double Meanings

You have deliberately selected a word that has more then one meaning, and that first decision is the problem. There are a ton of words that are problematic for programmers. Another example would be phone. You can phone someone, or you could have a phone in your pocket.

Use Getters and Setters

The standard naming for most objects is the getters/settings methods for properties.

```<code>Battery.Charge            // would be a property
Battery.setCharge(value)  // would set the property
Battery.getCharge()       // would get the property
</code>```

Properties Are States Not Nouns

I think you are mistaken by classifying object properties as nouns, and variables could also be thought of states. They are states relevant to the local scope of their existence.

You could describe the value that they hold as a noun, but I'm not sure that is true in all cases.

In OOP terminology object properties describe the state of that object. In your case the Battery is an object, and it's Charge is a state. So that would be a property of the object, but this depends on the context of how it's used.

If you need to be able to Charge the battery, and also know what it's current Charge is, then you have a problem.

Using Scope To Enforce Context

Context is what will clarify which meaning of a word you intend a method or property to convey. Scope is setting the accessibility of a property/method from outside the object.

```<code>Batter._charge            // a hidden private property
Battery.setCharge(value)  // would set the private property
Battery.getCharge()       // would get the private property
Battery.Charge()          // would perform the Charge action
</code>```

Methods Are Verbs

You can describe the method of an object as a verb, but the word action is better suited. In OOP terminology you perform actions upon objects using their methods. It's bad form to modify an object's property from outside the object. It's preferred to call a method that performs the actions required that causes it's state to change.

The word Charge is a verb, but it's also a noun. When used to call the method of an action it becomes clear that the verb is being used Battery.Charge(....).

But, context is very important. While the word Charge() is a verb it's not as meaningful as startCharging().

Valid methods for Battery could include Charging, Discharging, setCharge, getCharge, hasCharge, Discharge and Charged.

Simple one word methods often don't explicitly state their actions clearly, but there are some cases like open and close where little explaining is required.

So there isn't really a correct answer as to how to name these types of properties/methods. Except that you need to use the above techniques wisely to ensure there is no confusion.

Title: How to name a variable when the word is both a noun and a verb

Body:
I have run into a corner-case problem with the general guidance of:

nouns for variables

verbs for functions

Specifically, I have a case where the word is ambiguous - it can be either a verb or a noun.  And in some cases when we're discussing the application, it will be used both ways in the same sentence.

My intent is to make sure the program will remain readable to future developers as well as myself when I return to sections of code months later.

One of the examples is with a battery.  A battery has a charge and you can also charge() a battery.

I think that having both Battery.Charge and Battery.Charge(value) will be confusing to future developers.

My current solution is to simply pick a different word for one or both of those cases (the variable and the function).  My problem with that approach is the Battery object's variable and function for charge won't align with design discussions involving the Battery.

My question is if there is another / better way to handle this conflict in naming convention?

Some additional reading on the subject.  None really addressed the particular of my question.

Meaningful concise method naming guidelines

Naming conventions for variables

The selected answer from https://softwareengineering.stackexchange.com/questions/14169/what-naming-guidelines-do-you-follow

Body:
Avoid Double Meanings

You have deliberately selected a word that has more then one meaning, and that first decision is the problem. There are a ton of words that are problematic for programmers. Another example would be phone. You can phone someone, or you could have a phone in your pocket.

Use Getters and Setters

The standard naming for most objects is the getters/settings methods for properties.

Battery.Charge            // would be a property
Battery.setCharge(value)  // would set the property
Battery.getCharge()       // would get the property

Properties Are States Not Nouns

I think you are mistaken by classifying object properties as nouns, and variables could also be thought of states. They are states relevant to the local scope of their existence.

You could describe the value that they hold as a noun, but I'm not sure that is true in all cases.

In OOP terminology object properties describe the state of that object. In your case the Battery is an object, and it's Charge is a state. So that would be a property of the object, but this depends on the context of how it's used.

If you need to be able to Charge the battery, and also know what it's current Charge is, then you have a problem.

Using Scope To Enforce Context

Context is what will clarify which meaning of a word you intend a method or property to convey. Scope is setting the accessibility of a property/method from outside the object.

Batter._charge            // a hidden private property
Battery.setCharge(value)  // would set the private property
Battery.getCharge()       // would get the private property
Battery.Charge()          // would perform the Charge action

Methods Are Verbs

You can describe the method of an object as a verb, but the word action is better suited. In OOP terminology you perform actions upon objects using their methods. It's bad form to modify an object's property from outside the object. It's preferred to call a method that performs the actions required that causes it's state to change.

The word Charge is a verb, but it's also a noun. When used to call the method of an action it becomes clear that the verb is being used Battery.Charge(....).

But, context is very important. While the word Charge() is a verb it's not as meaningful as startCharging().

Valid methods for Battery could include Charging, Discharging, setCharge, getCharge, hasCharge, Discharge and Charged.

Simple one word methods often don't explicitly state their actions clearly, but there are some cases like open and close where little explaining is required.

So there isn't really a correct answer as to how to name these types of properties/methods. Except that you need to use the above techniques wisely to ensure there is no confusion.


问:
What happens when an unexpected packet arrives at a computer?
I have been wondering this for a few days. I basically understand how networks and packets work. But what happends when an unexpected packet arrives? Like, when I didn't send a request for a website's index, but it sends the index to my machine anyways ? Does my browser pop up? Does it ignore it? Anyway to actually catch it?</p>

答:
Simple : It gets thrown away.

If either TCP, or network layer can't accept the packet, then it is thrown away. This might happen if port is not open or packet was re-send, but original packet arrived, so it is not necessary anymore.

TCP and most network system contains rigorous checks on what packets to accept and what packet are bad or corrupted in some way. If such packet arives, it is ignored and client requests a re-send.


问:
How do we make unit tests run fast?
We have reached the point in our project where we have almost a thousand tests and people have stopped bothering with running them before doing a check in because it takes so long. At best they run the tests that are relevant to the piece of code that they changed and at worst they simply check it in without testing.

I believe this problem is due to the fact that the solution has grown to 120 projects (we usually do much smaller projects and this is only the second time we do TDD properly) and the build + test time has grown to about two-three minutes on the lesser machines.

How do we lower the run time of the tests? Are there techniques? Faking more? Faking less? Maybe the bigger integration tests shouldn't run automatically when running all the tests?

Edit: as a response to several of the answers, we already use CI and a build server, this is how i know the tests fail. The problem (actually a symptom) is we keep getting messages about failed builds. Running partial tests is something that most people do but not all. and regarding the tests, they are actually pretty well made, they use fakes for everything and there is no IO at all.

答:
0. Listen to your programmers.

If they're not running the tests, it means they perceive the cost (waiting for the tests to run, dealing with false failures) to be greater than the value (catching bugs right away). Decrease the costs, increase the value, and people will run the tests all the time.

1. Make your tests 100% reliable.

If you ever have tests that fail with false negatives, deal with that right away. Fix them, change them, eliminate them, whatever it takes to guarantee 100% reliability. (It's OK to have a set of unreliable, but still useful tests that you can run separately, but the main body of tests must be reliable.)

2. Change your systems to guarantee that all tests pass all the time.

Use continuous integration systems to ensure that only passing commits get merged in to the main/official/release/whatever branch.

3. Change your culture to value 100% passing tests.

Teach the lesson that a task isn't "done" until 100% of tests pass and it has been merged in to the  main/official/release/whatever branch.

4. Make the tests fast.

I have worked on projects where tests take a second, and on projects where they take all day. There is a strong correlation between the time it takes to run tests and my productivity.

The longer tests take to run, the less often you'll run them. That means you'll go longer without getting feedback on the changes you're making. It also means you'll go longer between commits. Committing more often means smaller steps that are easier to merge; commit history is easier to follow; finding a bug in the history is easier; rolling back is easier, too.

Imagine tests that run so fast that you don't mind automatically running them every time you compile.

Making tests fast can be hard (that's what the OP asked, right!). Decoupling is key. Mocks/fakes are OK, but I think you can do better by refactoring to make mocks/fakes unnecessary. See Arlo Belshee's blog, starting with http://arlobelshee.com/post/the-no-mocks-book.

5. Make tests useful.

If the tests don't fail when you screw up, then what's the point? Teach yourselves to write tests that will catch the bugs you're likely to create. This is a skill unto itself, and will take lots of attention.

Title: How do we make unit tests run fast?


答:
If one has source code for a compiler/build system whose output should not depend on anything other than the content of the supplied source files, and if one has several other compilers and knows that they do not all contain the same compiler hack, one can make sure one gets an executable that depends upon nothing other than the source code.

Suppose one has source code for a compiler/linker package (say the Groucho Suite) written in such a way that its output will not depend upon any unspecified behaviors, nor on anything other than the content of the input source files, and one compiles/links that code on a variety of independently-produced compilers/linker packages (say the Harpo Suite, the Chico suite, and the Zeppo Suite), yielding a different set of exeuctables for each (call them G-Harpo, G-Chico, and G-Zeppo).  It would not be unexpected for these executables to contain different sequences of instructions, but they should be functionally identical.  Proving that they are functionally identical in all cases, however, would likely be an intractable problem.

Fortunately, such proof won't be necessary if one only uses the resulting executables for one single purpose: compiling the Groucho suite again.  If one compilers the Groucho suite using using G-Harpo (yielding G-G-Harpo), G-Chico (G-G-Chico), and G-Zeppo (G-G-Zeppo), then all three resulting files, G-G-Harpo, G-G-Chico, and G-G-Zeppo, should all byte-for-byte identical.  If the files match, that would imply that any "compiler virus" that exists in any of them must exist identically in all of them (since the all three files are byte-for-byte identical, there's no way their behaviors could differ in any way).

Depending upon the age and lineage of the other compilers, it may be possible to ensure that such a virus could not plausibly exist in them.  For example, if one uses an antique Macintosh to feed a compiler that was written from scratch in 2007 through a version of MPW that was written in the 1980's, the 1980's compilers wouldn't know where to insert a virus in the 2007 compiler.  It may be possible for a compiler today to do fancy enough code analysis to figure it out, but the level of computation required for such analysis would far exceed the level of computation required to simply compile the code, and could not very well have gone unnoticed in a marketplace where compilation speed was a major selling point.

I would posit that if one is working with compilation tools where the bytes in an executable file to be produced should not depend in any way upon anything other than the content of the submitted source files, it is possible to achieve reasonably good immunity from a Thompson-style virus.  Unfortunately, for some reason, non-determinism in compilation seems to be regarded as normal in some environments.  I recognize that on a multi-CPU system it may be possible for a compiler to run faster if it is allowed to have certain aspects of code generation vary depending upon which of two threads finishes a piece of work first.

On the other hand, I'm not sure I see any reason that compilers/linkers shouldn't provide a "canonical output" mode where the output depends only upon the source files and a "compilation date" which may be overridden by the user.  Even if compiling code in such a mode took twice as long as normal compilation, I would suggest that there would be considerable value in being able to recreate any "release build", byte for byte, entirely from source materials, even if it meant that release builds would take longer than "normal builds".


问:
How to make custom libraries accessible?
I am trying to compile and install every custom module under it's own designated folder. (ex: /myApps/myLinux/compiled_app)

I had luck with Python so far, where my Python is compiled from source and lives in:
/myApps/myLinux/python2.5 and "python2.5" -> /myApps/myLinux/python2.5.6-gcc463

So I can access this Python through a wrapper script that sets the right environment.

The question is recently I had to compile and add something called gperf3.0.4. So now it lives:
/myApps/myLinux/gperf3.0 and "gperf3.0" -> /myApps/myLinux/gperf3.0.4-gcc463

The question is: How will I point to this lib if some other app needs to access it? Is it done through the LD_LIBRARY_PATH variable?

答:
As you know, the -L flag to the linker is used to specify a path to search for libraries (static or shared) at link time.

The -R flag to the linker can be used to embed in the executable a path to be used to search at run time.  This is needed if your shared library will be installed in a location not in the standard system shared library path.

To specify a custom flag to be used at link time to gcc, you can use the -Wl, prefix. So if you link your app with:

```<code>gcc -L/path/to/lib -Wl,-R/path/to/lib -o myapp myapp.o -lgperf
</code>```

(Or similar) your app will link against the version of your library at that path and search that path for libraries at run time.

Body:
As you know, the -L flag to the linker is used to specify a path to search for libraries (static or shared) at link time.

The -R flag to the linker can be used to embed in the executable a path to be used to search at run time.  This is needed if your shared library will be installed in a location not in the standard system shared library path.

To specify a custom flag to be used at link time to gcc, you can use the -Wl, prefix. So if you link your app with:

gcc -L/path/to/lib -Wl,-R/path/to/lib -o myapp myapp.o -lgperf

(Or similar) your app will link against the version of your library at that path and search that path for libraries at run time.


答: An interface by its nature is simply a specification of what an object should look like, by its very definition it doesn't have state, its not just java its any language.</p>

Title: Why is an interface in Java not allowed to have state?

Body: There must be a good reason why Java designers didn't allow any state to be defined in interfaces . Can you please throw some light on this aspect of design decision ?</p>


问:
What are some things you have read that inspired and guided you as a programmer?
For example, being a beginner, I find a lot of inspiration and direction from reading this post by Bryan Woods.</p>

答: For inspiration, Yegge's Blogs, Graham's Essays and the SICP Video Lectures.</p>

ContentLicense: CC BY-SA 2.5


答:
Because interfaces are meant to specify an abstraction.

A good abstraction should be as simple as possible, which means specifying as little as possible about implementation details. State is an implementation detail, and as such shouldn't be included in your abstraction.

Java designers therefore made a pretty good decision to avoid including state in interfaces.

Alternatives to consider if you ever feel like that you want state in an interface:

Use composition to plug together objects that have do state and implement your desired interfaces.

Use an abstract base class (but be aware that this mixes interface and implementation and can get you into trouble if you are not careful)

An interesting video on this topic is "Simple made easy" by Rich Hickey. They key point is that if you mix things together that should be kept separate, you get into a mess.....


问:
Round Robin - how to determine the optimal quantum of time?
I have to calculate the proper quantum of time for Round Robin process scheduling algorithm. As I read in Tanenbaum's book, the quantum of time should cover like 80% of the running time of all processes.

Is there a formula how to find the optimal quantum of time?

答:
You cannot choose an "optimal" quantum of time as long as you don't have any information beforehand about

how long each of your processes will run

how many new processes you have to expect within a given time frame

how quick each of your processes has to start / continue / respond

In a real system, you typically don't have those parameters beforehand, since this would mean to predict the future. Therefore, you will have typically have to make assumptions about those parameters, adapt the quantum dynamically to the environment, or choose a different kind of algorithm.


问:
Pushing a new version of a live web application in a Git based workflow
I have a question regarding workflow in web development. I'm building my project in Symfony, and it's in Git.

Right now, I have three environments, dev (local), staging and prod. The project itself is hosted on GitHub in a private repo.

I'm wondering what a good way is to update the staging & production environments. Should I somehow setup a push based system so I can push the prod/staging branch directly to my server, and use Git hooks to regenerate cache files and run database migrations?

Right now on similar projects I use a manual pull system, where I SSH to the server, pull down changes from the prod Git branch and manually run cache/migrations. This obviously is not ideal.

答: Have you considered a service like Beanstalk or Codebase ? They will host your git repositories, but they also have "native" support for deploying web applications, unlike Github. You would probably still have to write hooks to handle stuff like database migrations, but such services do automate much of the deployment process.</p>


答: Because with state it would have been an abstract class; and since there is multiple inheritance allowed for interfaces => Java would have supported full multiple inheritance => diamond problem.</p>


问:
Egoless programming, is it possible or even desirable
Is it possible to have egoless programming or is it even desirable?

As a profession we do seem to want to show off the latest gizmo, technique or say “look at this awesome piece of code I’ve written”. Yet we can get very defensive when asked to submit items of work for code reviews or get negative comments from other programmers (hearing the term WTF, has never been a good sign).

Can we as a profession be able to sit down and analyse a piece of code, data or architecture for its merits or constraints and a calm and respectful manner, without causing offense or antagonising our colleagues, or are we just the archetypal Dilbert character, raging against the system?

答: Yes, I think it is possible. And I think Jeff Atwood explains how to do this best in his blog entry Strong Opinions, Weakly Held. </p>


问:
How to communicate inefficiency before it's implemented?
This is a problem I come across pretty often. Basically we are using an application to make art content. Often times this requires writing custom tools for it.

Sometimes you run into parts of the application where doing something is not directly supported in the UI, which means it will either be a little more involved or communicating this to the software company that makes this software so they can address it in the future.

But I see some of the more experienced people suggesting to use methods that are extremely inefficient.

For example let's say you want to access the value of the last item in a 10M item array. Let's say it's not directly doable. So you could write some code that would be inconvenient in this case because you just want it directly doable in the app.

But instead of this, they suggest you to let's say create a spare property for each item in the array, that will basically store the same thing, and then by using a built-in tool which will go through each element and set the value of ALL these new properties to the last item read. So it will be like:

```<code>for i=0 to array.Length:
    new_property[0..array.Length - 1] = array[i];
</code>```

So this will indeed give you the last item when the operation is done, but it will be extremely slow.

How can you communicate this to the people suggesting these methods? Because they think it's quite reasonable and since they have more experience and this is mainly an art application, it shouldn't involve more work for us/them.

答:
The way I handle efficiency topics before implementation (feasibility study phase or architecture phase) is the following:

Explicit performance requirements, and even better, define the Key Performance Indicators which will allow you to check whether or not the software meets performance requirements. (If you are subcontracting the software, these KPI need to be very clear. They should be part of the acceptance criteria.)

For example: the basic requirement we had in mobile phone UI was that something (anything) should move at most 100ms after an end user action. If not possible, the longest possible reaction time needed to be below 200ms. (The rational is based on human perception of 'instantaneously').

Whenever a specific performance target seems hard to meet, prototype the expected solution. For your art application example, if you think that the suggested solution might not work, you can just ask for a proof of concept / prototype, before the real implementation.

Note: If it meet the performance requirements, do you really care about a suboptimal solution ?

(optional) If you/they cannot find a reasonable solution to meet performance requirements, it may be time to make a trade-off between 'functional' requirements, and performance requirements.

During the development process, monitor the KPI to detect possible performance issues at the earliest. The subcontractor may be doing it internally even if you don't ask for this explicitly.

Moreover, this KPI monitoring allows using the "optimize only if necessary" strategy. The developers don't spent their time optimizing their code unless there is a real need for performance.

Hope this helps.


问:
Is Ken Thompson's compiler hack still a threat?

Ken Thompson Hack (1984)

Ken Thompson outlined a method for corrupting a compiler binary (and other compiled software, like a login script on a *nix system) in 1984. I was curious to know if modern compilation has addressed this security flaw or not.

Short description:

Re-write compiler code to contain 2 flaws:

When compiling its own binary, the compiler must compile these flaws

When compiling some other preselected code (login function) it must compile some arbitrary backdoor

Thus, the compiler works normally - when it compiles a login script or similar, it can create a security backdoor, and when it compiles newer versions of itself in the future, it retains the previous flaws - and the flaws will only exist in the compiler binary so are extremely difficult to detect.

Questions:

I could not find any answers to these on the web:

How does this relate to just-in-time compilation?

Are functions like the program handling logins on a *nix system compiled when they are
run?

Is this still a valid threat, or have there been developments in
the security of compilation since 1984 that prevent this from being a
significant issue?

Does this affect all languages?

Why do I want to know?

I came across this while doing some homework, and it seemed interesting but I lack the background to understand in a concrete way whether this is a current issue, or a solved issue.

Reference Material

Overview

Some code

答: Systems in which the end users have access to the source code are the ones for which you would have to hide this type of attack.  Those would be open source systems in today's world.  The problem is that although there is a dependence on a single compiler for all Linux systems, the attack would have to get onto the build servers for all of the major Linux distributions.  Since those don't download the compiler binaries directly for each compiler release, the source for the attack would have had to be on their build servers in at least one previous release of the compiler.  Either that or the very first version of the compiler that they downloaded as a binary would have to have been compromised.</p>


问:
What is a good script language for writing server administration tasks?
I am administering hundreds of RHEL servers. There are many daily tasks to perform. Right now I am working with bash scripting and python. I was wondering if other languages such as Erlang, Lua, Ruby, Haskell, or Clojure.</p>

答:
It depends on your needs and preference.

Generally, I find that bash is the most important. There are some tasks which can get a bit ugly with it, though, in which case Python is my only choice.

Why? Because it's not as ugly as Perl, and has the greatest library availability after it.


问:
Unit Testing Open Source projects that require keys for api calls
What's the recommended method for setting up tests on code that requires a private key to make calls to external web services?

Full on mock objects or is there a way of distributing open source unit tests with a neat method to add in a tester's own private key at test time?

I'm using python but any general best practice

答:
full on mock objects. The idea of unit tests is that you're testing the unit of code that you've written - not, in this case, the external webservice. So you mock that webservice so the mock object returns data you expect and you use that the see if your code works with that data.

Now later when you come to do integration tests, you'll need to have the webservice running and accessed (so you can test that the webservice returns the data you expect it to, given the inputs you provide) but you'll have a high confidence that your unit will work correctly when this happens, because you've already tested that part in isolation!

(of course, your mileage may vary depending on your codebase and how much effort would be required to unit test that you'd get anyway with the integration testing, but the above is the concept of unit testing)


答:
Since bash and python have already been recommended, I'd recommend Ruby.

Two very handy tools come to mind: Rake and capistrano.

Rake allows you to have tasks. A task is, for example, a list of commands to execute. Or it can be way more complex and include code. A lot already exist, there are plenty of libraries, and defining a new task is relatively easy.

capistrano (based on Rake syntax) allows you to have tasks, just like Rake, but by including remote tasks by default. From the description:

Capistrano is a utility and framework for executing commands in parallel on multiple remote machines, via SSH. It uses a simple DSL (borrowed in part from Rake) that allows you to define tasks, which may be applied to machines in certain roles. It also supports tunneling connections via some gateway machine to allow operations to be performed behind VPN's and firewalls.

Not to mention that the Ruby language was invented to remain simple.

A dynamic, interpreted, open source programming language with a focus on simplicity and productivity.

Title: What is a good script language for writing server administration tasks?

Body: I am administering hundreds of RHEL servers. There are many daily tasks to perform. Right now I am working with bash scripting and python. I was wondering if other languages such as Erlang, Lua, Ruby, Haskell, or Clojure.</p>


答:
David Wheeler has a good article: http://www.dwheeler.com/trusting-trust/

Me, I'm more worried about hardware attacks.  I think we need a totally VLSI design toolchain with FLOSS source code, that we can modify and  compile ourselves, that lets us build a microprocessor that has no backdoors inserted by the tools.  The tools should also let us understand the purpose of any transistor on the chip.  Then we could pop open a sample of the finished chips and inspect them with a microscope, making sure they had the same circuitry that the tools said they were supposed to have.


答:
Just adding - an interface, by definition, doesn't have state.  That's the definition of what an interface is, and if it had state then it wouldn't be an interface any more.

If you need to use state then an interface is the wrong tool for the job you want to do.  Maybe you'd be better off asking another question (and maybe on SO itself rather than here) describing exactly what the problem you're encountering that is causing you to ask this question is, and you may get more help in finding a solution.


答:
JUnit test are normally to be quick, but some of them simply must take some time to excecute.

For example, the database test are usually taking a few time to initialize and finish.

If you have hundreds of tests, even if they are fast, they require a lot of time to run because of their number.

What can be done is:

1) Identify the crucial tests. Those for the most important parts of libraries and those which are most likely to fail after changes. Only those test should be run always on compile. If some code is often broken, its tests should be obligatory even if they take long to execute, on the other side, if some part of software never caused a problem, you can safely skip tests on each build.

2) Prepare the continuous integration server, which will run all tests in the background. It's up to you if you decide to build every hour or to build after every commit (the second makes sense only if you want to automatically detect whose commit has caused trouble).

Title: How do we make unit tests run fast?

Body:
We have reached the point in our project where we have almost a thousand tests and people have stopped bothering with running them before doing a check in because it takes so long. At best they run the tests that are relevant to the piece of code that they changed and at worst they simply check it in without testing.

I believe this problem is due to the fact that the solution has grown to 120 projects (we usually do much smaller projects and this is only the second time we do TDD properly) and the build + test time has grown to about two-three minutes on the lesser machines.

How do we lower the run time of the tests? Are there techniques? Faking more? Faking less? Maybe the bigger integration tests shouldn't run automatically when running all the tests?

Edit: as a response to several of the answers, we already use CI and a build server, this is how i know the tests fail. The problem (actually a symptom) is we keep getting messages about failed builds. Running partial tests is something that most people do but not all. and regarding the tests, they are actually pretty well made, they use fakes for everything and there is no IO at all.


答:
What you should bear in mind at all times is that for most of your professional life you don't own the code that you write.

If you are an employee the software is owned by the company. If you are a contractor the software is owned by the company. If you are a freelancer then the software is owned by the client. Only if you are the company/client is the software owned by you.

Once you realise this then there's less of the "this is my code" thoughts and attitude. Yes, it still happens and it's only human nature to get upset if someone criticises what you've just produced, but by taking that step to try to remove the attachment it should be less of a blow.

From the other side when you are reviewing the code produced by someone else concentrate on what the code does and how it does it and be constructive when suggesting improvements.

Title: Egoless programming, is it possible or even desirable

Body:
Is it possible to have egoless programming or is it even desirable?

As a profession we do seem to want to show off the latest gizmo, technique or say “look at this awesome piece of code I’ve written”. Yet we can get very defensive when asked to submit items of work for code reviews or get negative comments from other programmers (hearing the term WTF, has never been a good sign).

Can we as a profession be able to sit down and analyse a piece of code, data or architecture for its merits or constraints and a calm and respectful manner, without causing offense or antagonising our colleagues, or are we just the archetypal Dilbert character, raging against the system?


问:
Server and MySQL safety for Java / JDBC applications?
I have an external java application which use JDBC as to reach a MySQL database.

This will be used by many people and is going to store all the people's data on the same server / MySQL.

As I bet people will be able to crack the .jar and see the source, I expect them to be able to cheat the program by editing it and making it get information from other users even when they are not allowed to reach said information.

Is there any standard way to protect a server so people should only be able to reach the information their account is connected to even if said people are able to change the source?

答:
You can either add security on the SQL server proper, by using different database user accounts for each user and handling security using SQL features; you might enforce security restrictions via stored procedures, security views, etc.

This might not be a great idea, though. SQL security is not great, MySQL might not implement all the features you need to effectively protect your data and it might involve lots of work on your side.

The other solution is to add an intermediator between your app and the database which is outside the end users' control. Have your app connect to another application, authenticate against it and have the second application enforce security. This application might be a webapp which exposes the necessary methods via web services/RPC/REST, whatever.

ContentLicense: CC BY-SA 4.0


问:
Champaign Fountain Puzzle
Empty glasses of water are arranged in the following order:

When you pour liquid into the 1st glass if it's full, then the extra liquid would be flown into the glasses 2 and 3 in equal quantities. When glass 2 is full, the extra liquid would be flown into 4 and 5 and so on.

Given an N liters of liquid and maximum capacity of each glass is 1 liter, give the amount of liquid  present in any glass if you empty N liters of liquid by pouring into glass by filling out the function getWaterInBucket(int N, int X) where X is the glass number. So for example if I want have 4 liters at the beginning and I want to find the water in glass 3 the function is getWaterInBucket(4, 3)

How do I solve this programmatically? I tried to find a math solution using Pascal's triangle. This did not work. I considered it to be a tree so I can add a parameter like this getWaterInBucket(BTree root, int N, int X) and then try some recursive solution for each level, but parameters are not allowed in this problem. Is there something obvious, some trick?

答:
This is the binomial function. The ratio of the water between the glasses of level N can be discovered using nCr for each glass in the level. In addition, the total number of glasses prior to level N is the sum of 1 to (N - 1), a formula for which you should be able to find available fairly easily. Thus, given X, you should be able to determine it's level, and use nCr to check the ratios of the glasses for that level, and thus determine how much water is in X, if there are enough liters to go down to X anyway.

Secondly, your idea of using the BTree is fine, it's just that the BTree is an internal variable, not an external parameter.

IOW, if you've covered this mathematics in your education (here in the UK it's taught prior to university) then you should be able to solve this without too much problem.

Title: Champaign Fountain Puzzle


答:
I think dreza's answer addresses the coupling issue, but it continues what I see as a common flaw -- multiple projects.

Multiple projects are used a lot more frequently than they are actually required.  Unless there is a PHYSICAL need for multiple projects, they simple add complexity without much, if any, benefit.  Take your problem as an example, at best, it encourages the low coupling.  But you have to create yet another project to implement it, and if you were doing unit testing you would probably come to the same solution from a different direction.  And even the encouragement isn't without a potential downside -- the other way to overcome the circular dependency is to split the project off into it's own solution, which just hides the issue.

I would encourage you to go forward with defining an interrface as dreza suggested, but to also look into the possibility of joining any projects you have into a single project.  Unless the projects are used by different apps or on different hardware, multiple projects are, IMO, a bad design.

Title: Overcoming circular reference

Body:
I am working on an asp.net MVC web application which contains several projects.

One is BusinessObjects, which contains business logic / processes.

Another is EmailGeneration which is used to send marketing campaign / customer emails.

The EmailGeneration project references the BusinessObjects project because it need to generate templated emails based on Business Objects.

I need to be able to trigger emails from business objects so that I can, say, automatically send an invoice when an order is completed.

However, I can't add the reference as it would create a circular reference. This suggests that my design is flawed.

How can I change my design to reduce coupling between the components?


问:
Workflow: Using binary document formats in Git without locks (moving from subversion)
We're a software consultancy with a multitude of projects for different customers. We traditionally use Subversion, but are currently considering moving to Git.

A significant portion of the documents we produce are shared with our customers (requirements, global designs, test specs, etc), and we use MS Office to produce these. In Subversion, we could use its "Lock" feature to ensure that no one was editing the same document at the same time. In Git, you can't do that since by its distributed nature, git doesn't have locks.

Locks are really little more than a communication mechanism, but they're a very effective one.

Currently, our code and customer-facing documents are typically in different subfolders of a different svn repository. When moving to git, what would you recommend we do? I see a set of options:

We move the svn repositories to git 1-on-1. Instead of using locks on the Office files, we do what the git people suggest and somehow try to change our workflow to fix it. This could be working in a branch on any document edit, and merging that over review. This approach breaks over e.g. Excel sheets that contain project management info; they're easily edited by team members (and we encourage that this is done), but not subject to any formal review process

We use git for code and svn for docs and project management. This has the disadvantage that certain more design-ish documents won't be "nearby" the code it specs, increasing the chance that people forget to update them. Additionally, everybody has to use and understand two sets of tools. That said, maybe this is a great opportunity to move to text-based doc tools (latex, markdown, HTML, whatever) for non-customer-facing design docs.

Like 1, but we hack up a git lock command that does what svn lock does for us (toggle the read-only flag appropriately and sync with a server through some means).

I don't buy the argument that locks don't work in a DVCS because the system should even work when you're entirely offline. Svn locks can be overridden as well; they're a communication mechanism. Without some sort of network connection, you won't get your computer to communicate a lot.

We can't be the only shop who're very happy with how svn lock fits in our workflow, right?

Any ideas or tips?

I found https://stackoverflow.com/questions/119444/locking-binary-files-using-git-version-control-system but the discussion is rather technical; i'm looking for ways to solve or avoid the practical problem of two team members editing the same binary file at the same time.

答:
You can use git for those documents without adding locking. Choose a git workflow that blocks pushes to the master branch if not on master. (There are several workflows to choose from.) This will prevent people from overwriting each other's modifications to binary document files.
Assume two people modify the same binary document. The first one that pushes it to master gets their changes in. The second one will get blocked because their copy is behind the master branch. They have to synch first. So the second person does synch. It will show a merge conflict for the binary document. That person saves their version somewhere and resolves the conflict by taking the version from master (that was pushed by the first person). At this point the second person's files are up to date with the master branch.
They merge in their changes to the latest binary document (by hand), which will then contain both the first person's and the second person's changes. Then the new version is pushed to master and becomes the new master branch.
The merging is a pain, but it only happens when there is a conflict. Also, changes don't get lost or overwritten. The conflicts are detected and users are able to resolve them cleanly.</p>


答:
Though your description of the problem does not give a thorough insight into the codebase, I think I can safely say your problem is two-fold.

Learn to write the right tests.

You say you have almost a thousand tests, and you have 120 projects. Assuming that at most half of those projects are test projects, you have 1000 tests to 60 production code projects. That gives you about 16-17 tests pr. project!!!

That is probably the amount of tests that I would have to cover about 1-2 classes in a production system. So unless you only have 1-2 classes in each project (in which case your project structure is too fine grained) your tests are too big, they cover too much ground. You say this is the first project that you are doing TDD properly. A say, the numbers that you present indicate that this is not the case, you are not doing TDD property.

You need to learn to write the right tests, which probably mean that you need to learn how to make the code testable in the first place. If you cannot find the experience inside the team to do that, I would suggest hiring help from the outside, e.g. in form of one or two consultants helping your team over a duration of 2-3 months to learn to write testable code, and small minimal unit tests.

As a comparison, on the .NET project that I am currently working on, we can run roughly about 500 unit tests in less than 10 seconds (and that was not even measured on a high spec machine). If those were your figures, you would not be afraid to run these locally every so often.

Learn to manage the project structure.

You have divided the solution into 120 projects. That is by my standards a staggering amount of projects.

So if it makes sense to actually have that amount of projects (which I have a feeling it doesn't - but your question does not provide enough information to make a qualified judgement of this), you need to divide the projects into smaller components that can be build, versioned, and deployed separately. So when a developer runs unit the test suite, he/she only needs to run the tests relating to component he/she is working on currently. The build server should take care of verifying that everything integrates correctly.

But splitting up a project in multiple components build, versioned, and deployed separately requires in my experience a very mature development team, a team that is more mature than I get the feeling that your team is.

But at any rate, you need to do something about the project structure. Either split the projects into separate components, or start merging projects.

Ask yourself if you really need 120 projects?

p.s. You might want to check out NCrunch. It's a Visual Studio plug-in that runs your test automatically in the background.

Title: How do we make unit tests run fast?


问:
Tabs versus spaces—what is the proper indentation character for everything, in every situation, ever?
The coding standards for the code hosted in drupal.org suggest to use two spaces to indent the code; other sites suggest to use tabs to indent the code.

What is the proper indentation character for everything, and in every situation? Please explain the answer you give.

答:
Spaces or Tabs - What Atwood really says is pick one thing and be consistent in your project. The only holy grail of code formatting is to make certain that its consistent so that the psychopath who maintains your code after you doesn't feel compelled to remedy the situation permanently.

That said, if you're working in Python or any other language where whitespace is an actual programming construct I can't imagine using tabs.


问:
What should every programmer know about programming?
Please, stay on technical issues, avoid behavior, cultural, career or political issues.

答:
Every programmer should know that he's putting assumptions in code all the time, e.g. "this number will be positive and finite", "this code will be able to connect to the server all the time within a blink of an eye".

And he should know that he should prepare for when those assumptions break.


答:
when your focus is on getting it done and getting it right, then it no longer matters whether the solution was your idea or not

at that point, your ego has left the building - and you are much more likely to learn something!


问:
Name for a tree where nodes hold a reference to their parent as well as their children
I am creating a tree where some nodes have a reference to their parent as well as their children. It feels like a tree version of a doubly linked list.

Is there a name for this?

答:
I don't know if there is a official name for it.

I would call it "tree with back pointer", or more specific "B-Tree with back pointer" ...

I found also the names Doubly-Linked Lists and Tree Node List. According to it, you could call Doubly-Linked Tree


问:
Architecting persistence (and other internal systems). Interfaces, composition, pure inheritance or centralization?
Suppose that you need to implement persistence, I think that you're generally limited to four options (correct me if I'm wrong, please)

Each persistant class:

Should implement an interface (IPersistent)

Contains a 'persist-me' object that is a specialized object (or class) that's made only to be used with the class that contains it.

Inherit from Persistent (a base class)

Or you can create a gigantic class (or package) called Database and make your persistence logic there.

What are the advantages and problems that can come from each of one? In a small (5kloc) and algorithmically (or  organisationally) simple app what is probably the best option?

答:
My first answer would be to save yourself the trouble and find a good data mapper ORM and focus on your business objects and logic, and let the ORM worry about persistence. The "data mapper" part is important here, because it alows you to have a domain layer which is completely separated from the persistence. So you'll have just simple classes without any persistence leaking in in the form of a base class, or interface or anything.

Now if you still want to try and build your own, this is how I'd do it:

This is heavily based on how the Doctrine2 ORM for PHP works:
The entry point for whatever code is using the ORM is your EntityManager, this is what you use to fetch and save your entities/domain objects. Then you have your data mapping layer, which should be able to be injected with mapping info (in xml or whatever), so it can figure out how to connect tables to classes and columns to properties, and how to deal with relationships.
Underneath all that, you have the actuall db access layer, which has to be flexible enough to deal with different databases. So when you ask for an entity from the EM, it will go through the mapper layer to find out what table to use, then to the dbal to fetch the data, and again through the mapper to map the data to an object. When you save an entity, the EM will use the mapping layer to find out the table/columns from the object/properties, and send the data to the dbal to actually save it.
Doctrine also uses auto-generated proxy classes for your objects. These proxies are injected with the EM to allow for loading of relationships.

This is a largely simplified version of things, and I'm sure someone has already done it in whatever language you're using. Doctrine for instance also has an ODM, for MongoDB (this is still beta).

EDIT:
You'll notice that I never mentioned anything like a base class or an interface that you'd use in your domain objects. They should be absolutely stupid when it comes to persistence, and the only "leakage" from the persistence to the domain layers happens in the proxy classes, which should look exactly like your domain objects to the application that's using them. This way your domain code stays clean and simple.


问:
Hacking Python "Requests" library.. How to start as an intermediate pythonista?
My question is conceptual, yet extremely vital for me.

I'm an intermediate Python developer. I know fair enough about it, and actually use it on a daily basis. Now, I would like to transform my capabilities from a library/API user to "designer" one.

I found that "Python Requests" library by Kenneth Reitz is an extremely good place to explore Python patterns and idioms. I need to learn to split a big problem into its essential Modules, define required classes/functions, and use them to work together.

I lack such a discipline, and I'm starving to gain this knowledge.

How should I dive into this library? It is big, with many modules (with logical names, of course), classes, and methods. I started to read the code, but I don't know where I am and what this portion of code is designed for.

答:
Get focused. Think of a question, then try to answer it. For example:

how does the library encode a dictionary into a form suitable to submit to a web server?

how does the library store authentication information?

how does the library convert a dictionary of headers into a web request?

how does the library implement logging?

... And so on.


答:
You start by following common tasks; find out how a regular GET request is handled, for example, or how the .json() method works once the data has arrived.

It does help to be familiar with the subject matter modelled by a body of code. Knowing how HTTP works from a client perspective helps; if you don't know what a 302 redirect is or how HTTP uses headers to exchange info, you will have a harder time understanding all that goes on.

Luckily the requests library does keep it's concerns nice and separated; you don't need to dive all the way into the urllib3 library innards for example.

Title: Hacking Python "Requests" library.. How to start as an intermediate pythonista?

Body:
My question is conceptual, yet extremely vital for me.

I'm an intermediate Python developer. I know fair enough about it, and actually use it on a daily basis. Now, I would like to transform my capabilities from a library/API user to "designer" one.

I found that "Python Requests" library by Kenneth Reitz is an extremely good place to explore Python patterns and idioms. I need to learn to split a big problem into its essential Modules, define required classes/functions, and use them to work together.

I lack such a discipline, and I'm starving to gain this knowledge.

How should I dive into this library? It is big, with many modules (with logical names, of course), classes, and methods. I started to read the code, but I don't know where I am and what this portion of code is designed for.


问:
Splitting documentation between docstrings and sphinx
I'm using a combination of docstrings and Sphinx to document my Python programs.  With Sphinx's autodoc, I can write a lot of documentation into the source as docstrings.  Is there any standard for how much documentation to put in each?

For example,

case 1 is index.rst

```<code>.. automodule:: foo
   :members:
</code>```

and foo.py

```<code>"""documentation"""

class Foo:
"""documentation""""

    def bar(self, baz):
        """more documentation"""
</code>```

case 2 index.rst

```<code>.. automodule:: foo
</code>```

and foo.py

```<code>"""documentation

.. autoclass:: Foo

"""

class Foo:
"""documentation

.. automethod:: bar

""""

    def bar(self, baz):
        """more documentation"""
</code>```

case 3 index.rst

```<code>.. automodule:: foo
   .. autoclass:: Foo
      .. automethod:: bar
</code>```

and foo.py

```<code>"""documentation

Classes:
    Foo

"""

class Foo:
"""documentation

Methods:
    bar

""""

    def bar(self, baz):
        """more documentation"""
</code>```

The 1 is easy to maintain, but the docstrings in the source lacks information.
The 2 puts that information into the source docstrings, but it's a bit harder to maintain and is a bit harder to read.
The 3 is easy to read and has complete documentation in the source code, but is very hard to maintain, and when you use sphinx to generate the documentation, some information is repeated.  (It'll look something like:

```<code>foo:
    documentation
    classes:
        Foo

    Foo:
        documentation
        methods:
            baz
        baz
</code>```

答:
The Python standard library keeps all of its Sphinx documentation separate, forgoing the autodoc feature entirely, and uses much more brief docstrings in the source files.  Thus, the following practice is perhaps best:

Docstrings in source code should not use Sphinx directives, using only light reST formatting for readability.  Because (good) Python code is quite clear on its own, keep docstrings informative but simple.  These should be primarily for project devs and anyone digging into the source code.

Actual end-user (or developers for libraries) documentation should be taken care of by Sphinx.  Use autodoc when suitable, adhering to the above.


问:
Should I return from a function early or use an if statement?
I've often written this sort of function in both formats, and I was wondering if one format is preferred over another, and why.

public void SomeFunction(bool someCondition)
{
    if (someCondition)
    {
        // Do Something
    }
}
```

<p>or</p>

<pre class="lang-java prettyprint-override"><code>public void SomeFunction(bool someCondition)
{
    if (!someCondition)
        return;

    // Do Something
}
</code>```

I usually code with the first one since that is the way my brain works while coding, although I think I prefer the 2nd one since it takes care of any error handling right away and I find it easier to read

答:
I use early returns almost exclusively these days, to an extreme. I write this

self = [super init];

if (self != nil)
{
    // your code here
}

return self;
```

<p>as</p>

<pre class="lang-c prettyprint-override"><code>self = [super init];
if (!self)
    return;

// your code here

return self;
</code>```

but it really doesn't matter. If you have more than one or two levels of nesting in your functions, they need to be busted up.

Body:
I use early returns almost exclusively these days, to an extreme. I write this

self = [super init];

if (self != nil)
{
    // your code here
}

return self;

as

self = [super init];
if (!self)
    return;

// your code here

return self;

but it really doesn't matter. If you have more than one or two levels of nesting in your functions, they need to be busted up.


答:
You just need to simulate the pouring, something like

```<code>void pour(double glasses[10], int glass, double quantity)
{
    glasses[glass] += quantity;
    if(glasses[glass] > 1.0)
    {
         double extra = glasses[glass] - 1.0;
         pour( glasses, left_glass(glass), extra / 2 );
         pour( glasses, right_glass(glass), extra / 2 );
         glasses[glass] = 1.0;
    }
}

double getWaterInGlass(int N, int X)
{
    double glasses[10] = {0,0,0,0,0,0};
    pour(glasses, 0, X);
    return glasses[N];
}
</code>```

As it stands, this is not a tree. Because different glasses pour into the same glasses, that prevents it from being a tree.

Title: Champaign Fountain Puzzle

Body:
Empty glasses of water are arranged in the following order:

When you pour liquid into the 1st glass if it's full, then the extra liquid would be flown into the glasses 2 and 3 in equal quantities. When glass 2 is full, the extra liquid would be flown into 4 and 5 and so on.

Given an N liters of liquid and maximum capacity of each glass is 1 liter, give the amount of liquid  present in any glass if you empty N liters of liquid by pouring into glass by filling out the function getWaterInBucket(int N, int X) where X is the glass number. So for example if I want have 4 liters at the beginning and I want to find the water in glass 3 the function is getWaterInBucket(4, 3)

How do I solve this programmatically? I tried to find a math solution using Pascal's triangle. This did not work. I considered it to be a tree so I can add a parameter like this getWaterInBucket(BTree root, int N, int X) and then try some recursive solution for each level, but parameters are not allowed in this problem. Is there something obvious, some trick?

Body:
You just need to simulate the pouring, something like

void pour(double glasses[10], int glass, double quantity)
{
    glasses[glass] += quantity;
    if(glasses[glass] > 1.0)
    {
         double extra = glasses[glass] - 1.0;
         pour( glasses, left_glass(glass), extra / 2 );
         pour( glasses, right_glass(glass), extra / 2 );
         glasses[glass] = 1.0;
    }
}

double getWaterInGlass(int N, int X)
{
    double glasses[10] = {0,0,0,0,0,0};
    pour(glasses, 0, X);
    return glasses[N];
}

As it stands, this is not a tree. Because different glasses pour into the same glasses, that prevents it from being a tree.


问:
Why do game developers prefer Windows?
Is it that DirectX is easier or better than OpenGL, even if OpenGL is cross-platform? Why do  we not see real powerful games for Linux like there are for Windows?</p>

答:
So I've gone over all these answers, and as a game developer who has code on console games that have been on Walmart shelves, I have a very different answer.

Distribution.

See, if you want to be on a Nintendo console, you have to get Nintendo's permission, buy from Nintendo's factories, pay Nintendo's overheads, negotiate with Walmart, deal with warehousing, you need money up front to manufacture, to print boxes, to ship, to do all the insurance, et cetera.

If you want onto the XBox, sure there's XBLA, but you still need Microsoft's blessing, you have to wait your turn in line, it's tens of thousands of dollars just to release a patch, etc.

On iOS, you still need Apple's okay, and they can (and do) capriciously pull you.

On Steam, you still need Valve's permission or greenlight, and lots of money.

.

On Windows?  You set up a website and a download button.

.

I'm not saying the other platforms aren't valuable.  But there is so *much* horrible stuff going on when you're trying to develop a game, that to me, the promise of being able to just slap a binary on a site and focus on the work - at least to get started - really lowers a lot of potential failure barriers.

"We can do the XBLA port later, when things are stable" mentality.

And to a lesser extent sure this is fine for Linux too, and if seven customers is good, you can start there.

But Windows has three massive advantages: genuinely open development, genuinely open deployment, and a very large, very active customer base which is interested in quirky stuff.

It's hard to imagine where else I'd rather start.

Title: Why do game developers prefer Windows?


答:
Thinking about this as a tree problem is a red-herring, it's really a directed graph.  But forget all about that.

Think of a glass anywhere below the top one.  It will have one or two glasses above it that can overflow into it.  With the appropriate choice of coordinate system (don't worry, see the end) we can write a function to get the "parent" glasses for any given glass.

Now we can think about an algorithm to get the amount of liquid poured into a glass, irrespective of overflow from that glass.  The answer is however much liquid is poured into each parent minus the amount stored in each parent glass, divided by 2.  Just sum that for all parents.  Writing this as a python fragment of the body of an amount_poured_into() function:

```<code># p is coords of the current glass
amount_in = 0
for pp in parents(p):
    amount_in += max((amount_poured_into(total, pp) - 1.0)/2, 0)
</code>```

The max() is to ensure we don't get a negative amount of overflow.

We are almost finished!  We choose a coordinate system with 'y' down the page, first row glasses is 0, second row is 1, etc.  The 'x' coordinates have a zero under the top row glass and the second row have x coordinates of -1 and +1, third row -2, 0, +2, and so on.  The important point is that the left or right-most glass in level y will have abs(x) = y.

Wrapping all that up into python (2.x), we have:

```<code>def parents(p):
    """Get parents of glass at p"""

    (x, y) = p
    py = y - 1          # parent y
    ppx = x + 1         # right parent x
    pmx = x - 1         # left parent x

    if abs(ppx) > py:
        return ((pmx,py),)
    if abs(pmx) > py:
        return ((ppx,py),)
    return ((pmx,py), (ppx,py))

def amount_poured_into(total, p):
    """Amount of fluid poured into glass 'p'"""

    (x, y) = p
    if y == 0:    # ie, is this the top glass?
        return total

    amount_in = 0
    for pp in parents(p):
        amount_in += max((amount_poured_into(total, pp) - 1.0)/2, 0)

    return amount_in

def amount_in(total, p):
    """Amount of fluid left in glass p"""

    return min(amount_poured_into(total, p), 1)
</code>```

So to get the amount actually in a glass at p, use amount_in(total, p).

It's not clear from the OP, but the bit about "you cant add parameters" may mean the original question has to be answered in terms of the glass numbers shown.  This is solved by writing a mapping function from the show glass numbers to the internal coordinate system used above.  It's fiddly, but either an iterative or mathematical solution can be used.  An easy to understand iterative function:

```<code>def p_from_n(n):
    """Get internal coords from glass 'number'"""

    for (y, width) in enumerate(xrange(1, n+1)):
        if n > width:
            n -= width
        else:
            x = -y + 2*(n-1)
            return (x, y)
</code>```

Now just rewrite the amount_in() function above to accept a glass number:

```<code>def amount_in(total, n):
    """Amount of fluid left in glass number n"""

    p = p_from_n(n)
    return min(amount_poured_into(total, p), 1)
</code>```

Title: Champaign Fountain Puzzle

Body:
Thinking about this as a tree problem is a red-herring, it's really a directed graph.  But forget all about that.

Think of a glass anywhere below the top one.  It will have one or two glasses above it that can overflow into it.  With the appropriate choice of coordinate system (don't worry, see the end) we can write a function to get the "parent" glasses for any given glass.

Now we can think about an algorithm to get the amount of liquid poured into a glass, irrespective of overflow from that glass.  The answer is however much liquid is poured into each parent minus the amount stored in each parent glass, divided by 2.  Just sum that for all parents.  Writing this as a python fragment of the body of an amount_poured_into() function:

# p is coords of the current glass
amount_in = 0
for pp in parents(p):
    amount_in += max((amount_poured_into(total, pp) - 1.0)/2, 0)

The max() is to ensure we don't get a negative amount of overflow.

We are almost finished!  We choose a coordinate system with 'y' down the page, first row glasses is 0, second row is 1, etc.  The 'x' coordinates have a zero under the top row glass and the second row have x coordinates of -1 and +1, third row -2, 0, +2, and so on.  The important point is that the left or right-most glass in level y will have abs(x) = y.

Wrapping all that up into python (2.x), we have:

def parents(p):
    """Get parents of glass at p"""

    (x, y) = p
    py = y - 1          # parent y
    ppx = x + 1         # right parent x
    pmx = x - 1         # left parent x

    if abs(ppx) > py:
        return ((pmx,py),)
    if abs(pmx) > py:
        return ((ppx,py),)
    return ((pmx,py), (ppx,py))

def amount_poured_into(total, p):
    """Amount of fluid poured into glass 'p'"""

    (x, y) = p
    if y == 0:    # ie, is this the top glass?
        return total

    amount_in = 0
    for pp in parents(p):
        amount_in += max((amount_poured_into(total, pp) - 1.0)/2, 0)

    return amount_in

def amount_in(total, p):
    """Amount of fluid left in glass p"""

    return min(amount_poured_into(total, p), 1)

So to get the amount actually in a glass at p, use amount_in(total, p).

It's not clear from the OP, but the bit about "you cant add parameters" may mean the original question has to be answered in terms of the glass numbers shown.  This is solved by writing a mapping function from the show glass numbers to the internal coordinate system used above.  It's fiddly, but either an iterative or mathematical solution can be used.  An easy to understand iterative function:

def p_from_n(n):
    """Get internal coords from glass 'number'"""

    for (y, width) in enumerate(xrange(1, n+1)):
        if n > width:
            n -= width
        else:
            x = -y + 2*(n-1)
            return (x, y)

Now just rewrite the amount_in() function above to accept a glass number:

def amount_in(total, n):
    """Amount of fluid left in glass number n"""

    p = p_from_n(n)
    return min(amount_poured_into(total, p), 1)