问:
Repetitive code over multiple classes. Best way to centralize code?
I'm working with 3D arrays that I flatten into a 1D arrays with the following index calculation:

```<code>index = x + WIDTH * (y + DEPTH * z)
</code>```

I'm starting to notice that I'm using the calculation all over the place in different classes and am wondering if using a macro would be a good solution?

I've also thought about creating a static method in a class and then using that in all these classes, but adding that dependency to all those classes doesn't sit well with me.

Is there a good standard practice or pattern to follow in this case?

答:
Given that your question is tagged c++, do not use macros! They should only be used for include guards and a few other esoteric uses.

The proper solution here is a standalone inline function. Assuming that WIDTH and DEPTH are constants that are visible to the function, here is its definition:

```<code>inline int f(int x, int y, int z) {
  return x + WIDTH * (y + DEPTH * z);
}
</code>```

Place this function in a header accessible by all of the classes that use it. I would put it in a header with utility functions for whatever namespace it is part of. If such a header does not exist, this might be a good time to create it. Regardless, it must go in a header so the function definition is visible to each compilation unit that includes a call to the function: in other words, the compiler has to be able to see the function to put its contents inline with the calling code.

This solution centralizes the code, providing all of the benefits of a function. Since it is inline, it removes the function call overhead.

Most importantly, it provides well-defined semantics about its parameters:

```<code>index = f(x++, ++y, z);
</code>```

This is well-defined: we can look up how this works in the C++ standard. If it were a macro, it may work as expected in a simple case but it can break down in more complex macros (beyond the scope of this question).

Body:
Given that your question is tagged c++, do not use macros! They should only be used for include guards and a few other esoteric uses.

The proper solution here is a standalone inline function. Assuming that WIDTH and DEPTH are constants that are visible to the function, here is its definition:

inline int f(int x, int y, int z) {
  return x + WIDTH * (y + DEPTH * z);
}

Place this function in a header accessible by all of the classes that use it. I would put it in a header with utility functions for whatever namespace it is part of. If such a header does not exist, this might be a good time to create it. Regardless, it must go in a header so the function definition is visible to each compilation unit that includes a call to the function: in other words, the compiler has to be able to see the function to put its contents inline with the calling code.

This solution centralizes the code, providing all of the benefits of a function. Since it is inline, it removes the function call overhead.

Most importantly, it provides well-defined semantics about its parameters:

index = f(x++, ++y, z);

This is well-defined: we can look up how this works in the C++ standard. If it were a macro, it may work as expected in a simple case but it can break down in more complex macros (beyond the scope of this question).


问:
How to deal with libraries that use different naming conventions?
I'm working on a personal project that uses a few different libraries, such as GLFW, glm and Bullet. How are different naming conventions usually dealt with in this context?

Working with C++ I always used the same convention the standard library uses (all lowercase, underline as word separator, not ::getSize(), just ::size(), etc.) Personal preference aside, this works for me. glm uses a naming convention that is close to mine, but they use camelCase, even for types (there's glm::vec3 for instance.) Bullet has things like btScalar, or btVector (prefixing everything with bt but the type has an uppercase first letter.) GLFW prefixes everything "GLFW" or "glfw".

Usually, things like glm don't bother me that much because the "glm::" prefix indicates clearly that it's part of a library, but bullet has no namespace (I'm not sure but I believe it's a C library.) My first instinct was to typedef the differences away, that is useless of course since I can't rename method names (and good thing I didn't, since there would be A LOT typedefs)

Should I just accept the differences? Maybe abstract everything behind another class? How do you usually deal with this?

答:
Accept and use each library's preferred naming preferences.

To do anything else would add a layer of abstraction between the true names and the ones you've picked to enforce your preferred style.  That makes it somewhat of a nightmare to look up anything (including the library documentation for a method or function), unless the only real difference is capitalization (a distinction which really ought to be irrelevant anyway; just use the casing that the library has already established).

ContentLicense: CC BY-SA 3.0


答:
What's worse than dealing with this is dealing with projects that have dealt with it in a cute way.

If I know something is supposed to be in a namespace, then at least I know that. I know it in all projects whenever I work with it. It might work great for you but anyone else who has used that library will be unnecessarily confused. And probably really annoyed.

My first instinct was to typedef the differences away

Don't do this without a really, really good reason.

Title: How to deal with libraries that use different naming conventions?

Body:
I'm working on a personal project that uses a few different libraries, such as GLFW, glm and Bullet. How are different naming conventions usually dealt with in this context?

Working with C++ I always used the same convention the standard library uses (all lowercase, underline as word separator, not ::getSize(), just ::size(), etc.) Personal preference aside, this works for me. glm uses a naming convention that is close to mine, but they use camelCase, even for types (there's glm::vec3 for instance.) Bullet has things like btScalar, or btVector (prefixing everything with bt but the type has an uppercase first letter.) GLFW prefixes everything "GLFW" or "glfw".

Usually, things like glm don't bother me that much because the "glm::" prefix indicates clearly that it's part of a library, but bullet has no namespace (I'm not sure but I believe it's a C library.) My first instinct was to typedef the differences away, that is useless of course since I can't rename method names (and good thing I didn't, since there would be A LOT typedefs)

Should I just accept the differences? Maybe abstract everything behind another class? How do you usually deal with this?


问:
Is it OK to deprecate methods that need to be public due to the packaging model but are not to be used outside the codebase in Java?
I am currently working on a semi-large project that has several packages. There are 3 main packages, a "client" package, a "server" package and a "common" package. There are two jars, one for the client and one for the server each containing their own package and the common package. As is implied by the package names, there is a server and a client. The clients connect to the server and exchange messages. These messages take the form of a serilized subclass of Message. All subclasses of Message are in the common package. When a message is recieved, a method is called on the message class to handle the message. There is one subclass of Message that is responsible for sending the name of the clients to the servers. In the method that is called when the server recieves it, it then calls a setName() method on the object representing the connection between the server and client.

For obvious reasons, I don't want external programmers calling the setName() method however it has to be public because the message and the class to represent a connection are in different packages. I would like to know if it is good form to deprecate the method with a reason along the lines of "for internal use only", and if not, what is a better option?

答:
You may need to consider some re-structuring.

So it looks like we have [Client] -message-> [Server] and Message (and all derivatives of) are stored in the [Common] package.  In your comment you explain that the Message contains a function which gets called by either the Server or the Client depending on which received the message, which in turn calls a method on either the client or the server.

In my eyes, and just from what I can gather from your explanations, is that you should consider writing the message handling mechanisms right into either the client and server portions instead of having the message handling included as part of the message class.

ContentLicense: CC BY-SA 4.0


问:
How to approach refactoring an existing web application?
I've been reading and thinking a lot lately, and I've come to the conclusion that I maybe I should rethink my web development strategy. I'm doing a lot of on-the-fly programming, and in the 2 years I've been working on a PHP web application, what might have begun as a little tool became quite the big project. But there is a ton of legacy code from me and my predecessor, snippet of code that might have make sense at the time, but now I'm questioning the usefulness of said code in the form it is actually. Furthermore so such things like unit testing and Test-Driven Development wasn't in my scope until quite recently.

So how would you approach refactoring the web application? What would be the things I should look for and in which order? What about browser game versus functional web app? Would there be then difference in the approach?

答:
Yes - Web Applications are different to Web Sites

I would treat them separately. If you have one part of your site that is simply a collection of documents (that look the same to anonymous users and logged-in users alike) - then the best method of structuring it is very different from a web app that serves dynamically different pages to each user. Split those two parts of the site up into two apps / components and tackle each part differently.

Start Using Version Control

Once your code is under version control, you can go through and, confidently, remove all unnecessary code that you had previously kept 'just in case' etc. I don't know how I survived without version control.

Reduce the infinities

If four different urls all point to the same resource, then the problem is much bigger.
You end up dealing with an infinite amount of urls. As soon as you can, ensure that you have an URL Normalisation policy in place. Once that is done, you can start attaching semantic meanings to URLs and be able to do reverse lookups from resource-to-url. This allows you to separate the 'web imprint' from the 'resources' of the site.

You have to ask yourself, "given an url what is its normalised form?". Once you have got this pinned down. Then then 50,0000+ urls on your site can be reduced to say, 2,000. which is a lot easier to comprehend and manage in your mind.

see: http://www.sugarrae.com/be-a-normalizer-a-c14n-exterminator/

Start by modelling 'what is', not 'what you want it be'

If you are tidying up a legacy site, that was not designed with best practice in mind from the start, then it is tempting to jump from 'a mess' to 'the ideal design'. I believe that you need to do it in at least two steps:  'mess' -> 'well modelled legacy code' -> 'ideal new code with added features'. Stop adding features. Concentrate on fixing the mess or encapsulating it behind an anti-corruption layer. Only then, can you start to change the design into something better.

See: http://www.joelonsoftware.com/articles/fog0000000069.html

See: http://www.laputan.org/mud/

Putting it under test is a good idea.

Create a test suite / framework and start adding tests. But, its quite tricky to test some legacy code. So, don't get too hung up over it. As long as you have the framework there,
you can add tests little by little.

See: http://www.simpletest.org/en/web_tester_documentation.html

Have courage in your convictions

Most of the literature on best practices of software development is desktop-centric/Enterprise App Centric. While your site is in a mess you read these books and you can be in awe of the wisdom that exudes from them. But, do not forget that most of this best practice has been accrued in times before the web/SEO became important. You know a lot about the modern web, more than is mentioned in classic books like POEA, Gof etc. There is a lot to take from them, but do not completely discard your own experience and knowledge.

I could go on. But those are some things that I have picked when refactoring an old legacy site into a shiny new one.

ContentLicense: CC BY-SA 2.5


问:
C# Subject Observer Architecture question
I'm making a C# application using the Subject Observer design pattern in a slightly different way.

I am passing the Provider Class, implementing IObservable (this has the OnNext() method that notifies all classes that subscribe to it), to the Observer Class constructor, implementing IObserver (this acts on the OnNext() method posted by the provider class).

That way every single Observer class has the capability of executing the OnNext() method notifying other classes of changes as well.

So a very simplistic look of my sample project goes like:

```<code>ProviderClass provider;
ObserverClass observer1;
ObserverClass observer2;
.......... observer3;
.......... observer'n';

main()
{
    provider = new ProviderClass();
    observer1 = new ObserverClass(provider);
    observer2 = new ObserverClass(provider);
    observer3 = new ObserverClass(provider);
    provider.subscribe(observer1);
    provider.subscribe(observer2);
    provider.subscribe(observer3);
    ............
    ............
    // arbitrary code happens here
    ............
    ............
    // later in code observer1,2,3 can each post OnNext() methods
    // that each other observer class will also catch since they all have
    //, so with some
    // filtering of objects being passed in each OnNext() method
    // I think this is a great way of passing data from one class to
    // another.
}

class ObserverClass : IObserver<Object>
{
    ProviderClass copyInstance;

    // Now the observer class can notify other classes using OnNext()
    public ObserverClass(Provider copy)
    {
        copyInstance = copy;
    }

    public virtual Override OnNext(....)
    {
        // perform specific action
    }

    public void notifyClasses(...)
    {
       copyInstance.OnNext(...);
    }
}
</code>```

Are there any drawbacks to modifying the Subject Observer design pattern this way?

答:
I pretty much agree with what @radarbob answer states. For some time I wondered as well, about how one would implement such a design pattern in "real life". It's fine when reading about it to understand the design pattern theoretically, but actually applying it is different.

If you have access to Pluralsight I would highly recommend the following video (worth signing up with a trial for):

Tactical design patterns in .NET

I think the issues that he addresses and topics he talk about (specifically about the observer pattern as well), would benefit you.


问:
Philosophy behind Undefined Behavior
C\C++ specifications leave out a large number of behaviors open for compilers to implement in their own way. There are a number of questions that always keep getting asked here about the same and we have some excellent posts about it:

https://stackoverflow.com/questions/367633/what-are-all-the-common-undefined-behaviour-that-a-c-programmer-should-know-abo

https://stackoverflow.com/questions/4105120/what-is-undefined-behavior

https://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points

My question is not about what undefined behavior is, or is it really bad. I do know the perils and most of the relevant undefined behavior quotes from the standard, so please refrain from posting answers about how bad it is. This question is about the philosophy behind leaving out so many behaviors open for compiler implementation.

I read an excellent blog post that states that performance is the main reason. I was wondering if performance is the only criteria for allowing it, or are there any other factors which influence the decision to leaving things open for compiler implementation?

If you have any examples to cite about how a particular undefined behavior provides sufficient room for compiler to optimize, please list them. If you know of any other factors other than performance, please back your answer with sufficient detail.

If you do not understand the question or do not have sufficient evidences/sources to back your answer, please do not post broadly speculating answers.

答:
Historically, Undefined Behavior had two primary purposes:

To avoid requiring compiler authors to generate code to handle conditions which were never supposed to occur.

To allow for the possibility that in the absence of code to explicitly handle such conditions, implementations may have various kinds of "natural" behaviors which would, in some cases, be useful.

As a simple example, on some hardware platforms, attempting to add together two positive signed integers whose sum is too large to fit in a signed integer will yield a particular negative signed integer.  On other implementations it will trigger a processor trap.  For the C standard to mandate either behavior would require that compilers for platforms whose natural behavior differed from the standard would have to generate extra code to yield the correct behavior--code which may be more expensive than the code to do the actual addition.  Worse, it would mean that programmers who wanted the "natural" behavior would have to add even more extra code to achieve it (and that extra code would again be more expensive than the addition).

Unfortunately, some compiler authors have taken the philosophy that compilers should go out of their way to find conditions that would evoke Undefined Behavior and, presuming that such situations may never occur, draw extended inferences from that.  Thus, on a system with 32-bit int, given code like:

```<code>uint32_t foo(uint16_t q, int *p)
{
  if (q > 46340)
    *p++;
  return q*q;
}
</code>```

the C standard would allow the compiler to say that if q is 46341 or larger, the expression q*q will yield a result too large to fit in an int, consequently causing Undefined Behavior, and as a result the compiler would be entitled to assume that can't happen and thus would not be required to increment *p if it does.  If the calling code uses *p as an indicator that it should discard the results of the computation, the effect of the optimization may be to take code which would have yielded sensible results on systems that perform in almost any imaginable way with integer overflow (trapping may be ugly, but would at least be sensible), and turned it into code which may behave nonsensically.

OwnerDisplayName: Als

Title: Philosophy behind Undefined Behavior

Body:
Historically, Undefined Behavior had two primary purposes:

To avoid requiring compiler authors to generate code to handle conditions which were never supposed to occur.

To allow for the possibility that in the absence of code to explicitly handle such conditions, implementations may have various kinds of "natural" behaviors which would, in some cases, be useful.

As a simple example, on some hardware platforms, attempting to add together two positive signed integers whose sum is too large to fit in a signed integer will yield a particular negative signed integer.  On other implementations it will trigger a processor trap.  For the C standard to mandate either behavior would require that compilers for platforms whose natural behavior differed from the standard would have to generate extra code to yield the correct behavior--code which may be more expensive than the code to do the actual addition.  Worse, it would mean that programmers who wanted the "natural" behavior would have to add even more extra code to achieve it (and that extra code would again be more expensive than the addition).

Unfortunately, some compiler authors have taken the philosophy that compilers should go out of their way to find conditions that would evoke Undefined Behavior and, presuming that such situations may never occur, draw extended inferences from that.  Thus, on a system with 32-bit int, given code like:

uint32_t foo(uint16_t q, int *p)
{
  if (q > 46340)
    *p++;
  return q*q;
}

the C standard would allow the compiler to say that if q is 46341 or larger, the expression q*q will yield a result too large to fit in an int, consequently causing Undefined Behavior, and as a result the compiler would be entitled to assume that can't happen and thus would not be required to increment *p if it does.  If the calling code uses *p as an indicator that it should discard the results of the computation, the effect of the optimization may be to take code which would have yielded sensible results on systems that perform in almost any imaginable way with integer overflow (trapping may be ugly, but would at least be sensible), and turned it into code which may behave nonsensically.


答: Aside from using a free function, you may want to examine your class decomposition. If you have a lot of classes which have a 3D matrix flattened into a 1D, and they all access that matrix via this calculation, it's likely that they should all have a common class threeDinOneD which they use.

Title: Repetitive code over multiple classes. Best way to centralize code?

Body:
I'm working with 3D arrays that I flatten into a 1D arrays with the following index calculation:

```<code>index = x + WIDTH * (y + DEPTH * z)
</code>```

I'm starting to notice that I'm using the calculation all over the place in different classes and am wondering if using a macro would be a good solution?

I've also thought about creating a static method in a class and then using that in all these classes, but adding that dependency to all those classes doesn't sit well with me.

Is there a good standard practice or pattern to follow in this case?


问:
Having a production branch or using master?
I work on small team with other remote developers on a Rails application. We are starting to modify our git workflow. We have thought about a branching structure like below:

```<code>(dev) -> (qa) -> (stag) -> (master)
</code>```

But some of the developers thought it might be less confusing for new developers who might automatically push to production on master. They thought instead to have everyone work on master and create a separate branch for production.

```<code>(master) -> (qa) -> (stag) -> (prod)
</code>```

I was taught you want to keep master deployable and not use it as development and from previous places where I've worked master is always meant to be deployable for production.

What would be some of the disadvantages of using a branching structure where master is actively used for development and a separate prod branch is what you use for deployments?

答:
I prefer checks over conventions in this case. Every team contains member who are better at getting new features started and other people who are better at stabilizing things for a release.

If you lack the latter, then code reviews will help (often, the more disciplined people will want code reviews anyway).

That's why we configure our Git repo (we're using Gitlab) that only certain people can merge pull requests and each developer gets their own private fork of the main repo.

That solves two problems:

New developers can't change the wrong branch (since they can't push their work directly into the main repo). They might push to master on their own repo but that will be fixed when the pull request comes in.

Code conventions quickly spread through the team since each commit is checked by at least another person who brings their view and knowledge.

OwnerDisplayName: willyw

OwnerDisplayName: Aaron Digulla


答:
It all depends on the overall software development process. Configuration management and how a new version comes to be cannot be defined without knowing about the overall process.

There is the "agile" faction who would opt for a "always working first commit area". They would run automated build and test facilities constantly against that area and try to have a working system "at all times".

They would see the (master) -> (release) with maybe 1,2 intermediate steps organization as beneficial.

Then there is the more "classic" faction, which has a process driven by planning and planned integration steps towards milestones, where a "unit of work" release is a planned activity with requirements such as "only release when it is (unit) tested and supposed to fit to the next planned milestone". There, the planning comprises the versioning of "units of work" and typically they go to lengths to define how the next planned product release is supposed to look like in terms of features and fixes. For the sake of planning, they want to know that what a developer releases is "proper" and a conscious act of committing a unit of work.

That classic approach does not mean necessarily that there are longer times where there is no complete product build is available.

So the "classic" workflow would be: (dev) -> (unit) -> (integration) -> (test/qa) -> (production).

The role of the integrator is to "accept/buy" released units or reject them if they do not fit the needs of the next upcoming release.

It is, on a side note also possible to mix those 2 basic approaches in opportune ways.

From my experience (which was mostly in the area of using the "classic" approach), the "classic" approach worked decently well in projects from about 4-50 people in a team.

Title: Having a production branch or using master?

Body:
I work on small team with other remote developers on a Rails application. We are starting to modify our git workflow. We have thought about a branching structure like below:

```<code>(dev) -> (qa) -> (stag) -> (master)
</code>```

But some of the developers thought it might be less confusing for new developers who might automatically push to production on master. They thought instead to have everyone work on master and create a separate branch for production.

```<code>(master) -> (qa) -> (stag) -> (prod)
</code>```

I was taught you want to keep master deployable and not use it as development and from previous places where I've worked master is always meant to be deployable for production.

What would be some of the disadvantages of using a branching structure where master is actively used for development and a separate prod branch is what you use for deployments?


答:
There are neither any advantages nor disadvantages to this approach.  The reason I say this is simple:  to Git, it makes no difference if you develop from master or release from master.  You don't even need to release branches; you could tag an arbitrary commit and release that, instead.

The real trouble here is one of process and procedure.  The more senior devs that are concerned that doing it in one way will confuse the newer devs need to be prepared to invest the time to explain what the  release model is and why it's that way.

So long as everyone understands that master is for development, and some other arbitrary branch is for releases, and the work to  maintain this is done, then there shouldn't be any problems with this approach.

OwnerDisplayName: Makoto


答:
I can see your dilemma. I had it too, until I unlearned what I always assumed about master.

I was taught you want to keep master deployable and not use it as development and from previous places where I've worked master is always meant to be deployable for production.

From Git's documentation / book - Git branching

The “master” branch in Git is not a special branch. It is exactly like any other branch. The only reason nearly every repository has one is that the git init command creates it by default and most people don’t bother to change it.

So, if you have a preferred workflow, and it is difficult to work with it because different developers on the team have different ideas about master. You might even consider renaming master to say prod and use a workflow like below -

```<code>(dev) -> (qa) -> (stag) -> (prod)
</code>```

Here is how you change the master branch name.

I am NOT saying that you must change the master branch name. But if you have a preferred workflow and it helps to change the master branch name, do it by all means :-)

Body:
I can see your dilemma. I had it too, until I unlearned what I always assumed about master.

I was taught you want to keep master deployable and not use it as development and from previous places where I've worked master is always meant to be deployable for production.

From Git's documentation / book - Git branching

The “master” branch in Git is not a special branch. It is exactly like any other branch. The only reason nearly every repository has one is that the git init command creates it by default and most people don’t bother to change it.

So, if you have a preferred workflow, and it is difficult to work with it because different developers on the team have different ideas about master. You might even consider renaming master to say prod and use a workflow like below -

(dev) -> (qa) -> (stag) -> (prod)

Here is how you change the master branch name.

I am NOT saying that you must change the master branch name. But if you have a preferred workflow and it helps to change the master branch name, do it by all means :-)

OwnerDisplayName: Sumeet Pareek


问:
How much should I be using 'let' vs 'const' in ES6?
I've been writing a lot of ES6 code for io.js recently. There isn't much code in the wild to learn from, so I feel like I'm defining my own conventions as I go.

My question is about when to use const vs let.

I've been applying this rule: If possible, use const. Only use let if you know its value needs to change. (You can always go back and change a const to a let if it later turns out you need to change its value.)

The main reason for this rule is it's easy to apply consistently. There are no grey areas.

The thing is, when I apply this rule, in practice 95% of my declarations are const. And this looks weird to me. I'm only using let for things like i in a for loop, or occasionally for things like accumulated Fibonacci totals (which doesn't come up much in real life). I was surprised by this – it turns out 95% of the 'variables' in my ES5 code to date were for values that do not vary. But seeing const all over my code feels wrong somehow.

So my question is: is it OK to be using const this much? Should I really be doing things like const foo = function () {...};?

Or should I reserve const for those kind of situations where you're hard-coding a literal at the top of a module – the kind you do in full caps, like const MARGIN_WIDTH = 410;?

答:
JavaScript is a bit special in that variables can be functions and such, but consider in C#, Java or another similar C style language:

```<code>const public void DoSomething()
</code>```

The const is odd, and that's because method declarations in these languages cannot change, once they're compiled into something else, that is what they do, no matter what (ignoring some horrible hacks that may exist).

Why should JavaScript be any different? So it isn't compiled, but that doesn't mean we should throw away the safety that compilers can provide. Using the const keyword gives us more safety, which will surely lead to more robust applications.

Body:
JavaScript is a bit special in that variables can be functions and such, but consider in C#, Java or another similar C style language:

const public void DoSomething()

The const is odd, and that's because method declarations in these languages cannot change, once they're compiled into something else, that is what they do, no matter what (ignoring some horrible hacks that may exist).

Why should JavaScript be any different? So it isn't compiled, but that doesn't mean we should throw away the safety that compilers can provide. Using the const keyword gives us more safety, which will surely lead to more robust applications.


问:
"Comments are a code smell"
A coworker of mine believes that any use of in-code comments (ie, not javadoc style method or class comments) is a code smell.  What do you think?

答:
Categorically defining a method or process as a "code smell" is a "zealotry smell".  The term is becoming the new "considered harmful".

Please remember that all of these sort of things are supposed to be guidelines.

Many of the other answers give good advice as to when comments are warranted.

Personally I use very few comments. Explain the purpose of non-obvious processes and leave the occasional death-threat to anyone that might be considering altering things on their own that required weeks of tuning.

Refactoring everything until a kindergartner can understand it is likely not an efficient use of your time, and probably will not perform as well as a more concise version.

Comments don't affect run time,so the only negative issue to consider is the maintenance.

Title: "Comments are a code smell"


问:
Practical Meta Programming System (MPS)
This is in regards to Meta Programming System or MPS by JetBrains. Thus far, from my efforts to learn how to use MPS, I have only learned its basic purpose and that it is very complex.

Is MPS worth learning?

Is there anyone who already effectively uses MPS to create their own languages and editors for those languages and uses these created editors as their primary way of programming? If so, what types of programs have they made with this?

What are the advantages and disadvantages of working with MPS?

答:
I found the metaprogramming facilities in Boo and Ruby to be quite useful for building domain specific languages. Ayende Rahien's Domain Specific Languages in Boo was helpful when I was building a DSL for test automation. It may not directly apply to JetBrain's system, but it should be helpful for understanding the potential of a flexible language infrastructure.

Java itself has a fair amount of boilerplate that's required in order to get anything useful done. DSLs and code generation tools can help isolate some of that cruft, if they are well-designed. I don't know Jetbrains tool well enough to tell you if it's worth learning, but understanding how to build DSLs and understanding metaprogramming in general is totally worth it. It looks like JetBrains' product occupies a niche similar to the Microsoft Visualization and Modeling SDK, but I found the Microsoft tool a bit too frustrating in my limited use of it, mostly due to the emphasis on visual tools. Since the MPS seems to do a pretty good job on focusing on the text editing experience for the generated DSL, that might be enough to convince me to dig into it.

There's a certain subculture of developers who are cynical about any kind of meta-programming, and another subculture that is skeptical of any language X where X is not the language they are used to. Sufficiently old-school programmers may recall the 4GL movement that turned out, like many over-hyped endeavors, not to be all the vendors promised; some vendors claimed their language would let businesspeople who understand their domain write code and minimize the need for lower-level programmers. That rarely worked out.

So you may have some organizational resistance to using such tools, but like any tool, used effectively, it can make your team more effective and efficient.

The downside is that, unless you're already experienced doing so, building a DSL may still take you more time than building similar business logic the way you always have: in the clunky constructs of your usual language. The payoff, over time, is worth it, in my opinion. If you've worked with a system that uses metaprogramming effectively, like Rails in Ruby, you'll quickly understand how DSLs can pay off. If you've used something like Boo to build a DSL by altering the compiler pipeline (less painful than it sounds, at least in Boo), you quickly enjoy the power better ways of expressing your problems.

But unless you've got a team that is already in their element using such tools, you'll need to start slowly. Maybe the business rules for pricing a contract or product would benefit from a terse, easy-to-read language that allowed you to rapidly respond to changes in offerings, discounting schemes, etc. Or you could find the most tedious, repetitive coding task you have and explore whether a DSL that simplifies that task can help you.

Title: Practical Meta Programming System (MPS)


问:
Should I validate API output?
I'm working on a Web API to provide data to a third party per the specification they provided.

The process for each API call is essentially:

1. Extract data as XML

2. Deserialize data to DTO (POCO)

3. Return DTO as response content (which is then serialized to JSON via Web API 2 content negotiation)

I've been asked by someone on our development team to validate the DTOs.

Aside from ensuring that the responses are in a format that the client can process, which was essentially already accomplished by creating the DTO classes, this seems like a waste of effort to me. I checked Google, but the only output validation anyone's talking about is sanitizing values like credit card numbers and SSNs. I can't remember ever seeing a method validate an object it just created before returning it.

Because the response JSON is really just the extracted XML after having been deserialized and re-serialized, the only way the DTO could be invalid would be if the extracted XML was invalid. So I'd basically be retesting the extractions on every call.

Except a few cases where the format is specified, I don't actually know what values are considered valid by the client. The best I can really do in most cases is make sure there aren't any blanks. I would essentially be attempting to blindly recreate the client's validation just so we could pre-validate the data before the client validated it anyway. Assuming I managed to get it right, the payoff would just be to shift the initial support burden to our team because our server would be throwing validation exceptions instead of their client.

The structure of the DTOs is fairly complicated. If I ignore the fact that they're being created through deserialization, the validation gets complicated fast. Most of the validation issues I would then have to check for (e.g., null array elements, blank values) aren't actually possible in the real implementation. XmlSerializer isn't going to create null array elements, and NOT NULL database fields aren't going to result in missing XML elements. Add 100% unit test coverage, and this is now adding significant effort and complexity.

Is output validation even a practice? I've never seen it done before. It feels overly cautious and preemptive. If it is a practice, is there another term for it that would help me to find more information on the subject?

答:
This is an interesting question and I am in almost exactly the same situation at the moment.

I've reached the conclusion that it's actually quite necessary because it's entirely possible that you might rename a field in your DTO. What happens then? You fix up the rest of your code, make everything work, deploy your new web service, but now your customer complains that your XML is now incorrect. Everything works on your end but you didn't catch the error.

My web service returns JSON but it's the same principle. I have a unit test that instantiates the WebApi controller, calls the method and retrieves the result as JSON, and make sure that the result is correct. If you mock and stub your data you can ensure that the entire result returned is exactly as you expect. Remember that you're not testing the .NET WebApi serialisation code, you're not testing that the output values are correct, you're testing that the output of your web service is syntactically correct (XML fields are nested properly and named properly).

I would say with XML is just as likely (possibly more likely) that you might accidentally change the format without even realising it and break the service for your client(s). So yeah, go for it, validate away.

Title: Should I validate API output?


问:
DRY principles vs local-only variable scope in functions (PHP motivated question)
I'm new to programming and in particular new to PHP. I often find that DRY is directly opposed to the principle of keeping variables local in PHP functions. Sometimes I think that it's easier to violate DRY to be able to readily access large arrays than it is to pass these arrays or their relevant subcomponents as parameters in functions.

Does anyone have thoughts on this or examples of code that finds another way? Here's an example of the kind of code I sometimes write because my arrays are large and I don't want to copy/pass them or have to write out all the chunks I might use in a function, hence deciding to violate DRY and not use a function:

```<code>if($array1['status'][$i]==1)
{
    for($j = 1; $j < 3; $j ++)
    {
        $j = 1;
        $dist = $array2['dist'][$j];
        $id =  $array2['id'][$j];
        $email =  $array2['email'][$j];
        send_offer_email($array1['appt_id'][$i], $array1['fulladd'][$i], $dist, $id, $email);
        $j = 2;
        $dist = $array2['dist'][$j];
        $id =  $array2['id'][$j];
        $email =  $array2['email'][$j];
        send_offer_email($array1['appt_id'][$i], $array1['fulladd'][$i], $dist, $id, $email);
    }
}
elseif($array1['status'][$i]==2)
{
    for($j = 3; $j < 7; $j ++)
    {
        $j = 1;
        $dist = $array2['dist'][$j];
        $id =  $array2['id'][$j];
        $email =  $array2['email'][$j];
        send_offer_email($array1['appt_id'][$i], $array1['fulladd'][$i], $dist, $id, $email);
        $j = 2;
        $dist = $array2['dist'][$j];
        $id =  $array2['id'][$j];
        $email =  $array2['email'][$j];
        send_offer_email($array1['appt_id'][$i], $array1['fulladd'][$i], $dist, $id, $email);
    }
}
</code>```

Clearly I'm a noob, but I'd love to hear what's wrong with doing this vs having to write out all the crap I'd need to reduce each of these if branches to something like:

```<code>$j = 1
function(blah blah, $j)
</code>```

In particular the blah blah in the function above would be really long and itself violate DRY.

Thanks!

答:
Your code is very difficult to read and it appears your data isn't structured well for what it represents and how it's used. I suspect if you address these issues then the solution to your reusability problem will become clear.

It isn't completely obvious what your code does, but take this example loosely based on what I think your code is doing:

```<code>class Applicant {

    public $id;
    public $address;
    public $email;
    public $status;

    public function sendDecision() {
        if($this->status == 1) {
            $this->sendOffer();
        } else {
            $this->sendRejection();
        }
    }

    public function sendOffer() {
        // Do something with $this->email
    }

    public function sendRejection() {
        // Do something with $this->email
    }
}

$applications = array(); // Populate this with an array of Applicant instances

foreach($applications as $applicant) {
    $applicant->sendDecision();
}
</code>```

That keeps you from creating a haze of array indexes which are easy to mix up, keeps related logic and data grouped together, and allows you to do something different depending on the status without duplicating code.

Body:
Your code is very difficult to read and it appears your data isn't structured well for what it represents and how it's used. I suspect if you address these issues then the solution to your reusability problem will become clear.

It isn't completely obvious what your code does, but take this example loosely based on what I think your code is doing:

class Applicant {

    public $id;
    public $address;
    public $email;
    public $status;

    public function sendDecision() {
        if($this->status == 1) {
            $this->sendOffer();
        } else {
            $this->sendRejection();
        }
    }

    public function sendOffer() {
        // Do something with $this->email
    }

    public function sendRejection() {
        // Do something with $this->email
    }
}

$applications = array(); // Populate this with an array of Applicant instances

foreach($applications as $applicant) {
    $applicant->sendDecision();
}

That keeps you from creating a haze of array indexes which are easy to mix up, keeps related logic and data grouped together, and allows you to do something different depending on the status without duplicating code.


问:
What to tell a manager who tells your estimates are too much for a genuine task
My manager keeps complaining that the estimates we have come up with are too much for the customer — every time he asks us to think like a customer and see whether the estimate is valid, but my point is that even though we somehow finish the project on time, lots of issues might creep up in the project. Apart from this, I am the only developer for this project, and he puts most of the blame on me; be it design, bugs or any other discrepancy in the project.

I am not sure if I am doing anything wrong, or is software engineering a field with no bugs where everything should be perfect? Is my manager is wrong? Can anyone explain?

答:
Estimation is difficult to get right. The variations are often high. In my experience, breaking down tasks helps a lot to estimate and plan well. This will help your manager see why your estimates are what they are.

Try estimating in Story points at a high level (And estimate in man-hours on specific subjects within the sprint). This over a period of time will give you the velocity, which is the Story points achievable over a sprint or release. Then as we add stories, we add the Story points estimates. This activity might not be suited for a one member team though.

Deliver something to customer and get feedback from the customer. This will help you to identify early a design issue.


答:
Your manager is probably under pressure to justify your time estimates, thus the friction. This kind of friction is part of the work environment, there are lots of books, classes, coaching sessions available that will teach you how to interact with different people and personality types so you can do your job.

More importantly for the customer ( and for your own ease of mind ), have you established a track record for the effort it takes to deliver a project, or talked with others in the industry about your time estimates? A good way to approach this kind of problem is to find a senior programmer who can give you some guidance on how long things should take so you get a sense of how you are doing relative to industry expectations. Once you have a sense for how long things 'should' take, you can apply your soft skills to the task of negotiating with your manager for more time as appropriate.

The customer may expect things better, faster, and cheaper, but in general they only get two of those three things. I've had potential customers say things like, "We have X budget and we want to become an online leader in our space." Just because they want it does not mean it will happen. Things take as long as they take.

Of course, if you really want to excel, find a way to do the same work better, faster, and cheaper. Like I said, mostly that is not what the customer gets, but if you can do it, you will get props from your manager and your customers.  Automation, improved tools, better processes - these are your friends.

Title: What to tell a manager who tells your estimates are too much for a genuine task

Body:
My manager keeps complaining that the estimates we have come up with are too much for the customer — every time he asks us to think like a customer and see whether the estimate is valid, but my point is that even though we somehow finish the project on time, lots of issues might creep up in the project. Apart from this, I am the only developer for this project, and he puts most of the blame on me; be it design, bugs or any other discrepancy in the project.

I am not sure if I am doing anything wrong, or is software engineering a field with no bugs where everything should be perfect? Is my manager is wrong? Can anyone explain?


问:
Having Foreign Keys in Business Objects
Let's say I have a business object called Child. It has no persistence logic, and is not designed around the UI. It merely represents a business entity.

Parent will have a reference to the Child objects: List Children

Since Parent has a direct object reference to Child(s), is it bad to also have Child have ParentId? Downside is that it is a duplicated data as there is already reference. Upside, is it will make it easier to populate from the data-layer.

Any opinions on this type of thing in general? If a business object has a reference to another business object is it bad form to also possess the foreign key?

答:
Keys are used to inform relationships and enforce referential integrity. Whether you need the ParentId key to live in the Child object depends on whether you will find yourself in a situation where you are working with a Child object and you need to know its parent. In general, I would suggest that it is good practice to include the key in the Child object because it allows you to find the identity of the Parent without going through the Parent object.

You might even run into a situation where you are working with a set of Child objects independent of the Parent objects. In that case, you might decorate the Child object with a minimal set of properties from the Parent object (name, for example) if you need that information, rather than generating the full Parent object. Your solution will depend on the specifics of your use case, the language you are using, whether you expect these objects to be long-lived, the cost of garbage collecting stale objects, etc.

Title: Having Foreign Keys in Business Objects


问:
Find all factors(prime and composite) of a number
We can find all the prime factors using a Sieve of Erastothenes. But how do we find ALL the factors of a number?

For instance, 24 = 2x2x2x3

But the complete factor list is - 1,2,3,4,6,8,12,24.

I am thinking of pushing all the factors in a vector, then using a bit mask to get each subset. The problems with this approach are-

The complexity will be O(n). For n=10^10, there are 35 factors, so 2^35 sets of factors, which is 10^10.

Repeated subsets. As the example above, we get multiple subsets with the same factors repeated, like (22....),(2.2...),(2..2..),etc. How to remove this redundancy?

答:
Most simple solution:

```<code>for i = 1 to sqrt(n)
   if i divides n put i and n/i into your result set
      (make sure if i=n/i you put it only once there)
</code>```

Complexity: O(sqrt(n))

(as long as you do not work with big numbers and can assume all basic arithmetic operations like test for divisibility as O(1).

A more sophisticated algorithm ist described here. First, find all prime factors of n including their powers dividing n (using the Sieve of Erastothenes). Then, create all combinations of those factors, as described in the blog post.

Body:
Most simple solution:

for i = 1 to sqrt(n)
   if i divides n put i and n/i into your result set
      (make sure if i=n/i you put it only once there)

Complexity: O(sqrt(n))

(as long as you do not work with big numbers and can assume all basic arithmetic operations like test for divisibility as O(1).

A more sophisticated algorithm ist described here. First, find all prime factors of n including their powers dividing n (using the Sieve of Erastothenes). Then, create all combinations of those factors, as described in the blog post.


问:
What features would you like to have in PHP?
Since it's the holiday season now and everybody's making wishes, I wonder - which language features you would wish PHP would have added? I am interested in some practical suggestions/wishes for the language. By practical I mean:

Something that can be practically done (not: "I wish PHP would guess what my code means and fix bugs for me" or "I wish any code would execute under 5ms")

Something that doesn't require changing PHP into another language (not: "I wish they'd drop $ signs and use space instead of braces" or "I wish PHP were compiled, statically typed and had # in it's name")

Something that would not require breaking all the existing code (not: "Let's rename 500 functions and change parameter order for them")

Something that does change the language or some interesting aspect of it (not: "I wish there was extension to support for XYZ protocol" or "I wish bug #12345 were finally fixed")

Something that is more than a rant (not: "I wish PHP wouldn't suck so badly")

Anybody has any good wishes?

Mod edit: Stanislav Malyshev is a core PHP developer.

答:
I really wish for better unicode support out of the box. Most languages move in that direction but PHP still have strange commands littered all over.

PHP strings are just plain byte arrays. Their content is non-portable as it is dependent on the current default encoding.

The same applies to the representation built by serialize. It contains a length-prefixed byte representation of the string without actually storing any encoding information.

Most PHP (string) functions have no clue about Unicode. For a detailed list including each function’s risk level, refer to: http://www.phpwact.org/php/i18n/utf-8

http://blog.ginkel.com/2010/03/php-unicode-support-or-the-lack-thereof/


问:
Is there a license that prohibits code share and using outside the company?
I'm working on a tool for small development teams. And I want to sell them full source code of my application so that they'll be able to update/modify the app according to their needs.

Is there any existing license that I can use that prohibits:

Tool usage outside the company

Code sharing and publishing

Creating own products based on my tool, publishing and selling them

And allows:

Modify the existing code as much as they want

Use the [modified] tool inside the company

?

答:
There are no standard (open-source) licenses that fit your needs.

Your requirement that your customers can't redistribute the software rules out all open-source licenses.

You can create your own license that allows the purchaser to make modifications for their own use but forbids redistribution. To create the license, you are well advised to consult a lawyer as copyright licenses are legal documents and writing such documents falls under the expertise of lawyers.


问:
Why server-side repository merge is a terrible idea in git?
Is it a good practice to do git merges on git server?</p>

答:
The basic idea behind merging is that you have to consider resolving conflicts. You cannot just automatically merge and hope the system figured out what you meant - that is mostly does this is convenient but do not get confused and think that all merges work perfectly all the time.

After merging, you will want to review the results and only if it passes the human check (and possibly compiles and runs your local tests) then you can commit with a fair hope for correctness.

You can't do this on the server unless you log into it and treat the server as if it was your local workstation. And if you are doing that, why not do it on your local workstation in the first place!?


问:
Most efficient way of saving images and sending them to client browser
I'm building an app where users list items for sale and they can include up to five photos for each item. I'm using Azure Blob storage for storing the images.

In my app, I'll be displaying these images in several different ways and sizes. I'll show them as thumbnails,  as roughly 250px in width and full size, depending on the page on which they are being displayed.

I'd like to know best practice for storing these images and sending them to the client. I have a couple choices. I could store just the single image, and then retrieve and resize it, depending on how it's going to get displayed, on the server before sending it to the client. Or, I could store several copies of each image, each with the appropriate size that will be required in the client pages.

I'm leaning towards the first option where I just save the single original copy, resize it on the server and then send it to the client. I have no experience with this sort of thing so I'm not certain the best approach, or if there are others.

答:
There are couple optimizations that can be done:

After retrieving the images from the blobs, cache it into memory, depending on hit frequency. If the amount of images is very important, consider using redis as a L2 cache provider (now available on Azure). If the web server has to support a massive amount of hits, and the images can be publicly available, I recommend to delegate Images transfers to the azure CDN, to get a (very) low latency.

Set the Cache-Control http header to an appropriate value. This will greatly limit the amount of round-trip per client

If the images are meant to be viewed at multiple sizes, consider sending only two versions: Thumbnails and HD. Then you can easily integrate the image to any size using the CSS3 background-size:contain; property, which gives great results.

In order to limit client/server round-trips, I would advise to send your images grouped. Using CSS3, you can inline multiple images in a single css file. Here is a method I wrote that merge images from a directory, as a single CSS string:

```<code>private static string images2css(string directory = "~/Images")
{
    StringBuilder css = new StringBuilder();
    foreach (string f in Directory.GetFiles(System.Web.Hosting.HostingEnvironment.MapPath(directory)).Where(x => x.EndsWith(".png") || x.EndsWith(".jpg") || x.EndsWith(".jpeg")))
    {
        var img = Image.FromFile(f);
        byte[] raw = File.ReadAllBytes(f);
        string name = Path.GetFileNameWithoutExtension(f);
        string ext = Path.GetExtension(f).Substring(1);

        css.AppendFormat(".img-{0}{{", name.Replace('_', '-'));
        css.AppendLine("background-color: transparent; background-repeat: no-repeat; background-position: left top;");
        css.AppendLine(string.Format("background-image: url(data:image/{1};base64,{0});", Convert.ToBase64String(raw), ext));
        css.AppendLine(string.Format("padding-left: {0}px !important; min-height: {1}px;", img.Width + 5, img.Height));
        //   css.AppendLine("background-size: contain;");
        css.AppendLine("}");
        img.Dispose();
    }

    return css.ToString();
}
</code>```

Shouldn't be too much work adapting it to fetch the images from an Azure Blob. You could also save the css string back to another blob with Gzip compression, and make it available to the cdn.

Overall, there are 3 main parameters to consider to choose the best strategy:

Are there a lot of Images? (no: L1 cache, yes: L2 cache)

Does the front server has to handle a massive amount of hits? (yes: the front server must not send images. Best option is CDN)

Does a page displays the same set of images (=> group images in one file), or the combination always different? (=> L2 cache or CDN)

Body:
There are couple optimizations that can be done:

After retrieving the images from the blobs, cache it into memory, depending on hit frequency. If the amount of images is very important, consider using redis as a L2 cache provider (now available on Azure). If the web server has to support a massive amount of hits, and the images can be publicly available, I recommend to delegate Images transfers to the azure CDN, to get a (very) low latency.

Set the Cache-Control http header to an appropriate value. This will greatly limit the amount of round-trip per client

If the images are meant to be viewed at multiple sizes, consider sending only two versions: Thumbnails and HD. Then you can easily integrate the image to any size using the CSS3 background-size:contain; property, which gives great results.

In order to limit client/server round-trips, I would advise to send your images grouped. Using CSS3, you can inline multiple images in a single css file. Here is a method I wrote that merge images from a directory, as a single CSS string:

private static string images2css(string directory = "~/Images")
{
    StringBuilder css = new StringBuilder();
    foreach (string f in Directory.GetFiles(System.Web.Hosting.HostingEnvironment.MapPath(directory)).Where(x => x.EndsWith(".png") || x.EndsWith(".jpg") || x.EndsWith(".jpeg")))
    {
        var img = Image.FromFile(f);
        byte[] raw = File.ReadAllBytes(f);
        string name = Path.GetFileNameWithoutExtension(f);
        string ext = Path.GetExtension(f).Substring(1);

        css.AppendFormat(".img-{0}{{", name.Replace('_', '-'));
        css.AppendLine("background-color: transparent; background-repeat: no-repeat; background-position: left top;");
        css.AppendLine(string.Format("background-image: url(data:image/{1};base64,{0});", Convert.ToBase64String(raw), ext));
        css.AppendLine(string.Format("padding-left: {0}px !important; min-height: {1}px;", img.Width + 5, img.Height));
        //   css.AppendLine("background-size: contain;");
        css.AppendLine("}");
        img.Dispose();
    }

    return css.ToString();
}

Shouldn't be too much work adapting it to fetch the images from an Azure Blob. You could also save the css string back to another blob with Gzip compression, and make it available to the cdn.

Overall, there are 3 main parameters to consider to choose the best strategy:

Are there a lot of Images? (no: L1 cache, yes: L2 cache)

Does the front server has to handle a massive amount of hits? (yes: the front server must not send images. Best option is CDN)

Does a page displays the same set of images (=> group images in one file), or the combination always different? (=> L2 cache or CDN)


问:
Using Euclidan Distance to intialize centroid of K-Means
I need to initialize centroid of K-Means using Euclidan distance. Is it possible? If so how the algorithm should be? I have been stuck for a while and couldn't figure it out.

Thanks for your time.

答:
You dont initialize k-means using Euclidean distance. There are a range of initialization methods.

The most intuitive is selecting random instances from your data and initializing there. That way each cluster in your first assignment step has at least one instance. The assignment step is where you apply a distance measure, like euclidean distance.


答:
Q: "What to tell a manager who tells your estimates are too much for a genuine task"

A: "I am starting at new job in x weeks. Who should I hand over the project to?"


答:
Facebook is actually in the process of implementing server side rebasing (there's a summary here or you can skip to to about 10:55 in the video).

In their case they have such a volume of pushes that developers are in competition to push, and they can get stuck trying to pull then push before someone else pushes and forces them to pull again.

By implementing server side rebases, pushes are simply added to a queue and processed in order, ensuring developers don't waste time getting stuck in a push/pull loop while waiting for an opportunity to push cleanly. The majority of rebases can be performed without conflict, but if there is then the developer gets notified to manually handle the merge then submit a new push. It also helps that they have a very intensive automated testing suite (also discussed in that video) so they can have greater confidence in the integrity of their automated merging.

Having said that, this approach only has benefits because they have such a high volume of pushes. These benefits don't exist if you don't have contention between pushes, and merging on the server side could significantly complicate conflict scenarios.

In general it makes more sense for the pusher to handle merging because they're in the best position to handle conflicts, and there isn't usually any time saved by performing the process on the server side.

Title: Why server-side repository merge is a terrible idea in git?

Body: Is it a good practice to do git merges on git server?</p>


答:
I would avoid bidirectional relationships as much as possible, as they can lead to synchronization problems (Child can end up having an obsolete ParentId).

That being said, a unidirectional reference to another object by its ID only can be very useful.

It's a very natural way of cutting an object graph in slices that can be loaded in memory from the database on demand, as opposed to hydrating the whole graph at once. Much more natural than a complex lazy loading mechanism IMO. References by ID are often used in Domain Driven Design where these slices are autonomous Aggregates.

Title: Having Foreign Keys in Business Objects

Body:
Let's say I have a business object called Child. It has no persistence logic, and is not designed around the UI. It merely represents a business entity.

Parent will have a reference to the Child objects: List Children

Since Parent has a direct object reference to Child(s), is it bad to also have Child have ParentId? Downside is that it is a duplicated data as there is already reference. Upside, is it will make it easier to populate from the data-layer.

Any opinions on this type of thing in general? If a business object has a reference to another business object is it bad form to also possess the foreign key?


问:
Is "Unit testing" a form of formal method?
After some research, I understand that:

Formal method contribute to the reliability and robustness of a design. (ref : Wikipedia - Formal method)

Unit testing ensures us of the absence of errors introduced by a developer  (ref : Wikipedia - Unit testing)

Like I see here, a formal proof is just a mathematical calculation, based on a mathematical expression (boolean expression).

A unit test is checked from assertions, so mainly on one or a set of boolean expressions.

Moreover, I read (very quickly) a research's paper of unit test generation from formal proof.

Is unit testing a form of formal method (here I think to a system like formal proof)?

If it's totally separate concept what are the differences?

The aim of this question is not to know if after running unit testing there still bugs. His aim is to know if there is a link between unit testing and formal method.

答:
The two are different things, and in fact much more different in practice than they would be in theory.

A formal correctness proof proves something about the behaviour of an algorithm. For instance, it might investigate the invariants applying to the data as it is transformed by a sorting algorithm, and prove that if the algorithm terminates, every element is larger than the previous one. This kind of proof can be rigorous, i.e. if it's done correctly the algorithm cannot be wrong in this respect.

In practice, algorithms must be embodied in computer code, and it's usually infeasible to prove that a given bit of code accurately represents the algorithm that you want. (That would require formally proving the behaviour of the compiler, the standard library, the virtual machine, etc.) It gets a little easier the more similar the programing language is to the mathematical notation you've used in the formal proof, but not much. (The code running in central systems of the Space Shuttle was said to be almost a perfect mathematical notation itself, but not very pleasant to program in.)

It's much more cost-effective to actually run the code on judiciously chosen inputs and verify that it produces the expected outputs. This has the disadvantage that you can never be certain that there isn't an error in it - it might behave for those input/output pairs you haven't tested (and there are usually more pairs than you can test, or you wouldn't need a computer program to do the work in the first place), or worse, the code might be subtly non-deterministic or context-dependent in  way that your tests don't expose.

But in practice, most errors that affect a computation can be exposed with intelligent checks, and if you keep a record of known errors and test cases verifying they cannot recur, the quality of code can generally be made good enough to be of business value. Certainly it's a better idea to run unit tests, integration tests and user acceptance tests and get something out the door that people will pay for than to conduct a lengthy, expensive formal proof that overlooks a subtle deviation between the written specification and the actual expectations of the customer. So in the real world, the two are almost completely distinct activities.


答:
There is no precise, universally accepted definition of "formal method". However, most definitions imply some form of mathematical rigour, formalism or proof, none of which a unit test provides.

A unit test is simply "try it out and see if it works in this one single specific case", whereas formal methods try to prove that "it always works in every case under every condition in every environment".

Title: Is "Unit testing" a form of formal method?

Body:
After some research, I understand that:

Formal method contribute to the reliability and robustness of a design. (ref : Wikipedia - Formal method)

Unit testing ensures us of the absence of errors introduced by a developer  (ref : Wikipedia - Unit testing)

Like I see here, a formal proof is just a mathematical calculation, based on a mathematical expression (boolean expression).

A unit test is checked from assertions, so mainly on one or a set of boolean expressions.

Moreover, I read (very quickly) a research's paper of unit test generation from formal proof.

Is unit testing a form of formal method (here I think to a system like formal proof)?

If it's totally separate concept what are the differences?

The aim of this question is not to know if after running unit testing there still bugs. His aim is to know if there is a link between unit testing and formal method.


答:
Normalize the global namespace with a well thought-out naming convention that makes sense to new-comers!

To quote our beloved Jeff Atwood: PHP sucks but it doesn't matter!

Title: What features would you like to have in PHP?

Body:
Since it's the holiday season now and everybody's making wishes, I wonder - which language features you would wish PHP would have added? I am interested in some practical suggestions/wishes for the language. By practical I mean:

Something that can be practically done (not: "I wish PHP would guess what my code means and fix bugs for me" or "I wish any code would execute under 5ms")

Something that doesn't require changing PHP into another language (not: "I wish they'd drop $ signs and use space instead of braces" or "I wish PHP were compiled, statically typed and had # in it's name")

Something that would not require breaking all the existing code (not: "Let's rename 500 functions and change parameter order for them")

Something that does change the language or some interesting aspect of it (not: "I wish there was extension to support for XYZ protocol" or "I wish bug #12345 were finally fixed")

Something that is more than a rant (not: "I wish PHP wouldn't suck so badly")

Anybody has any good wishes?

Mod edit: Stanislav Malyshev is a core PHP developer.


问:
Which arguments pass by value and which pass by reference in Java?
I am starting to learn Java with the Java Trails tutorials offered by Oracle. I am in the section where it talks about passing arguments to methods The Java™ Tutorials: Passing Information to a Method or a Constructor.

It says that primitive types are passed by value, which makes sense to me. Right after that it says:

Reference data type parameters, such as objects, are also passed into
  methods by value. This means that when the method returns, the
  passed-in reference still references the same object as before.
  However, the values of the object's fields can be changed in the
  method, if they have the proper access level.

Now this doesn't make sense to me. If an object is passed by value, then changing the fields of that object will change the fields of the copy inside the method, not the original one.

When testing with the program below:

```<code>class Point {
    int x;
    int y;

    public static void movePoint(Point p, int x, int y) {
        p.x+=x;
        p.y+=y;
    }
}
</code>```

```<code>class App {
    public static void main(String argv[]) {
        Point p1;

        p1 = new Point();
        p1.x = 2;
        p1.y = 3;

        p1.movePoint(p1,2,2);

        System.out.println("x = " + p1.x +  " y = " + p1.y);
    }
}
</code>```

It worked as if the object was being passed by reference (it prints out x = 4 y = 5 after all).

So what exactly did Oracle mean in the passage above? Also, can anyone summarize what's passed by value and what's passed by reference in Java?

答:
There is a test for whether a language is pass-by-reference: can you write a swap function, such that after swap(a,b), the caller finds that the values of a and b are reversed? You cannot do this in Java, not even for objects.

Note that you cannot achieve this goal by swapping the internals of the two objects  - while the data may change, the identity of the objects has not, and this has consequences. For one thing, any other variable in the program that referenced either object would also see the change.

To see why you cannot write this swap, consider a simplified model of the language, in which the value of a reference variable is the address of the object it is currently bound to (garbage collection complicates this picture in a way not relevant here.) Before the call to swap, the caller's stack frame has a slot for a, containing the address of object A, and a slot for b containing the address of B. When swap is called, it receives the values of a and b, which are the addresses of A and B respectively. Therefore, it is decoupled from the caller: there is simply no way for it to find the addresses of the slots for a and b within the caller's stack frame, and therefore no way to set them so that the caller will see them swapped.

@supercat suggests an alternative way of looking at it: conceptually, a reference variable holds the unique ID of the object it references, something that is fixed for the life of the object, and it is this that gets copied to the function in a call. This view also makes it clear that the function has no way to find, let alone modify, the variable in the caller's stack frame.

Call-by-reference means exactly the opposite, by definition: the function accesses its arguments through the caller's variable bindings, so that changing one of its arguments' binding in the function necessarily changes it in the caller's stack frame. The  term dates to a time when mainstream languages did not have objects or references as first-class entities, and when an argument was passed, either a copy of its value (in pass-by-value) or its address (in pass-by-reference) was pushed on to the stack so that the function could retrieve it. Passing an argument's address allows swap to work, but passing an address it contains does not.

Another way of looking at it: pass-by-reference does not mean pass the reference held in the argument variable, it means create, in the function, a local variable that references the argument variable.

Therefore, we can see that Java uses pass-by-value, not pass-by-reference.

See also Java is Pass-by-Value, Dammit! which also discusses argument-passing in Java RMI.


问:
Repetitive code over multiple classes. Best way to centralize code?
I'm working with 3D arrays that I flatten into a 1D arrays with the following index calculation:

```<code>index = x + WIDTH * (y + DEPTH * z)
</code>```

I'm starting to notice that I'm using the calculation all over the place in different classes and am wondering if using a macro would be a good solution?

I've also thought about creating a static method in a class and then using that in all these classes, but adding that dependency to all those classes doesn't sit well with me.

Is there a good standard practice or pattern to follow in this case?

答:
but adding that dependency to all those classes doesn't sit well with me.

If each array is encapsulated inside a data class, it makes sense to add an overloaded operator to that class to lookup the elements.

```<code>// This code only illustrate the overloaded operator.
// The code here is incomplete, and by itself is unfit
// for all purposes.
//
// (See below for a link to OpenCV's matrix source code.)
//
template <typename T>
class Array2
{
    T& operator () (int x, int y) { return mData[y * mWidth + x]; }
    const T& operator () (int x, int y) const { return mData[y * mWidth + x]; }
    // If one really needs a function to just return the value of (y * mWidth + x), such functions can also be added to this class.

private:
    int mWidth;
    int mHeight;
    T* mData; // make sure you know how to allocate, free, prevent memory leak, and prevent double-freeing of this pointer to array.
};
</code>```

A more fully implemented version will probably look like this:

OpenCV Core mat.inl.hpp, Lines 1476-1494, Mat_<Tp>::operator()(...)

If there are multiple classes, and it is foreseeable in the near future that each class may soon have different dimensions (say, 2D and 4D), and if you are not already using a class for each kind of array, you should seriously consider making that transition.

If, however, you must use a free function (not a member of any class), keep these issues in mind:

Think about how WIDTH and HEIGHT are passed into the function. In the code sample (and also in @Snowman's answer), it is assumed that your entire program will have a single hard-coded value for WIDTH and HEIGHT. As soon as you start using arrays of different sizes, you will see that the data class encapsulation approach is the only sane way to do that.

If you need to use arrays of different dimensions, give distinct names to those functions.

Avoid function name collisions. If you make that a class member, there is less worry of function name collisions. Otherwise, you can make that a static class member, give that function a project-specific prefix, or put that function in a namespace.

Finally, it is okay to have duplications for such small pieces of code. Avoidance of code duplication should be weighed against the problem of increasing dependencies (and hence coupling). In this particular case, it is favorable to let each class own the convenience functions it uses.

Of course, the encapsulated data class approach should still be the most favorable approach, when all things are considered.

If in the very far future you start considering implementing SIMD and other performance optimizations, further changes to the data class will be needed.

Body:
but adding that dependency to all those classes doesn't sit well with me.

If each array is encapsulated inside a data class, it makes sense to add an overloaded operator to that class to lookup the elements.

// This code only illustrate the overloaded operator.
// The code here is incomplete, and by itself is unfit
// for all purposes.
//
// (See below for a link to OpenCV's matrix source code.)
//
template <typename T>
class Array2
{
    T& operator () (int x, int y) { return mData[y * mWidth + x]; }
    const T& operator () (int x, int y) const { return mData[y * mWidth + x]; }
    // If one really needs a function to just return the value of (y * mWidth + x), such functions can also be added to this class.

private:
    int mWidth;
    int mHeight;
    T* mData; // make sure you know how to allocate, free, prevent memory leak, and prevent double-freeing of this pointer to array.
};

A more fully implemented version will probably look like this:

OpenCV Core mat.inl.hpp, Lines 1476-1494, Mat_<Tp>::operator()(...)

If there are multiple classes, and it is foreseeable in the near future that each class may soon have different dimensions (say, 2D and 4D), and if you are not already using a class for each kind of array, you should seriously consider making that transition.

If, however, you must use a free function (not a member of any class), keep these issues in mind:

Think about how WIDTH and HEIGHT are passed into the function. In the code sample (and also in @Snowman's answer), it is assumed that your entire program will have a single hard-coded value for WIDTH and HEIGHT. As soon as you start using arrays of different sizes, you will see that the data class encapsulation approach is the only sane way to do that.

If you need to use arrays of different dimensions, give distinct names to those functions.

Avoid function name collisions. If you make that a class member, there is less worry of function name collisions. Otherwise, you can make that a static class member, give that function a project-specific prefix, or put that function in a namespace.

Finally, it is okay to have duplications for such small pieces of code. Avoidance of code duplication should be weighed against the problem of increasing dependencies (and hence coupling). In this particular case, it is favorable to let each class own the convenience functions it uses.

Of course, the encapsulated data class approach should still be the most favorable approach, when all things are considered.

If in the very far future you start considering implementing SIMD and other performance optimizations, further changes to the data class will be needed.


答:
I would essentially be attempting to blindly recreate the client's validation just so we could pre-validate the data before the client validated it anyway.

Having to do it blindly should be a big red flag that you and your clients haven't come to an agreement about what constitutes valid data.  If you don't know what valid data looks like, you can't test your code.  If you can't test your code, you can't say it works with any level of confidence.  Essentially, you're depending on your clients to find your bugs, which isn't good.

Is output validation even a practice?

Absolutely.  Whether or not it's a common enough practice is open for debate.  :-)

Output validation is part of something called design by contract (or DbC), which is a term coined by Bertrand Meyer when he designed the Eiffel programming language in the 1980s.  One of the design principles surrounding DbC dictates that the first step in developing a unit of code is specifying what conditions must be satisfied on entry (preconditions) and exit (postconditions) for execution to be considered successful.  These conditions, called the contract, are extremely powerful tools for ensuring that all parties understand what code is supposed to do and that it makes good on those promises.  The link above contains a more thorough description of contracts and why you'd want them.  Eiffel and a handful of other languages support DbC or something like it directly; many others do it with assertions or add-on packages.

What you're generating sounds complex enough that what you should be using one or more functions to generate each part.  As you break the whole into smaller pieces, specifying what's correct for each piece and doing the verification becomes a set of smaller, simpler tasks that are less prone to mistakes.

For example, say part A of your output is composed of sub-parts B, C and D.  If the functions that generate B, C and D can guarantee that their outputs are correct, validating A becomes a simple matter of checking that the other three actually produced something.  If there's no validation in the generation of the sub-parts, it's up to A to check everything, and that can get very complex very quickly.

If this sounds like a duplication of effort already being done by the client, it shouldn't be.  In an ideal world, the class that represents B would have features to ensure that inputs and outputs are valid and the implementation would be used at both ends of the transaction.  When that's not possible, the  best that can happen is that both sides do their own validation.  That hurdle is usually administrative rather than technical.  Either way, there's nothing wrong with extra validation:  you're doing it to ensure that what you produce is correct and the client does its own version to detect your mistakes.

Title: Should I validate API output?

Body:
I'm working on a Web API to provide data to a third party per the specification they provided.

The process for each API call is essentially:

1. Extract data as XML

2. Deserialize data to DTO (POCO)

3. Return DTO as response content (which is then serialized to JSON via Web API 2 content negotiation)

I've been asked by someone on our development team to validate the DTOs.

Aside from ensuring that the responses are in a format that the client can process, which was essentially already accomplished by creating the DTO classes, this seems like a waste of effort to me. I checked Google, but the only output validation anyone's talking about is sanitizing values like credit card numbers and SSNs. I can't remember ever seeing a method validate an object it just created before returning it.

Because the response JSON is really just the extracted XML after having been deserialized and re-serialized, the only way the DTO could be invalid would be if the extracted XML was invalid. So I'd basically be retesting the extractions on every call.

Except a few cases where the format is specified, I don't actually know what values are considered valid by the client. The best I can really do in most cases is make sure there aren't any blanks. I would essentially be attempting to blindly recreate the client's validation just so we could pre-validate the data before the client validated it anyway. Assuming I managed to get it right, the payoff would just be to shift the initial support burden to our team because our server would be throwing validation exceptions instead of their client.

The structure of the DTOs is fairly complicated. If I ignore the fact that they're being created through deserialization, the validation gets complicated fast. Most of the validation issues I would then have to check for (e.g., null array elements, blank values) aren't actually possible in the real implementation. XmlSerializer isn't going to create null array elements, and NOT NULL database fields aren't going to result in missing XML elements. Add 100% unit test coverage, and this is now adding significant effort and complexity.

Is output validation even a practice? I've never seen it done before. It feels overly cautious and preemptive. If it is a practice, is there another term for it that would help me to find more information on the subject?


问:
Python Forgiveness vs. Permission and Duck Typing
In Python, I often hear that it is better to "beg forgiveness" (exception catching) instead of "ask permission" (type/condition checking).  In regards to enforcing duck typing in Python, is this

```<code>try:
    x = foo.bar
except AttributeError:
    pass
else:
    do(x)
</code>```

better or worse than

```<code>if hasattr(foo, "bar"):
    do(foo.bar)
else:
    pass
</code>```

in terms of performance, readability, "pythonic", or some other important factor?

答: In terms of correctness, I think exception handling is the way to go (I sometimes use the hasattr() approach myself, though).  The basic problem with relying on hasattr() is that it turns violations of code contracts into silent failures (this is a big problem in JavaScript, which doesn't throw on non-existing properties).


答:
My two biggest wishes as a hardcore PHP programmer:

Support finally. It's a big mess to fictionally get around this through flags or similar means.

I would LOVE to see support on C#'s syntax for getters and setters. When you have lots of getters and setters a simple syntax such as C#'s is a great performance booster instead of doing it the Java way and writing getter and setter methods. Magic methods are awesome in cases where you want to create members dynamically (for instance, if you want to give a template renderer some variables to use), but are no good for normal properties on which you would like the IDE to autocomplete, know their types, and so on. this would help make code smaller and still as readable and easy to use.


问:
How do you unit-test code using graph structures?
I am writing (recursive) code that is navigating a dependency graph looks for cycles or contradictions in the dependencies. However, I am not sure how to approach unit testing this. The problem is that one of our main concerns is will the code handle on all the interesting graph structures that can arise and making sure that all nodes will be handled appropriately.

While usually 100% line or branch coverage is sufficient to be confident that some code works, it feels like even with 100% path coverage you'd still have doubts.

So, how does one go about selecting graph structures for test cases to have confidence that their code could handle all the conceivable permutations you'll find in real world data.

PS- If it matters, all edges in my graph are labelled "must have" xor "cannot have" and there are no trivial cycles, and there is only one edge between any two nodes.

PPS- This additional problem statement was originally posted by the question's author in a comment below:

For all vertices N in forest F, for all vertices M, in F, such that if there are any walks between N and M they all must either use only edges labelled 'conflict' or 'requires'.

答:
We keep thinking that we have all the tricky ones covered and then we realise that a certain structure causes problems we hadn't considered before. Testing every tricky that we can think of is what we are doing.

That sounds like a good start. I guess you already tried to apply some classic techniques  like boundary value analysis or equivalence partitioning, as you already mentioned coverage based testing. After you invested a lot of time in constructing good test cases, you will come to a point where you, your team and also your testers (if you have those) run out of ideas. And that's the point where you should leave the path of unit testing and start testing with as much real world data as you can.

It should be obvious that you should try to pick a big diversity of graphs from your production data. Maybe you have to write some additional tools or programs just for that part of the process. The hard part here is probably to verify the correctness of your programs output, when you put ten thousand different real world graphs into your program, how will you know if your program produces always the correct output? Obviously you cannot check than manually. So if you are lucky, you may be able to make a second, very simple implementation of your dependency check, which might not fulfill your performance expectations, but is easier to verify than your original algorithm. You should also try to integrate a lot of plausibility checks directly into your program (for example, make some bookkeeping if your graph search hit every node and every edge of the graph).

Finally, learn to accept that every test can only proof the existence of bugs, but not the absence of bugs.


问:
Advantages of the imperative style over the functional style
There's a lot of hype over functional languages right now, and I've spent the last year studying Haskell as my intro to FP as a result.  Seeing the advantages FP provides is easy (such as referential transparency and easy concurrency due to the emphasis on immutability).

The question I have been asking myself, now, is whether imperative programming provides any clear advantage over the functional paradigm?  I'm not considering more controversial advantages like, oh, more people in the industry know the imperative style.  I'm looking more for advantages inherent to the style, regardless of popularity (such as, oh, it's actually better for writing performant low-level code quickly, or it's easier to debug because of x, y, and z, etc).  We could pretend that it's an ideal world, if you like, when talking about each paradigm (for example, let's pretend that UNIX was written in SML when talking about functional disadvantages, instead of saying, imperative is advantageous because UNIX is written in C).

It seems I'm having a harder time these days finding out what the advantages of the imperative style are, especially as processor architecture get's more complex so that a Haskell vs C implementation of optimized low-level code looks equally frightening to me.

答:
Well, you already hinted at the answer.  Imperative programming is closer to the metal, so it makes more sense in places (like embedded) where you're working closer to the metal. Nobody would bother programming an Arduino in Haskell (well, except maybe for @JimmyHoffa), though programming one in Scheme is not unheard of.

Some other reasons:

Computing model more readily maps to the underlying hardware.

Imperative programs can be written in a functional style, with a small amount of additional effort.

A huge base of preexisting imperative code guarantees the need for people having imperative skills.

The last point is perhaps the most important one.  Imperative code has an advantage because its already out there, in great volume.  And as long as we keep making languages like Java, we'll be writing more of it.


问:
Client-Server Application Object with child Objects
Actually I'm going to build an Client Server Application, at the Login I recive the User Object from the Server.
An user has a List of tasks, but I don't want to load them at the Login.
So what would be an good style to recive the Tasks of an user.
Should my user have an "GetTasks()" function which will access the server and will return the Tasks, which would be an strongly object orientated method.
Or should I Use an Service, which has a function that takes the user as parameter and returns the task.</p>

答:
How about decoupling the user model from the task model. Instead create a TaskManager object that could get the tasks for the user.
Something like </p>

```<code>TaskManager.getUserTasks(user); //which would return a list of Task objects
</code>```

<p>the advantage of keeping these two is that you can keep on adding functionality without having to change the User model. </p>

Body:
How about decoupling the user model from the task model. Instead create a TaskManager object that could get the tasks for the user.
Something like

TaskManager.getUserTasks(user); //which would return a list of Task objects

the advantage of keeping these two is that you can keep on adding functionality without having to change the User model.


问:
Required clarification about GPL licence for creating extensions of GPL software
I asked a question and received quite good answers. I read all answers and other sources about my question.

I concluded that if I write my code and distribute it to my clients for a free under my own license and ask them to download F3 separately at their own, my code won't be covered under GPL license anymore as I am not providing the GPL code with my distribution.

For example, people are selling WordPress themes and plugins which are not covered by GPL but WordPress is licensed under GPL. Am I right?

答: It depends. You should ask a lawyer. Actually, you should ask n lawyers, one for each country you intend to distribute your software to.


问:
Having one server for the whole team
I'm currently in a team where it lacks of its resources. The dev't team had only one websphere server for testing and we could not deploy our project in our local desktop because the websphere is only installed in one unit. Hence, every one is waiting after another in testing their fixes and feature. I tried to ask the manager if we could have our own copy of websphere installed in our local desktop but the manager is questioning us why we need it. I said some of the reason, but I guess the manager is still in doubt.

The question now is, in a development team, is having your own local application server installed in your desktop part of a best practices? how and why? what are the benefits? or what are the cons and pros?

答:
Very valid question and my team was in a similar situation - which is why sticking to the core specification of the API in use is so much more important to avoid vendor lock-ins. My answer is assuming you are on a Java stack but you can juxtapose for other language stacks. So yes get local setups for your team but you may not be able to get WebSphere so improvise - here are a few suggestions...

For your local environments I would suggest you deploy you application in Apache Tomcat preferably if you have no application server requirements. If you do have application server requirements consider Apache TomEE.

Other options where you can reduce the dependency on the application (and the web server) is by writing and diverting efforts towards Unit Testing (JUnit or TestNg) - this is reduce dev and deployment times in the long run and also reduce your competition for resources like the one WebSphere server you talked off.

Now the critical part - if in prod you are deploying on WebSphere for whatever reason - you have to test on WebSphere - however infrequently that may be. That is necessary since there are cases where some libraries in WebSphere may conflict with your application libraries and you may have to say adjust the ClassLoader Policy! This is something you can only discover when the application is deployed on WebSphere and not the 2 alternatives I proposed above. Another area that is notorious with WebSphere is JDBC if you use it with JDBC3 types like CLOBs, BLOBs, etc. It does not follow standards and you may have to write some conditional logic for such issues - so you identify the environment weather it is Dev or Test environment and execute conditional logic. If you use underlying container frameworks like Spring unit Testing, Working with a plain web container like Tomcat, and even running conditional environment specific logic/config (called even profiles) is so much easier.

Of course these aren't overnight changes - but since you are loosing time anyways on the shared system you might as well consider investing in some of these suggestions. So as a closing comment - focus on ways to reduce your team's dependency on a proprietary vendor server product even though you have to deploy on it for prod - that is exactly why we have standardized specifications.

Title: Having one server for the whole team