问:
How can QA staff test caching logic that they can't see?
I just implemented a caching layer in my web application, and now I'm wondering how QA is supposed to test it, since caching is transparent to the user.

One idea I have is to put logging in the methods that invoke the code that populate the cache, and record when an object is pulled from cache and when it requires recreation from the database, and then the testers could view the logs to see if, for example, a certain object is reloaded from the db every 10 minutes, instead of every page view.

But can anyone suggest some better practices for this situation?

答:
Remember to get the testers to reboot the servers and check that the data they have entered is still there.    I saw a system that had many man months of testing, fail when that was done!

The hardest part to test is that however the data is updated, there is never any out-of-date results returned from the cache.

This can be partly helped by always using the data in the cache to populate the confirmation page a user sees after they have made a change.   E.g don’t use the object you used to update the database, but request the data from the cache, that then request it from the database.   A bit slower, but much more likely to show up bugs quicker.


问:
How to manage legacy code in branches?
I have a website application which was developed and versioned using Git. A new version of the site was developed from the base of the older site.

The 'legacy' site lives in master and the new version of the application is now in international.

What techniques or processes can I employ to better manage this discrepancy?

Ideally, I would like the 'legacy' site to live in a legacy branch or similar, and our currently deployed code base to be tracked in master.

Can I just rename the branches? What impact would that have? I assume that I would need to rename all the remotes and anything which was pulling from those remotes?

Or should I think about forking the repo and then cleaning up the new site's repo?

Perhaps I could use tags to manage the divergence?

答:
Create a new branch called legacy from the current master.

Then merge your international branch into master and go from there.

Be sure to push your changes to any remote repositories you need to.

You can always merge changes from master to legacy, or from legacy to master as needed.

ContentLicense: CC BY-SA 3.0

Title: How to manage legacy code in branches?


问:
Is this breaking SOA?
We're havng a bit of a disagreement in our team, and I would like to hear other people's views. We have a mature solution that uses an Entity Framework model, which is accessed by a repository layer, which is in turn accessed by a business logic layer. The BLL classes implement interfaces, which are also implemented by services in a WCF project. The WCF services are very thin wrappers around the BLL classes, and each service call is basically a one-liner that passes the call through to the BLL. We have a WPF project that uses the WCF services for its data access.

We have recently added an ASP.NET MVC4 project to the solution, to provide web access to some of the features in the WPF project. As the web server is on the same local network as the SQL Server database and the WCF web folder, I added a reference to the BLL project from the MVC project, and did the data access directly between the MVC controllers and the BLL classes.

Our senior technical chap objected to this, and said I was breaking SOA. He wants me to add WCF service references to the MVC project, and access the data via WCF.

My argument against this is that going via WCF does not add anything of benefit to the MVC project, but hurts the performance (I know this for a fact, as I tried it). Given that the WCF services just pass the calls straight throuh to the BLL classes, which implement the same interfaces, I don't see how it is any more SOA to use WCF than to call the BLL methods directly. Furthermore, performance is critical in this MVC project, and I don't want to do anything that will impact the performance unless there is a very good reason to do so. I can't see any good reason here. Even if we optimise the WCF project, it is still always going to be slower than accessing the BLLs directly.

According to the definitions of SOA that I've seen (for example, see the Definitions section in Wikipedia's SOA article http://en.wikipedia.org/wiki/Service-oriented_architecture), our BLLs are definitely within the pattern. I don't see any requirement to use web services to call it SOA. Sure, web services are a form of SOA, but they aren't the only form. As far as I can see, a layered approach, which ours defintely is, is completely compliant with the principle of SOA. Now I realise that I may not have put his argument very strongly, but that's mainly because I can't understand it. I'm hoping that some people who are more knowledgeable than me can come along and offer some comments one way or the other. We have a meeting to discuss this tomorrow, and I;m sure he's going to pressrue me very strongly to use WCF instead of referencing the BLLs directly. If he's right, then fair enough, we'll have to suffer the performance hit. However, if there isn't any benefit to using WCF in this case, then I need to be able to explain why the way I've done it so far is fully SOA.

I would appreciate any comments.

答:
Let's step aside and deal with simpler terms: Whether or not, your system's architecture fits the SOA-paradigm, let's deal with »distributed systems« and »client-server-architecture«.

What is the main goal, choosing a distributed system? Decoupling. Since you are using a database server and the business logic is the client, your system is (slightly) decoupled and may be called distributed.

If I get your architecture right, you have a database, which is used by a business layer to perform certain operations on the data. The business layer itself is (only) accessible via WCF. So this assures a wide decoupling of components:

1) Database

2) Business Logic

3) A presentation layer (WPF)

If you want, you could go one step further and split (2) into smaller components without disturbing (1) or (3): Say you have one endpoint for accounting another endpoint for employees, changes are transparent to (1) or (3). The only coupling-point is the endpoint; so you could even do a split in half, one independent application is doing the accounting another the employees without making changes to (1) or (3).

If you now add »a reference to the BLL project from the MVC project, and did the data access directly between the MVC controllers and the BLL classes.« you are tighten the decoupling again. Every major change in the BLL forces changes in your MVC-Project.

Our senior technical chap objected to this, and said I was breaking SOA. He wants me to add WCF service references to the MVC project, and access the data via WCF.

He is right.

My argument against this is that going via WCF does not add anything of benefit to the MVC project, but hurts the performance
  Au contraire: it looses the coupling, which has clear advantages as said above.

If it hurts your performance, your problem sits probably elsewhere.

Furthermore, performance is critical in this MVC project, and I don't want to do anything that will impact the performance unless there is a very good reason to do so.

Go and fix your performance problem before discussing any further.

Even if we optimise the WCF project, it is still always going to be slower than accessing the BLLs directly.

Yes. That will always be the case. But that is not relevant. The http traffic itself is mostly the fastest factor - if not go to your ops-guy. Most of the time the problem is an unperformant query or slow businesslogic. Do your homework there first and you won't care about the overhead caused by a WCF-call.

I don't see any requirement to use web services to call it SOA

The point is: independent from its name, its purpose is decoupling.

Aside: Why are you bitching around?

If he's right, then fair enough, we'll have to suffer the performance hit.

This is nonsense (in the first place) and more: it's purely theatralic. Go and ask him for his reasons, utter your concerns and find a solution together!.

Title: Is this breaking SOA?


问:
Where is the entry point for ASP.NET MVC's HttpContext?
I've got some custom FormsAuthentication with a custom AuthorizeAttribute where I'm maintaining the cookie manually, but I'm doing it within the filter's context.

What I want to do is move this to the very beginning entry point, at the very beginning of the request's life, and only do it once, and not rely on an attribute kicking it off.

But, I don't know where that would be. MVC's page lifecycle is very different than the WebForms lifecycle, so its not as cut and dry as subscribing to one of WebForm's page lifecycle events.

So in ASP.NET MVC, where does the HttpContext begin?

答:
If this were recommended, there would be no AuthorizeAttribute and it wouldn't be an unsealed class.

Even if you could mess with it--and you can't--the worst performance hit is the fact that each HttpContext takes up a lot of memory.  Checking if an item exists in the session dictionary is a drop in a bucket for the current version of ASP.NET.

Self hosted NancyFX and ASP.NET MVC 6 actually tackled some of these challenges.  Both free of using the HttpContext if you choose to not host on IIS.  You may find NancyFX's pipelines and lack of HTTPContext more agreeable and performant than what you're doing in ASP.NET.


问:
How to organize large polyglot projects?
One of my projects started as PHP but recently some of the new functionality has been written in JavaScript on Node.js platform.

How do big polyglot projects that use multiple languages to write their server components organize their code?

One of the need arises when doing that is to have models and services written in multiple languages. This leads us to:

```<code>/models
  /php
  /js
  /ruby
/lib
  /php
  /js
  /ruby
</code>```

Alternatively we could put implementations written in different languages into the same folder and only differentiate them by the file extension.

```<code>/models
  user.php
  user.js
  role.rb
/lib
  auth.php
  service.js
  anotherService.rb
</code>```

What are some common patterns to deal with this?

答:
It may be the right time to introduce packages. It seems that the project has become complex enough to separate different functionality into independent packages. So the structure might look like this:

```<code>/package-1  # (Ruby)
  /models
    file.rb
  /lib
    some-file.rb
/package-2  # (Node)
  /models
    ...
  /lib
    ...
/package-3  # (Node)
  /models
    ...
  /lib
    ...
</code>```

Having structure like this opens up the possibility of updating code from each package independently, having clean APIs between them, then maybe even some independent versioning of each. Before you know it, you're gonna run full-blown Microservices architecture :-)

Of course every situation is different, but this pattern is very common for large services.

OwnerDisplayName: Denis Pshenov

Body:
It may be the right time to introduce packages. It seems that the project has become complex enough to separate different functionality into independent packages. So the structure might look like this:

/package-1  # (Ruby)
  /models
    file.rb
  /lib
    some-file.rb
/package-2  # (Node)
  /models
    ...
  /lib
    ...
/package-3  # (Node)
  /models
    ...
  /lib
    ...

Having structure like this opens up the possibility of updating code from each package independently, having clean APIs between them, then maybe even some independent versioning of each. Before you know it, you're gonna run full-blown Microservices architecture :-)

Of course every situation is different, but this pattern is very common for large services.

OwnerDisplayName: Krzysztof Kozielczyk


问:
Best way to analyse a large class before refactoring it into smaller classes?
Foreword

I'm not looking for a way to refactor a large spaghetti code class, that topic has been covered in other questions.

Question

I'm looking for techniques to begin to understand a class file written by another coworker  that spans over 4000 lines and has a single huge update method that is more than 2000 lines.

Eventually I hope to build unit tests for this class and to refactor it into many smaller classes that follow DRY and the Single Responsibility Principle.

How can I manage and approach this task? I'd like to be able to eventually draw a diagram of the events that occur within the class and then move on to abstracting functionality, but I'm struggling to obtain a top-down view of what the class's responsibilities and dependencies are.

Edit: In the answers people have already covered topics relating to input parameters, this information is for newcomers:

In this case the main method of the class has no input parameters and runs a while loop until instructed to stop. The constructor takes no parameters either.

This adds to the confusion of the class, its requirements and dependencies. The class gets references to other classes through static methods, singletons and reaching through classes it already has reference to.

答:
First, a word of caution (even though it isn't what you're asking) before answering your question: A tremendously important question to ask is why are you wanting to refactor something? The fact that you mention "coworker" begs the question: Is there a business reason for the change? Especially in a corporate environment, there will always be both legacy and even newly committed code that you won't be happy with. The fact of the matter is that no two engineers will solve a given problem with the same solution. If it works and doesn't have horrible performance implications, then why not leave it be? Feature work is generally much more beneficial than refactoring because you don't particularly like an implementation. That said, there's something to be said for technical debt and cleanup of unmaintainable code. My only suggestion here is that you've already made the distinction and have buy in to make the changes, otherwise you'll likely be asked "why are you making these changes" after having sunk in a whole lot of effort and will likely then get much more (and understandable) push back.

Before tackling a refactor, I always want to make sure that I have a firm understanding of what it is I'm changing. There are two methods I generally employ when tackling this kind of thing: logging and debugging. The latter is facilitated by the existence of unit tests (and system tests if they make sense). As you don't have any, I'd strongly urge you to write tests with full branch coverage to ensure that your changes don't create unexpected behavioural changes. Unit tests and stepping through the code help you get a low level understanding of behaviour under a controlled environment. Logging helps you get a better understanding of behaviour in practice (this is especially useful in multithreaded situations).

Once I get a better understanding of everything that's going on through debugging and logging, then I'll start reading code. At this point, I generally have a much better grasp of what's going on and sitting down and reading 2k LoC should make much more sense at that point. My intention here is more to get an end-to-end view and make sure that there isn't any sort of odd edge cases that the unit tests I've written aren't covering.

Then I'll give the design a think and draw up a new one. If backwards compatibility is of any importance, I'll make sure that the public interface doesn't change.

With a firm understanding of what's going on, tests and a fresh new design in hand, I'll put it up for review. Once the design has been vetted by at least two people (hopefully familiar with the code), I'll start implementing the changes.

Hope that isn't too painfully obvious and helps.


问:
Does sending system-profile information with HTTP GET request conform to RESTful practices?
I'm using the Sparkle framework to update an app, and want to make use of the framework's ability to send system-profile information. The way this is done is by adding the information to the HTTP GET request that asks for the update package. However, I've been told by my colleagues that it's best to reconfigure the framework to send the profile information using an HTTP POST request, as this is "the RESTful way to do things". </p>

<p>Is that correct??? I can't imagine, however, that the framework's creators would do implement these requests in a way that didn't conform to RESTful practices, and my research so far hasn't turned up any definitive answer. </p>

答:
RFC 7231, Section 4.3.1:

A payload within a GET request message has no defined semantics;

sending a payload body on a GET request might cause some existing

implementations to reject the request.

So in short, sending a payload is indeed incorrect. As far as REST goes, use POST if you're creating an item, and PATCH for partial updates or PUT for full updates (although this isn't set in stone and many people get away with solely using PUT).

Update based on comments:

Updating an object, whether it's via request body or GET params is not RESTful. A RESTful service abides by HTTP semantics.

The GET method requests transfer of a current selected
representation    for the target resource.

GET is intended to return the a representation of a resource's current state, not make changes to it.


答:
There is no general recipe, but some rules of thumb
(supposing a statically typed language but that shouldn't really matter):

1) Take a look a the method signature. It tells you what goes in and hopefully comes out. From the overall state of this god-class, I suppose this to be a first pain point. I suppose, that more than one parameter is used.

2) Use the search function from your Editor / EDI to determine Exit-Points (usually a return statement_ is used)

From that you know, what the function needs for testing and what you expect in return.

So a simple first test would be calling the function with necessary parameters and expect that the result is non-null. That is not much, but a starting point.

From that you could enter a hermeneutic circle (a term coined by H.G. Gadamer - a german philosopher). The point is: you have now a rudimentary understanding of the class and update this understanding with new detail knowledge and have a new understanding of the whole class.

This combined with the scientific method: make assumptions and look if they hold.

3) Take one parameter and look, where in the class it is somehow transformed:

E.g. you are doing Java like me, there are usually getter and setter for which you can look. Searchpattern $objectname. (or $objectname\.(get|set) if you are doing Java)

You could now make further assumptions on what the method does.

Trace only the input parameters (first) each through the method. If necessary make some diagrams or tables, where you write down every change to each of the variables.

From that, you can write further tests, describing the behaviour of the method.
If you have a rough understanding how each input parameter is transformed along the method, start experimenting: pass in null for one parameter or strange input. Make assumptions, check the outcome and vary input and assumptions.

If you do this a time, you have a "ton" of tests describing the behaviour of your method.

4) In a next step I would look for dependencies: what does the method need besides its input to work properly? Are there possibilities to reduce or restructure those?
The less dependencies you have, you clearer see the points, where to make the first splits.

5) From there you could go down the whole refactoring road with refactoring patterns and refactoring to patterns.

Here a nice Vid: GoGaRuCo 2014- The Scientific Method of Troubleshooting It's about troubleshooting but nevertheless usefull for a general methodology of understanding how something works.

You mention, that the called function has no input parameters: In this special case i would try to first identify the dependencies and refactor them to parameters, so you could swap them in and out as you like.

Title: Best way to analyse a large class before refactoring it into smaller classes?

Body:
Foreword

I'm not looking for a way to refactor a large spaghetti code class, that topic has been covered in other questions.

Question

I'm looking for techniques to begin to understand a class file written by another coworker  that spans over 4000 lines and has a single huge update method that is more than 2000 lines.

Eventually I hope to build unit tests for this class and to refactor it into many smaller classes that follow DRY and the Single Responsibility Principle.

How can I manage and approach this task? I'd like to be able to eventually draw a diagram of the events that occur within the class and then move on to abstracting functionality, but I'm struggling to obtain a top-down view of what the class's responsibilities and dependencies are.

Edit: In the answers people have already covered topics relating to input parameters, this information is for newcomers:

In this case the main method of the class has no input parameters and runs a while loop until instructed to stop. The constructor takes no parameters either.

This adds to the confusion of the class, its requirements and dependencies. The class gets references to other classes through static methods, singletons and reaching through classes it already has reference to.


答:
Interestingly, refactoring is the most efficient way I've found to understand code like this.  You don't have to do it cleanly at first.  You can do a quick and dirty analysis refactoring pass, then revert and do it more carefully with unit tests.

The reason this works is because refactoring done properly is a series of small, almost mechanical changes, that you can do without really understanding the code.  For example, I can see a blob of code that's getting repeated everywhere and factor that out into a method, without really needing to know how it works.  I might give it some lame name at first like step1 or something.  Then I notice that step1 and step7 seem to frequently appear together, and factor that out.  Then suddenly the smoke has cleared enough that I can actually see the big picture and make some meaningful names.

For finding overall structure, I've found creating a dependency graph often helps.  I do this manually using graphviz as I've found the manual work helps me learn it, but there are probably automated tools available.  Here's a recent example from a feature I'm working on.  I've found this is also an effective way to communicate this information to my colleagues.


问:
When is it a good idea to force garbage collection?
So I was reading a question about forcing the C# garbage collector to run where almost every single answer is the same: you can do it, but you shouldn't - except for some very rare cases. Sadly, nobody there elaborates on what are such cases.

Can you tell me in what sort of scenario it is actually a good or reasonable idea to force garbage collection?

I'm not asking for C# specific cases but rather, all programing languages that have a garbage collector. I know that you can't force GC on all languages, like Java, but let's suppose you can.

答:
The best practise is to not force a garbage collection in most cases.  (Every system I have worked on that had forced garbage collections, had underlining problems that if solved would have removed the need to forced the garbage collection, and speeded the system up greatly.)

There are a few cases when you know more about memory usage then the garbage collector does.  This is unlikely to be true in a multi user application, or a service that is responding to more then one request at a time.

However in some batch type processing you do know more then the GC.  E.g. consider an application that.

Is given a list of file names on the command line

Processes a single file then write the result out to a results file.

While processing the file, creates a lot of interlinked objects that can not be collected until the processing of the file have complete (e.g. a parse tree)

Does not keep match state between the files it has processed.

You may be able to make a case (after careful) testing that you should force a full garbage collection after you have process each file.

Another cases is a service that wakes up every few minutes to process some items, and does not keep any state while it’s asleep.  Then forcing a full collection just before going to sleep may be worthwhile.

The only time I would consider forcing
  a collection is when I know that a lot
  of object had been created recently
  and very few objects are currently
  referenced.

I would rather have a garbage collection API when I could give it hints about this type of thing without having to force a GC my self.

See also "Rico Mariani's Performance Tidbits"


答:
You mention a 2000 lines update method.

I'd start to read this method top to bottom. When you find a set of statements which belong to each other, i.e. share one responsibility (e.g. create a user), extract these statements into a function.

This way, you don't change how the code works but you

shorten this huge method

gain a better, higher level understanding of this method

prepare the method for further refactorings

Repeat these steps for other methods.


答:
According to the documentation, anonymous information about the user's system is sent with an update request.

I'd say that technically your colleagues are correct. This info doesn't appear to be used to affect the response to the request, so it doesn't really belong there. A better way to send that info would probably be to POST to a separate resource, such as /systemProfile or similar.

If you want to be strict about REST compliance you might seek to reconfigure the framework to provide the system profile info that way, but I wouldn't get too hung up on it. If the framework is useful and this is the way it provides profile information to the backend, the REST police aren't going to break down your door.

Title: Does sending system-profile information with HTTP GET request conform to RESTful practices?

Body:
I'm using the Sparkle framework to update an app, and want to make use of the framework's ability to send system-profile information. The way this is done is by adding the information to the HTTP GET request that asks for the update package. However, I've been told by my colleagues that it's best to reconfigure the framework to send the profile information using an HTTP POST request, as this is "the RESTful way to do things". </p>

<p>Is that correct??? I can't imagine, however, that the framework's creators would do implement these requests in a way that didn't conform to RESTful practices, and my research so far hasn't turned up any definitive answer. </p>


问:
Async Controllers in ASP.NET MVC: Real Advantages / How Achieved?
I have been working through an article about asynchronous controller methods in ASP.NET MVC (http://visualstudiomagazine.com/articles/2013/07/23/async-actions-in-aspnet-mvc-4.aspx) and I think I may be missing the point.

Consider this method I wrote, which is very similar to an example from the article:

```<code>[HttpGet]
[AsyncTimeout(8000)]
[HandleError(ExceptionType = typeof(TimeoutException), View = "TimedOut")]
public async Task<ActionResult> Index(CancellationToken cancellationToken)
{
    WidgetPageViewModel model = new WidgetPageViewModel()
    {
        toAdd = new Widget()
    };
    model.all = await _repo.GetAllAsync(cancellationToken);
    return View(model);
}
</code>```

As I understand things, this is how things will unfold at runtime:

An ASP.NET thread will be created for an incoming HTTP request.

This thread will (having presumably done some necessary preliminary work) enter my Index() method above.

Execution will reach the "await" keyword and kick off a data acquisition process on another thread.

The original "ASP.NET" thread will return to the code that called my handler method, with an instance of class Task as return value.

The infrastructural code that called my handler method will continue operating on the original "ASP.NET" thread, until it reaches a point where it needs to use the actual ActionResult object (e.g. to render the page).

The caller will then access this object using the Task.Result member, which will cause it (i.e. the "ASP.NET" thread) to wait for the thread created implicitly in step #3 above.

I am not seeing what this accomplishes compared to the same thing without await / async, except for two things I perceive as trifling:

The caller thread and the worker thread created by await can operate in parallel for some period of time (the "until" part of #5 above). My hunch is that period of time is pretty small. When the infrastructure calls into a controller method, I'm thinking it generally needs the actual ActionResult of the controller call before it can do much (if anything) more.

There is some helpful new infrastructure related to timeout and cancellation of long-running asynchronous controller operations.

The purpose of adding async controller methods is supposedly to free up those ASP.NET worker threads to actually answer HTTP requests. These threads are a finite resource. Unfortunately, I'm not seeing how the pattern suggested in the article actually serves to conserve these threads. And even if it does, and somehow offloads the burden of handling the request to some non-ASP.NET thread, what does that accomplish? Are threads that happen to be capable of handling an HTTP request that much different from threads in general?

答:
ASP.Net that does not use the Task Parallel Library (TPL) is limited in the number of requests it can handle concurrently by the number of threads in a thread pool. ASP.Net that uses the TPL is limited by the CPU/memory/IO of the machine handling requests.

The Task Parallel Library (TPL) does not consume threads in the way you seem to think it does. Tasks are not threads, they are a wrapper around some unit of computation. The task scheduler is responsible for executing each task on any thread that is not busy. At runtime, awaiting a task does not block the thread, it merely parks the execution state so that it may proceed at a later time.

Normally, a single HTTP request would be handled by a single thread, completely removing that thread from the pool until a response is returned. With the TPL, you are not bound by this constraint. Any request that come in starts a continuation with each unit of computation required to calculate a response able to execute on any thread in the pool. With this model, you can handle many more concurrent requests than with standard ASP.Net.

ContentLicense: CC BY-SA 4.0


问:
Algorithm for matching similar content text items
I am working on a website (C#, ASP.Net MVC 3) which reads some RSS feeds from multiple sources and put feed title and summary in a database table(Sql Server).

What I want to do is:
Put an algorithm in place which can relate multiple feeds. For example if each feed is a news item, I would like to relate all news which says in different grammar of English "Some has won some election".

Is there any standard algorithm for such kind of content matching logic?
If not, what kind of custom algorithm should be used?

If this logic can be written on Database side(e.g. Stored Procedure) it will be better.

答: I suppose you can take a minhash based near duplication detection approach since the text is relatively long (considering the content). There's a good article here</p>


问:
Better way of manipulating large objects
I am doing a lot of work with SOAP Api in PHP and the objects that are returned are quite big. I am then updating the objects from local db changes and pushing them back.

So more often than not I have a large object returned from a db query and a large object retrieved from a SOAP call and I have to update the one from the other.

So I end up with a lot of this:

```<code>$api_object->Organisation->Location[0]->AddressLine1 = $db_object->MainAddress->address_line_1;
$api_object->Organisation->Location[0]->AddressLine2 = $db_object->MainAddress->address_line_2;

$api_object->Organisation->Location[1]->AddressLine1 = $db_object->SecondaryAddress->address_line_1;
$api_object->Organisation->Location[1]->AddressLine2 = $db_object->SecondaryAddress->address_line_2;
</code>```

And so on, and so on with many more elements! And as you can see the names of the fields don't often match in naming or depth.

I just wondered if anyone had any suggestions how to do it better.

答:
Create alias references for the Location and Address objects to shorten the assignments.</p>

```<code>$loc = $api_object->Organisation->Location;
$main = $db_object->MainAddress;

$loc[0]->AddressLine1 = $main->address_line_1;
$loc[0]->AddressLine2 = $main->address_line_2;
</code>```

Body:
Create alias references for the Location and Address objects to shorten the assignments.

$loc = $api_object->Organisation->Location;
$main = $db_object->MainAddress;

$loc[0]->AddressLine1 = $main->address_line_1;
$loc[0]->AddressLine2 = $main->address_line_2;


问:
Write two versions of class with two different super classes without violating DRY?
I have a class called LimitedDict which is a dictionary that limits the number of entries it can contain by deleting old entries when a new one is added. It currently inherits from Python's dict class. I now need the exact same class, only I want it to inherit from defaultdict so that it has default values. How can I do this without just copying and pasting code?

I've thought of making it a wrapper, but that creates a lot of boilerplate code.

答:
Deriving a class "LimitedDict" from dict with the described behaviour will probably violate the Liskov substitution principle. Most code using dictionaries will expect that all data you put into a dict will stay in there and not vanish suddenly. So you cannot easily use a LimitedDict as a replacement for a dict in most places.

That's why it is not a good idea to derive such a class from dict, better implement it by using a dict. It is the old mantra: when in doubt, favor composition over inheritance. The internal "storage" attribute of your class could either be a standard dict or a defaultdict, and you can implement some mechanics to choose between those two (for example by injecting the dict object through the constructor), which solves your problem immediately.

You may call this "making a wrapper", and yes, you will have to add a little bit of boilerplate code, but to my experience this is a small price for the headaches you will save yourself from when you later have to maintain that code.


答:
In this specific case, you don't need to subclass defaultdict at all, because defaultdict is not much more than a dict subclass with an added __missing__ method.

You can simply subclass LimitedDict and add that method to the subclass:

```<code>class DefaultLimitedDict(LimitedDict):
    def __init__(self, factory, *args, **kw):
        self.default_factory = factory
        super().__init__(*args, **kw)

    def __missing__(self, key):
        if self.default_factory is None:
            raise KeyError(key)
        self[key] = new_value = self.default_factory()
        return new_value
</code>```

In a more generic situation you could move to using multiple inheritance; move your additional methods or custom behaviour to a mix-in class and inherit from both dict or defaultdict and your mix-in:

```<code>class LimitedDict(DictLimiter, dict):
    # methods will first be looked up on DictLimiter, then dict

class DefaultLimitedDict(DictLimiter, defaultdict):
    # methods will first be looked up on DictLimiter, then dict
</code>```

Title: Write two versions of class with two different super classes without violating DRY?

Body:
I have a class called LimitedDict which is a dictionary that limits the number of entries it can contain by deleting old entries when a new one is added. It currently inherits from Python's dict class. I now need the exact same class, only I want it to inherit from defaultdict so that it has default values. How can I do this without just copying and pasting code?

I've thought of making it a wrapper, but that creates a lot of boilerplate code.

Body:
In this specific case, you don't need to subclass defaultdict at all, because defaultdict is not much more than a dict subclass with an added __missing__ method.

You can simply subclass LimitedDict and add that method to the subclass:

class DefaultLimitedDict(LimitedDict):
    def __init__(self, factory, *args, **kw):
        self.default_factory = factory
        super().__init__(*args, **kw)

    def __missing__(self, key):
        if self.default_factory is None:
            raise KeyError(key)
        self[key] = new_value = self.default_factory()
        return new_value

In a more generic situation you could move to using multiple inheritance; move your additional methods or custom behaviour to a mix-in class and inherit from both dict or defaultdict and your mix-in:

class LimitedDict(DictLimiter, dict):
    # methods will first be looked up on DictLimiter, then dict

class DefaultLimitedDict(DictLimiter, defaultdict):
    # methods will first be looked up on DictLimiter, then dict


答:
You are looking for something like a mixin. A mixing is like a subclass that you can apply to multiple superclasses, to create a new combined class. Since Python supports multiple inheritance, this is fairly easy to do:

```<code>class A(object):
  def foo(self):
    print("A::foo")
  def bar(self):
    print("A::bar")

# a variation of A
class B(A):
  def foo(self):
    print("B::foo")

# a mixin that can be applied to any A -- that is A or B
class Mixin(A):
  def foo(self):
    print("before foo")
    super(Mixin, self).foo()
  def bar(self):
    super(Mixin, self).bar()
    print("after bar")

# Mixin + A
# actually, that's the same as Mixin itself
class MixedA(Mixin, A):
  pass

# Mixin + B
class MixedB(Mixin, B):
  pass

a = MixedA()
a.foo()
a.bar()

b = MixedB()
b.foo()
b.bar()
</code>```

Output:

```<code>before foo
A::foo
A::bar
after bar
before foo
B::foo
A::bar
after bar
</code>```

which tells us that the combined classes are architectured in a manner so that the Mixin changes apply to both the A and B superclasses as if you had copy-pasted them.

Body:
You are looking for something like a mixin. A mixing is like a subclass that you can apply to multiple superclasses, to create a new combined class. Since Python supports multiple inheritance, this is fairly easy to do:

class A(object):
  def foo(self):
    print("A::foo")
  def bar(self):
    print("A::bar")

# a variation of A
class B(A):
  def foo(self):
    print("B::foo")

# a mixin that can be applied to any A -- that is A or B
class Mixin(A):
  def foo(self):
    print("before foo")
    super(Mixin, self).foo()
  def bar(self):
    super(Mixin, self).bar()
    print("after bar")

# Mixin + A
# actually, that's the same as Mixin itself
class MixedA(Mixin, A):
  pass

# Mixin + B
class MixedB(Mixin, B):
  pass

a = MixedA()
a.foo()
a.bar()

b = MixedB()
b.foo()
b.bar()

Output:

before foo
A::foo
A::bar
after bar
before foo
B::foo
A::bar
after bar

which tells us that the combined classes are architectured in a manner so that the Mixin changes apply to both the A and B superclasses as if you had copy-pasted them.


答:
Garbage collection is first and foremost a memory management tool. As such, garbage collectors will collect when there is memory pressure.

Modern garbage collectors are very good, and getting better, so it's unlikely that you can improve on them by collecting manually. Even if you can improve things today, it may well be that a future improvement to your chosen garbage collector will make your optimisation ineffective, or even counterproductive.

However, garbage collectors typically do not attempt to optimise use of resources other than memory. In garbage collected environments, most valuable non-memory resources have a close method or similar, but there are some occasions where this isn't the case for some reason, such as compatibility with an existing API.

In these cases it may make sense to manually invoke garbage collection when you know that a valuable non-memory resource is being used.

RMI

One concrete example of this is Java's Remote Method Invocation. RMI is a remote procedure call library. You typically have a server, which makes various objects available for use by clients. If a server knows that an object isn't being used by any clients, then that object is eligible for garbage collection.

However, the only way that the server knows this is if the client tells it, and the client only tells the server that it doesn't need an object any more once the client has garbage collected whatever is using it.

This presents a problem, since the client may have lots of free memory, so may not run garbage collection very frequently. Meanwhile, the server may have lots of unused objects in memory, that it can't collect because it doesn't know that the client isn't using them.

The solution in RMI is for the client to run garbage collection periodically, even when it has lots of memory free, to ensure that objects are collected promptly on the server.

Title: When is it a good idea to force garbage collection?

Body:
So I was reading a question about forcing the C# garbage collector to run where almost every single answer is the same: you can do it, but you shouldn't - except for some very rare cases. Sadly, nobody there elaborates on what are such cases.

Can you tell me in what sort of scenario it is actually a good or reasonable idea to force garbage collection?

I'm not asking for C# specific cases but rather, all programing languages that have a garbage collector. I know that you can't force GC on all languages, like Java, but let's suppose you can.


问:
as a team: multiple projects vs single project?
I've been wondering for a while. I guess I have been adapting to the circumstances but I am in desperate need of some clearly defined workflows.

Ok, so say I am working with one other person and I have 2 projects come up both of equally large size with LOOSE deadlines. What should I do:

a) turn down one, work on other
  exclusively

b) take both on and develop both
  simultaneously

c) assign one person to each project

I have done c in the past which works fine. As a manager also, I've simply had to manage that project while I work on another. No big deal. But what is the ideal option above?

I'd hate to do a because I am turning down work either though deadline would permit working on both projects.

Any input?

Following things should be taken into account:

-) all members have same expertise in design & development and can
  effectively function as freelancers

-) timezones may not be the same so they would not be working at the same
  time

答: With no team experience: Personally, I think b would be the worst choice as you each have to keep up with multiple projects, their requirements, recent changes, etc.  I guess it's really just a matter of preference, but I would work on one exclusively until it's done.  Then work on the other.</p>

ContentLicense: CC BY-SA 2.5


问:
User Story vs Requirement
User Story captures what the user wants to do with the system at a high level. I understand that the user story would further drive a number low level requirements. Is user story same as high level requirement for the system?</p>

答:
I think there is a lot of inconsistency on what the word requirements means, even within classic text books.  The same inconsistency applies to user stories.  Different organizations and textbooks treat these terms differently.  For example, How Roger Pressman's classic Software Engineering textbook talks about requirements is quite different than Dean Leffingwell's Agile Software Requirements book.  I respect them both and they both can be valid.

Requirements can be things we code to that have extraordinary specificity with little left to the imagination.  On the other hand, it can be argued that requirements should specify what is the business problem and not specify the solution.  I think that it is more nuanced though and the answer is somewhere on a spectrum that is unique to each company and industry (not all software development occurs in IT).

I was taught that requirements elicitation leads to analysis, that leads to design, that leads to architecture that leads to requirements elaboration or specification, that leads to something that can be coded.  I don't believe this goes away with agile.  The timing of when these things happens does change and that is the most important difference.  In the waterfall model, elicitation and elaboration happens early.  In lean and scrum, elicitation and elaboration happens at various stages with more elaboration happening as you get closer to implementation in a sprint.  As does emergent design work.

In our organization, we are leaning towards the Leffingwell model of Epics, Features and Stories, not as requirements but as work breakdown and prioritization.  Requirements are a different thing. Requirements are managed separately because we are required to do so for regulatory agencies.  And yet some teams are certainly developing requirements within user stories during program increment and sprint planning.

Title: User Story vs Requirement


问:
Preferred way to expand a command line script to be used as a library in Python
I have a useful Python script that I've been invoking from the command line. It has decent number of options, maybe 20, and it's not unusual to run the script with six or seven flags. Then the rest of the input comes via stdin.

Now I have some other Python code from which I'd like to call this useful little utility. Two options I can think of are:

I can use subprocess.call and invoke my little script

(A little better) I can cobble together a command line and then pass it as a list of strings to argparse

I can totally refactor the program so that the entry point of my utility is Python function call and then have my command line utility just call this function. In principle this seems like the responsible thing to do, but it does leave my managing two separate interfaces to my function. For example, I have to decide whether I want to let argparse know the default values for my options or have the function know the defaults (or have two sets of defaults). Any validation I do using, say, ArgumentParser.add_mutually_exclusive_group will not apply when my tool is run as a library instead of a command line script.

Is there a standard paradigm for creating a single interface in Python that is well-suited to being invoked both from Python and from the command line?

答:
Calling a Python script A from another Python script B through subprocess looks weird to me. I'm not sure if performance impact will matter, but still, having a dependency on A in B and being able to call public methods directly seems more powerful and convenient, especially if both A and B use pip.

I would probably use subprocess in one of those cases:

If you don't use pip. Dependency handling and versioning can quickly become too complicated; calling A through command line will then be a better choice by providing a better abstraction.

If the script A is not written by you or your company and is intended to be used through command line.

For instance, vmbuilder—the tool which creates virtual machines in some distributions of Linux, is intended to be used from command line, and it seems natural (and easy) to use it this way instead of invoking Python methods directly.

If there is a risk for script A to be rewritten in another language. By using the command line, it doesn't matter which language A uses under the hood. It can be Java, C or Haskell, or may even be an ordinary Bash script—the way you call it will still be the same.


答:
One thing no one has mentioned is that, while the Windows GC is amazingly good, the GC on Xbox is garbage (pun intended).

So when coding an XNA game intended to run on XBox, it's absolutely crucial to time garbage collection to opportune moments, or you'll have horrible intermittent FPS hiccups.  Additionally, on XBox it's common to use structs way, way more often than you normally would, to minimize the number of objects that need to be garbage-collected.


答:
If there is not a good reason to not do so, I would definitely advocate a spin on option 3. As @jonsharp mentions, breaking up your utility into clean units of functionality is a good way to ensure testability. Even the smallest scripts can eventually morph into a much larger program and making sure that you have an extensible API sooner rather than later will alleviate much headache down the road.

The way I'd approach this is:

Break up your code into logical methods with clean and clear I/O

Add unit tests. Having them is never a bad thing.

Rather than using if __name__ == '__main__', create a main() (or similar) method containing your entry point

Use setuptool's setup() function to define the script entry point in your setup.py file.

For example:

```<code>from setuptools import setup
setup(
    name='mypackage',
    version='0.1',
    entry_points={
        'console_scripts': [ 'myscript = mypackage.mymodule:main' ],
    }
)
</code>```

Now, not only is all of your code (including main()) is easily unit testable, but you can still have your console entry point once you've done a python setup.py install|develop.

Any validation I do using, say,
  ArgumentParser.add_mutually_exclusive_group will not apply when my
  tool is run as a library instead of a command line script.

Depending on how your API is designed, you may need to add some extra validation to input parameters, but that should likely be there to prevent unexpected input anyways.

Edit: The only time I would use generally use subprocess is when I'm calling into a non-Python application or another Python script that I don't own or have the time to refactor, but the latter only being as a last resort. Most well-written Python utilities will expose both command line utilities and internal API.

Title: Preferred way to expand a command line script to be used as a library in Python

Body:
I have a useful Python script that I've been invoking from the command line. It has decent number of options, maybe 20, and it's not unusual to run the script with six or seven flags. Then the rest of the input comes via stdin.

Now I have some other Python code from which I'd like to call this useful little utility. Two options I can think of are:

I can use subprocess.call and invoke my little script

(A little better) I can cobble together a command line and then pass it as a list of strings to argparse

I can totally refactor the program so that the entry point of my utility is Python function call and then have my command line utility just call this function. In principle this seems like the responsible thing to do, but it does leave my managing two separate interfaces to my function. For example, I have to decide whether I want to let argparse know the default values for my options or have the function know the defaults (or have two sets of defaults). Any validation I do using, say, ArgumentParser.add_mutually_exclusive_group will not apply when my tool is run as a library instead of a command line script.

Is there a standard paradigm for creating a single interface in Python that is well-suited to being invoked both from Python and from the command line?

Body:
If there is not a good reason to not do so, I would definitely advocate a spin on option 3. As @jonsharp mentions, breaking up your utility into clean units of functionality is a good way to ensure testability. Even the smallest scripts can eventually morph into a much larger program and making sure that you have an extensible API sooner rather than later will alleviate much headache down the road.

The way I'd approach this is:

Break up your code into logical methods with clean and clear I/O

Add unit tests. Having them is never a bad thing.

Rather than using if __name__ == '__main__', create a main() (or similar) method containing your entry point

Use setuptool's setup() function to define the script entry point in your setup.py file.

For example:

from setuptools import setup
setup(
    name='mypackage',
    version='0.1',
    entry_points={
        'console_scripts': [ 'myscript = mypackage.mymodule:main' ],
    }
)

Now, not only is all of your code (including main()) is easily unit testable, but you can still have your console entry point once you've done a python setup.py install|develop.

Any validation I do using, say,
  ArgumentParser.add_mutually_exclusive_group will not apply when my
  tool is run as a library instead of a command line script.

Depending on how your API is designed, you may need to add some extra validation to input parameters, but that should likely be there to prevent unexpected input anyways.

Edit: The only time I would use generally use subprocess is when I'm calling into a non-Python application or another Python script that I don't own or have the time to refactor, but the latter only being as a last resort. Most well-written Python utilities will expose both command line utilities and internal API.


问:
Observer pattern without state change?
My question is whether there is a similar pattern to Observer that does not imply a state change in the observed object. The following is a concrete example:

I have a ConnectionFactory class which is responsible for creating JDBC database connections. Now I have the need to do something with every connection right after it has been created (specifically, I need to set a user-defined variable in the MySQL session) but I don't want to do it in the ConnectionFactory class since I feel this is something that goes beyond its responsibilities.

I have considered making ConnectionFactory extend java.util.Observable and having an Observer class that will be notified every time a connection is created. However it seems that this approach is incorrect, since the Observer pattern implies that the observed object is being observed for state changes, but the state of ConnectionFactory does not change at all when a connection is created.

As you can see, I have to go around this issue by simulating a state change before calling notifyObservers() (otherwise the observers will not be notified) and synchronizing those two statements in order to make sure that the "state changed" flag set in the first statement is still active when the second statement is executed. This kind of workaround just increases my suspicion that I have taken the wrong approach.

```<code>public class ConnectionFactory extends java.util.Observable {

    public static final ConnectionFactory INSTANCE = new ConnectionFactory();

    private ConnectionFactory(){};

    public Connection makeConnection(){

        Connection connection = null;

        // ... obtain the connection ...

        synchronized(this) {
            this.setChanged();
            this.notifyObservers(connection);
        }

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

```<code>public class Foo implements java.util.Observer {

    @Override
    public void update(Observable o, Object arg) {

        Connection con = (Connection)arg;

        // ... set MySQL user-defined variables ...

    }
}
</code>```

```<code>// Somewhere in initialization code...
ConnectionFactory.INSTANCE.addObserver(new Foo());
</code>```

So there is an object that I want to observe, there is a class that must do something when the observed object fires certain events, but those events are not state changes. If the Observer pattern is not applicable, then what would be a correct approach?

答:
If don't want to use the observer pattern, there could be another way. While I was reading your question, it became to me the idea to use the Decorator pattern.

You've said that you don't want to give to the class ConnectionFactory the responsibility to set a user-defined variable in the MySQL session. Then, why don't you decorate the Connection object with an external object, let's say MySqlConnection, that do the job on MySQL session at creation time?

A possibile solution could be this:

```<code>public class MySqlConnection extends Connection {
   // Decorated object
   private Connection connection;
   public MySqlConnection(Connection con) {
      this.connection = con;
      // Set the MySQL variable
      // ...
   }
   // Here you can redefine Connection methods if you need
   // to decorate some of them
}

public class ConnectionFactory {
   // ...
   public Connection makeConnection() {
      // Build a connection and then decorate it (code simplification)
      return new MySqlConnection(new Connection());
   }
}
</code>```

Title: Observer pattern without state change?

Body:
If don't want to use the observer pattern, there could be another way. While I was reading your question, it became to me the idea to use the Decorator pattern.

You've said that you don't want to give to the class ConnectionFactory the responsibility to set a user-defined variable in the MySQL session. Then, why don't you decorate the Connection object with an external object, let's say MySqlConnection, that do the job on MySQL session at creation time?

A possibile solution could be this:

public class MySqlConnection extends Connection {
   // Decorated object
   private Connection connection;
   public MySqlConnection(Connection con) {
      this.connection = con;
      // Set the MySQL variable
      // ...
   }
   // Here you can redefine Connection methods if you need
   // to decorate some of them
}

public class ConnectionFactory {
   // ...
   public Connection makeConnection() {
      // Build a connection and then decorate it (code simplification)
      return new MySqlConnection(new Connection());
   }
}


问:
Is an Optional/Maybe type useful in dynamic languages?
Optional is useful in an obvious way in a statically typed language, but I'm wondering if it's also useful in dynamic languages.  Usually there's no compiler telling you, "hey, you used this Optional<String> as a String", so you still have to discover your mistakes at runtime.  I have a static typing background and from my perspective I can't see the benefit of an Optional type in a dynamic language.

答:
That weakness is really no different than for all types in dynamic languages.  You have to run it to find out what's in it.  Options aren't really different in that regard.

Options are more than just a static annotation that a value may or may not be present.  They have operations associated with them as well.  Chains of mapped operations on options are one highly useful feature, that keeps you from having to check for failure after every step in the chain.

Even if you only immediately retrieved the value, it still would prod the programmer to check for Nothing the first time it ran and failed.  Without an option, that prodding would only take place if it happened to contain null the first time you ran it.  Many a null pointer dereference have stubbornly waited until the product was in the field.


问:
Are there too many parameters in this constructor?
Check this out:

```<code>public function __construct(
        \Magento\Framework\Model\Context $context,
        \Magento\Framework\View\DesignInterface $design,
        \Magento\Framework\Registry $registry,
        \Magento\Store\Model\App\Emulation $appEmulation,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\App\RequestInterface $request,
        \Magento\Newsletter\Model\Template\Filter $filter,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Newsletter\Model\TemplateFactory $templateFactory,
        \Magento\Framework\Filter\FilterManager $filterManager,
        array $data = []
    ) {
        parent::__construct($context, $design, $registry, $appEmulation, $storeManager, $data);
        $this->_storeManager = $storeManager;
        $this->_request = $request;
        $this->_filter = $filter;
        $this->_scopeConfig = $scopeConfig;
        $this->_templateFactory = $templateFactory;
        $this->_filterManager = $filterManager;
    }
</code>```

It's from https://github.com/magento/magento2/blob/develop/app/code/Magento/Newsletter/Model/Template.php#L107

The function declaration takes more space than the body.

I think it has something to do with dependency injection.

What's the benefit of structuring your code as above instead of something like this:

```<code>public function __construct(Magento $magento, array $data = []
    ) {
        parent::__construct($magento->context, $magento->design, $magento->registry, $magento->appEmulation, $magento->storeManager, $data);
        $this->_storeManager = $magento->storeManager;
        $this->_request = $magento->request;
        $this->_filter = $magento->filter;
        $this->_scopeConfig = $magento->scopeConfig;
        $this->_templateFactory = $magento->templateFactory;
        $this->_filterManager = $magento->filterManager;
    }
</code>```

Notice that I only need to create the Magento instance once. Then I pass it to all the classes that need stuff.

答:
How is your code easier than the first one?

What would Magento constructor look in your example? It would take the same parameters as the actual constructor from the first piece of code, and the only difference would be that instead of one class, you now have two classes.

Refactoring should improve code, not move the difficult part to a different class.

On the other hand, if you group logically related arguments together and replace them by a class, it may make sense. Looking at the first piece of code, I don't see any candidates for grouping. The code seems quite readable.


答:
I agree with Doc Brown on the matter of Liskov Substitution, but if you don't inherit from dict, how will you know you're supporting the right abstract interface? May I suggest using MutableMapping from the collections module?

The docstring for MutableMapping from the source looks like this:

```<code>"""A MutableMapping is a generic container for associating
key/value pairs.

This class provides concrete generic implementations of all
methods except for __getitem__, __setitem__, __delitem__,
__iter__, and __len__.

"""
</code>```

Which means those magic methods are all you need to implement to get the same dict functionality. It might look something like this (tested nominally in Python 2 and 3):

```<code>import collections

class LimitedDict(collections.MutableMapping):

    def __init__(self, maxlen=10):
        self.maxlen = maxlen
        self.latest = collections.deque(maxlen=maxlen)
        self.data = {}
    def __setitem__(self, key, value):
        '''ld[i] = y'''
        item_in = key in self.data
        if len(self.latest) >= self.maxlen and not item_in:
            self.latest.pop()
        elif item_in:
            self.latest.remove(key)
        self.latest.appendleft(key)
        self.data[key] = value
    def __delitem__(self, key):
        '''del ld[i]'''
        del self.data[key]
        self.latest.remove(key)
    def __getitem__(self, key):
        return self.data[key]
    def __iter__(self):
        for key in self.latest:
            yield key
    def __len__(self):
        return len(self.latest)
</code>```

And did not test much, but I did do this, and behaves as expected:

```<code>def main():
    ld = LimitedDict(3)
    ld['foo'] = 1
    print(ld['foo'])
    ld['bar'] = 2
    print(ld['bar'])
    del ld['bar']
    assert 'bar' not in ld
    ld['bar'] = 3
    ld['foo'] = 4
    ld['baz'] = 5
    ld['quux'] = 6
    assert 'bar' not in ld
    for key, value in ld.items():
        print(key, value)
    ld.update(foo=7, bar=8, baz=9)
    try:
        del ld['quux']
    except KeyError:
        raised = True
    assert raised
    assert not isinstance(ld, dict)
</code>```

This runs in both Python 2.6 and 3.3, and you can use e.g. iteritems in 2 too.

Also, setdefault is available, so you don't really need defaultdict, either. Here's the help on how it works:

```<code>>>> help(dict.setdefault)
Help on method_descriptor:

setdefault(...)
    D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D
</code>```

Body:
I agree with Doc Brown on the matter of Liskov Substitution, but if you don't inherit from dict, how will you know you're supporting the right abstract interface? May I suggest using MutableMapping from the collections module?

The docstring for MutableMapping from the source looks like this:

"""A MutableMapping is a generic container for associating
key/value pairs.

This class provides concrete generic implementations of all
methods except for __getitem__, __setitem__, __delitem__,
__iter__, and __len__.

"""

Which means those magic methods are all you need to implement to get the same dict functionality. It might look something like this (tested nominally in Python 2 and 3):

import collections

class LimitedDict(collections.MutableMapping):

    def __init__(self, maxlen=10):
        self.maxlen = maxlen
        self.latest = collections.deque(maxlen=maxlen)
        self.data = {}
    def __setitem__(self, key, value):
        '''ld[i] = y'''
        item_in = key in self.data
        if len(self.latest) >= self.maxlen and not item_in:
            self.latest.pop()
        elif item_in:
            self.latest.remove(key)
        self.latest.appendleft(key)
        self.data[key] = value
    def __delitem__(self, key):
        '''del ld[i]'''
        del self.data[key]
        self.latest.remove(key)
    def __getitem__(self, key):
        return self.data[key]
    def __iter__(self):
        for key in self.latest:
            yield key
    def __len__(self):
        return len(self.latest)

And did not test much, but I did do this, and behaves as expected:

def main():
    ld = LimitedDict(3)
    ld['foo'] = 1
    print(ld['foo'])
    ld['bar'] = 2
    print(ld['bar'])
    del ld['bar']
    assert 'bar' not in ld
    ld['bar'] = 3
    ld['foo'] = 4
    ld['baz'] = 5
    ld['quux'] = 6
    assert 'bar' not in ld
    for key, value in ld.items():
        print(key, value)
    ld.update(foo=7, bar=8, baz=9)
    try:
        del ld['quux']
    except KeyError:
        raised = True
    assert raised
    assert not isinstance(ld, dict)

This runs in both Python 2.6 and 3.3, and you can use e.g. iteritems in 2 too.

Also, setdefault is available, so you don't really need defaultdict, either. Here's the help on how it works:

>>> help(dict.setdefault)
Help on method_descriptor:

setdefault(...)
    D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D


问:
Relation between cache line and memory page

If I am correct, a page in a main memory is the smallest unit unit for transfer data between the main memory and an external storage device, such as a hard disk. A cache line of a main memory is the smallest unit for transfer data between the main memory and the cpu caches.

I wonder if a page size is always or best to be a natural number of cache line size?
If a cache line size is 64 byte, and a memory page size is 4KB, then each page has 4KB / 64 bytes == 64 cache lines in it.

Are a page and a cache line both fixed objects in a memory? Or are they just any contiguous block of a memory of a certain size, which can start and float anywhere within the memory?

Is it always that a cache line cannot span more than one pages, i.e. part of a cache line is in a page and the other part of the cache line is in another page?

Thanks.

答:
A cache line is the smallest unit that you can touch physical memory with. Meaning when you read/write 1 byte, a full cache line containing it is read into the cpu cache and written back. Note that even instructions that bypass the cache to write (ephemeral streaming instructions) write in cache line sizes. Depending on the CPU, cache line sizes are typically 32/64/128 bytes. When Memory pages are written to disk, they are written in whole. This will happen when the memory pressure is too high or with hibernating processes (and possibly for other uses). They will also be read whole when needed again. This is mainly because the kernel has no way of knowing if the page is partially or fully used. Other external storage read/write operations can have arbitrary granularity (eg: fwrite(..)).

The page size is CPU/GPU dependent. For most CPUs, page sizes will be at least 4KB and generally support a mix of 4KB/64KB/2MB/4MB/16MB/1GB (not necessarily all supported at the same time). The size will always be a power of two.

Both a page and a cache line are contiguous and require to be aligned to their respective size. A 64 byte cache line is always 64 byte aligned and a 2MB page is always 2MB aligned.

Because of #3, a cache line can never span 2 pages since page sizes will always be larger than a cache line and will always be a multiple of the size of a cache line.

See this excellent source for a wealth of information regarding memory:
http://lwn.net/Articles/250967/


答: It seems like your software could benefit from Dependency Injection. This way, the parent constructor could have its dependencies provided directly to it without having to burden all child classes with constructors that do little more than pass arguments along until they reach the constructor that actually needs them.</p>

Title: Are there too many parameters in this constructor?

Body:
Check this out:

```<code>public function __construct(
        \Magento\Framework\Model\Context $context,
        \Magento\Framework\View\DesignInterface $design,
        \Magento\Framework\Registry $registry,
        \Magento\Store\Model\App\Emulation $appEmulation,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\App\RequestInterface $request,
        \Magento\Newsletter\Model\Template\Filter $filter,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Newsletter\Model\TemplateFactory $templateFactory,
        \Magento\Framework\Filter\FilterManager $filterManager,
        array $data = []
    ) {
        parent::__construct($context, $design, $registry, $appEmulation, $storeManager, $data);
        $this->_storeManager = $storeManager;
        $this->_request = $request;
        $this->_filter = $filter;
        $this->_scopeConfig = $scopeConfig;
        $this->_templateFactory = $templateFactory;
        $this->_filterManager = $filterManager;
    }
</code>```

It's from https://github.com/magento/magento2/blob/develop/app/code/Magento/Newsletter/Model/Template.php#L107

The function declaration takes more space than the body.

I think it has something to do with dependency injection.

What's the benefit of structuring your code as above instead of something like this:

```<code>public function __construct(Magento $magento, array $data = []
    ) {
        parent::__construct($magento->context, $magento->design, $magento->registry, $magento->appEmulation, $magento->storeManager, $data);
        $this->_storeManager = $magento->storeManager;
        $this->_request = $magento->request;
        $this->_filter = $magento->filter;
        $this->_scopeConfig = $magento->scopeConfig;
        $this->_templateFactory = $magento->templateFactory;
        $this->_filterManager = $magento->filterManager;
    }
</code>```

Notice that I only need to create the Magento instance once. Then I pass it to all the classes that need stuff.


答:
There isn't anything significantly wrong with the first piece of code. Though having more than half a dozen dependencies may indicate a violation of the SRP, there is nothing inherently wrong with having a large constructor.

In fact, I would much prefer seeing a constructor with a large number of arguments than a method.  Constructors are for getting our ducks in a row so that our class can do what it needs to do.

Your replacement code is actually more dangerous as it is hiding the true dependencies of the class and it requires another object to be constructed just so that you can construct this object.


问:
C# Structs, 16 byte rec. - Are getters and setters included in that?
I was reading into the nitty-gritty of structs in C# here: "C Fundamentals, The differences between struct and class."
and at the very bottom it mentions keeping a struct below 16 bytes. I looked elsewhere and saw that it is commonly recommended, though it sounds like it's been bumped up a bit to 20-24 bytes here in later versions of .NET.

My question is, are they just adding up the ints, doubles, etc. to total the 16 bytes, or are they talking about the size after compile, and if so, wouldn't getters and setters add to that?

答:
The size of a struct refers to the struct's fields.

Automatic Properties include a field as part of their compiled structure; this field counts towards the size of the struct.

Otherwise, properties are no different from functions - they are not part of the struct's instance at all, since there's nothing particular to that instance in the property.


问:
Is it ok to break the "is a" relationship if I just want part of a class' functionality?
I asked a question about extending TreeMaps into "sort by value" TreeMaps on the "code review" site on stackoverflow. Based on the response, I re-wrote the code. I like it. But, it feels like I might be just using tricks. Or, maybe this is a legit way to write software? Can someone please take a look:

```<code>public class ToolBox {
    public interface SortedMapByValue {
        void put(Object key, Integer val);
        Set<Map.Entry<Object, Integer>> entrySet();
    }

    public static SortedMapByValue getInstanceSortedMapByValue() {
        class MyComp implements Comparator {
            Map<Object, Integer> sharedMap;

            public int compare(Object key1, Object key2) {
                if(key1.equals(key2)) { return 0; }

                Integer val1 = sharedMap.get(key1);
                Integer val2 = sharedMap.get(key2);

                if(val1 > val2) return -1;
                return 1;
            }
        }

        class MyMap<K> extends TreeMap<K, Integer> {
            Map<Object, Integer> sharedMap = new HashMap<Object, Integer>();
            MyMap(Comparator comp) {
                super(comp);
            }

            @Override
            public Integer put(K key, Integer val) {
                if(sharedMap.containsKey(key)) {
                    super.remove(key);
                    val += sharedMap.get(key);
                }
                sharedMap.put(key, val);
                super.put(key, val);
                return val;
            }
        }

        class TreeMapByValue implements SortedMapByValue {
            private MyMap<Object> realMap;

            public TreeMapByValue(MyMap myMap) { this.realMap = myMap; }

            public void put(Object key, Integer val) {
                realMap.put(key, val);
            }

            public Set<Map.Entry<Object, Integer>> entrySet() {
                return realMap.entrySet();
            }
        }

        MyComp myComp = new MyComp();
        MyMap<Object> myMap = new MyMap(myComp);
        myComp.sharedMap = myMap.sharedMap;

        SortedMapByValue treeMapByValue = new TreeMapByValue(myMap);
        return treeMapByValue;
    }
}
</code>```

The question is just whether it is ok to write classes that are fragile and that break super class' apis, as long as you wrap them in a static method? The TreeMapByValue does not break the "is a" relationship with TreeMap only because I hide it. That seems like a trick, but maybe that is a standard way to program? I don't know.

btw: the title refers to my wanting the "red-black" sort algorithm of TreeMap, but I didn't want what the api says regarding sort ordering. I kept the algorithm, but broke the api. But then I hid doing this.

Please ignore my complete ignorance of generics. I will study them soon enough.

答:
Is a and Has a are reasonable guidelines, very useful when you are learning, but in practice you should never use inheritence unless it's so blatently obvious that it's needed that there is just no question.

Inheritene often seems like a much neater solution, but people who have used it extensively have learned that it leads to difficult to maintain code in the long run.


问:
Static functions vs classes
Let's say that I want to build some utility functions to do some basic maths with the BigDecimals, for example I want to have a function that computes the average of a List<BigDecimal>.

What is the best approach? A static function or a utility class?

```<code>public static BigDecimal computeAverage(List<BigDecimal> numbers)
</code>```

or

```<code>public class BigDecimalUtil

public computeAverage(List<BigDecimal> numbers)
</code>```

答:
I voted for codingoutloud answer and I'd like to expand on it a little.

Wrapping collections in classes is something I've come to try to always do--my rule is "Never pass a naked collection".  The biggest failures of OO I've seen is when people are afraid to add methods where they belong.  In your case the methods belong on the collection (Totally obviously if you think about it), so put them there by wrapping the collection, and to put the icing on the cake, only expose the functionality you need outside your new class (as codingoutloud did) so that you can look at your single, small class and easily understand everything manipulating that collection.

You don't have to solve every possible problem because it's all your code, you can edit the collection wrapper just as easily as you can edit the other classes that interact with it.  This lets you pull other code into the class as necessary--if you don't ever expose the collection it will become quickly obvious what code belongs in the wrapper.

This also gives you complete control over the collection so you can prevent it from getting into a state inconsistant with it's function (For instance, if no entry in the collection can be null, just prevent anyone from putting a null in!)

Most of the bad OO code I've ever seen has been created because someone chose to write static utilities or inline code that belonged in objects they couldn't add code to instead of wrapping the offending objects in code of their own that they could modify.

Title: Static functions vs classes


问:
How can I build a seamless login for multiple sites using OAuth2?
The question is admittedly a bit leading, but what I mean to ask is:

If a user logs into into site X, is there a way to automatically log into site Y? Site Y utilises single sign-on via OAuth2 service on site X, so a login via site Y would log you into site X, but not vice versa (that is, login via site X would not give you a session/cookie for site Y)

Let's say I want to minimize the barriers for login, so if I can get a user authenticated into site Y without a single dialog, confirmation, or popup, that is ideal.

Take for example, Stack Exchange. How do they log you in automatically if your session cookie for Programmers SE has expired, but not the cookie for StackOverflow?

e.g. This!

One "solution" I thought of was:

Site X is configured to allow site Y authorization with an implicit grant, so users will no longer have to acknowledge authorization. Upon login via site X, open a hidden/invisible modal to siteY.com/oauth, which automatically authorises the user and saves a session cookie. Upon travel to site Y, user is simply continuing their session, as the cookie was created earlier.

Does that make sense?

答: We created the same feature using oAuth2. Probably use the method as what Site Y is using to automatically login to site X. Try to create an end point on Site Y where Site X can pass an accessToken. Then Site Y will validate the accessToken being passed by Site X if accessToken is valid then create a session. Hope this helps.</p>


问:
Do designer tools degrade the programming experience?
I've been looking around lately, specifically at some of the MS tools that are available, and I'm noticing a big focus on designer tools and wizards. Not just for UI development but for everything.

Entity Framework has the modeller

RIA Services has the DomainService wizard(s)

Workflow has the whole workflow designer thingy... (I don't know, haven't really used it)

There's more, but I think you get the idea. There's lots of designer tools. Using some of these I find that:

They complicate matters beyond the prescribed use cases

(ie all of the tech demo videos)

I have been evaluating some of these technologies recently, and trying to work with them I end up having to dissect exactly what the designers, modellers and wizards are doing for me... otherwise I'm lost when I actually have to try to do something with whatever was created.

This ends up being a case where I have to fight the tool, or fight its output enough such that I could have just done the whole thing myself without it - and had a much stronger understanding of what's going on. I find this particularly infuriating with the silverlight designer and RIA services domain service wizard.

I find myself asking "What good is this tool if I have to figure out its inner workings or re-write half of its output in order to use it?"

They're not as fast

The selling point of these tools is to increase productivity and this point may change over time using the tool, and doesn't necessarily apply to UI designers (though, in some cases it still does -> I'm looking at you silverlight designer). I find that I can hack some code much faster than I can drag-and-drop, resize, move, whatever in a designer.

The UI gets in the way of the model

Maybe this is just me, but when I am using anything reminiscent of a UML design tool I end up spending more time laying everything out so that my lines don't cross and so that I can see it all on the screen than modelling what I'm trying to achieve.

They're no fun

Half of the reason that I code for a living is that I enjoy it. Clicking checkboxes and selecting comboboxes and then fixing everything that comes out isn't fun.

I don't appear to be alone

The community seems to not want these tools either. The best example I can think of at the moment is Entity Framework Code-First.

So I ask:

Do designer tools actually improve productivity?

Are they fun-killers?

Is 'the community' actually asking for more designer tools, or are vendors just thinking we are?

答:
My guess is that good design tools do improve productivity and don't take away from "fun".  However, it's only a guess because I have yet to come across a good design tool.

I personally have not asked for design tools.


问:
How can QA staff test caching logic that they can't see?
I just implemented a caching layer in my web application, and now I'm wondering how QA is supposed to test it, since caching is transparent to the user.

One idea I have is to put logging in the methods that invoke the code that populate the cache, and record when an object is pulled from cache and when it requires recreation from the database, and then the testers could view the logs to see if, for example, a certain object is reloaded from the db every 10 minutes, instead of every page view.

But can anyone suggest some better practices for this situation?

答: Some things are better tested by a programmer, perhaps the one who wrote the code, using unit tests. Testing the correctness of your caching code is one of those things. (From the way you ask this question, I presume that your QA people treat the application as a "black box" and test it through its external interface.)</p>


问:
Optional Member Data
I am creating a strong binding of an XML specification and I am essentially trying to "flatten" the specification as much as possible, so that it won't feel like you are manipulating an XML tree.

There are many elements in the XML specification that are optional.  Here is a contrived example.

<person>
   <name>Fred</name>
   <favorite-color>Blue</favorite-color>
</person>
```

<p>In this the "name" element might be required, but the "favorite-color" element might say minOccurs=0 in the xsd making it optional.</p>

<p>I have two ideas of how to handle this, neither of which seems particularly elegant.</p>

<p>In Version1 you must test for nullptr before dereferencing getFavoriteColor's return to see if FavoriteColor is being used by your Person object.  This seems like a bad approach because it makes it ridiculously easy to crash your program, especially because it might be hard to keep track of which members are optional and which are required.  You may end up having to test everything for null all the time.  This seems terrible.</p>

<p>In Version2 you can assume that the getters will not return nullptr's.  You must check the "HasFavoriteColor" bool to find out whether or not FavoriteColor exists.  But the tradeoff is that it seems to violate the rule of least surprise.  You may setFavoriteColor( "Blue" ) then be surprised when your XML output doesn't show Blue because you forgot to setHasFavoriteColor( true ).</p>

<p>Which of these designs seems better, or at least more idiomatic?  Is there a better solution?</p>

<p>Research: this is similar but doesn't fit https://stackoverflow.com/questions/3027067/optional-member-objects
This is similar, but again doesn't seem to fit Checking for the presence of optional properties of an object</p>

<p>Also, I discovered that boost::optional exists, but that doesn't seem like a fit and I am not using anything but std and STL for my project.</p>

<p>(Assume "Name" and "FavoriteColor" below are user classes and not just typedefs of std::string.)</p>

<pre class="lang-cpp prettyprint-override"><code>using Name = std::string;
using NamePtr = std::shared_ptr<Name>;

using FavoriteColor = std::string;
using FavoriteColorPtr = std::shared_ptr<FavoriteColor>;

/* Version 1: nullptr indicates FavoriteColor is absent,
 but Name will never be null because it is required */
class Person
{
public:
    Person()
    :myName( std::make_shared<Name>() )
    ,myFavoriteColor()
    {}

    NamePtr getName() const { return myName; }
    void setName( const NamePtr& value )
    {
        if( value )
        {
            myName = value;
        }
    }
    FavoriteColorPtr getFavoriteColor() const { return myFavoriteColor; }
    void setFavoriteColor( const FavoriteColorPtr& value ) { myFavoriteColor = value; }
private:
    NamePtr myName;
    FavoriteColorPtr myFavoriteColor;
};

/* Version 2: FavoriteColor and Name can both
 be trusted to not be null, but you have to
 check HasFavoriteColor bool to see whether
 the optional data is legit */
class Person2
{
public:
    Person2()
    :myName( std::make_shared<Name>() )
    ,myFavoriteColor( std::make_shared<FavoriteColor>() )
    {}

    NamePtr getName() const { return myName; }
    void setName( const NamePtr& value )
    {
        if( value )
        {
            myName = value;
        }
    }
    FavoriteColorPtr getFavoriteColor() const { return myFavoriteColor; }
    void setFavoriteColor( const FavoriteColorPtr& value )
    {
        if ( value )
        {
            myFavoriteColor = value;
        }
    }
    bool getHasFavoriteColor() const { return myHasFavoriteColor; }
    void setHasFavoriteColot( const bool value ) { myHasFavoriteColor = value; }
private:
    NamePtr myName;
    FavoriteColorPtr myFavoriteColor;
    bool myHasFavoriteColor;
};
</code>```

答:
I don't know C++ but I hope my idea helps you to resolve your problem.

Create your model class based on your XSD file.

```<code>class Person
{
   private string name;
   private string favoritColor;

  public Person (XmlElement person)
  {
    // parse the XML element and assign the values to appropriate element
    // if the element does not exist, don't assign the values to its equivalent element
  }

  public string GetName()
  {
     return name;
  }

  public bool HasFavoriteColor()
  {
     if (favoritColor ! = null)
     {
       return true;
     }
     return false;
  }

  public string GetFavoriteColor()
  {
    return favoritColor;
  }

}
</code>```

Title: Optional Member Data

Body:
I don't know C++ but I hope my idea helps you to resolve your problem.

Create your model class based on your XSD file.

class Person
{
   private string name;
   private string favoritColor;

  public Person (XmlElement person)
  {
    // parse the XML element and assign the values to appropriate element
    // if the element does not exist, don't assign the values to its equivalent element
  }

  public string GetName()
  {
     return name;
  }

  public bool HasFavoriteColor()
  {
     if (favoritColor ! = null)
     {
       return true;
     }
     return false;
  }

  public string GetFavoriteColor()
  {
    return favoritColor;
  }

}


答:
The first approach you mentioned is absolutely perfect and there is no need for code refactoring. Dependency Injection is very useful for writing test cases on your class. Because you can easily mock the injected class functionality while you are writing test cases as per your need.

The second approach you mentioned is a wrong one. Because, you are passing entire Magento framework object to your class. Which means that your are exposing all unwanted features inside your class (No Encapsulation!).

If you are planning to pass the variables to this class i will definitely suggest you do create one wrapper class for that. But here you are passing the objects so there is no need for wrapper class. If we do so, It will increase the complexity of your code.


问:
eBook editions of programming books
(I'll get my justification for why this is on topic in early: programming books tend to have fairly specific formatting needs - code samples, tables, images and graphs - which are not common to all book types and are not necessarily well handled by eBook readers.  Similarly they're used in different ways - you often dip in and out rather than read cover to cover.)

I've just noticed that Don't Make Me Think by Steve Krug is available as an eBook edition for the Kindle (and presumably also for other readers) which set me thinking.

There are certain advantages to eBook readers for tech books - primarily that you can carry a massive library of what would be heavy physical books around very easily.  The downside is that certain eBook readers allegedly aren't particularly well equipped to cope with tables, code samples and so on and a book like Don't Make Me Think presumably makes extensive use of these sorts of things.

So, the question, what are your experiences of reading and using programming books on an eBook reader and would you recommend it? I'm specifically interested in the latest generation Kindle but happy to hear about all devices - might be useful to state which one you use in the answer.

答:
I've had a Kindle for a couple of years now (I got one just after they first came out).  I love it for reading fiction and mainstream non-fiction, but it doesn't handle technical material (figures, tables, program listings, etc.) very well.

So for this Christmas I've asked for a Kindle DX, which has a much larger screen with 50% better contrast compared to my first generation device. If I get it, I still plan to use the smaller one for fiction and other light reading.

A number of technical books have been optimized for the DX.  O'Reilly now has nearly 1000 books available for the Kindle.

Update: I've now had my new Kindle DX for a couple of months and love it.  The large screen makes all the difference when it comes to reading technical books.  PDF's display especially well -- I have been emailing all my PDF documents to my Kindle and read them there instead of on the PC.

Title: eBook editions of programming books


问:
How to create a JSON or HTML file that dynamically mirrors file directory structure for jsTree 3.0
I am looking to create a JSON OR HTML file that will accurately give the folder/file structure for a directory for use with jsTree 3.0 (www.jstree.com). I want to make it AJAX/lazy load so that each file folder is loaded when the user requests it. Each link in the jsTree will be used to fire a function.  There are a few good pages on Google that mentioned NodeJS, NPM, Grunt, angularJS, expressJS... the list goes on ... but its a bit over my head as a beginner.

I had looked at using HTML data to make the tree structure, but I again don't know how to do this 'dynamically', or work with the server side. I'd like to check the directory structure at page load after the document is ready, then have the tree read the JSON file. It could be JSON or HTML in my mind; as long as I can have onclick functions for each individual link.

How do I do this? Is there a better way to do this? It seems like it should be pretty common territory... just not for a beginner I guess.

答:
HTML as well as JSON are not "programming languages". HTML is a Markup Language, and JSON is more of a "Data structure".

They are "standard ways" to organize information. They're not the tool to organize it.

Since you are considering JSON and HTML, JavaScript seems like a pretty good approach (Albeit you could use PHP or Ruby or whatever).

For starters, you need to be able to travel to inner folders. Possible approaches include recursion and functions that call each other.

Follows a Gist that does exactly that (Goes "inside" every element on a folder tree). It is in JS, to run in NodeJS, and has two functions, dir() and dirSync(). dir() is more "pure JS" while dirSync() is more easily portable to other programming paradigms.
https://gist.github.com/frnco/bbfdb86d8ac14d5a52b4

I didn't save this anywhere, but I inserted comments where they may be useful.

To run this gist you need NodeJS (Installs fine on Windows) and nothing more. Just save it with a .js extension and run node file.js or simply run node on a terminal and paste the code (But no matter what, remove the last line, dir('/something-that-is-not-too-deep');).

Also, Node is not the onnly option, I just used it since it seems simpler to me to start with it.

This should help you start running node (Or porting it to another language) and traversing folder structures. Then it's just a matter of properly saving this information on a variable.


答:
Both of your designs have the problem that they put the burden of keeping track of the validity of the favoriteColor attribute on the user of the class. Your second design (with the getHasFavoriteColor) has the additional problem that it duplicates the information if there is a valid value for favoriteColor in a way that makes it very easy to create inconsistencies.

Of the two designs you have, the first one is the more idiomatic one.

Some other possibilities are:

Add a member bool hasFavoriteColor() const { return myFavoriteColor != NULL; }. It makes the check against NULL look better in the client code, but isn't really that different.

You could have a special 'empty' FavoriteColor object that gets returned when the user doesn't have a favorite color. That way you never have to return a NULL pointer.

Title: Optional Member Data

Body:
I am creating a strong binding of an XML specification and I am essentially trying to "flatten" the specification as much as possible, so that it won't feel like you are manipulating an XML tree.

There are many elements in the XML specification that are optional.  Here is a contrived example.

<person>
   <name>Fred</name>
   <favorite-color>Blue</favorite-color>
</person>
```

<p>In this the "name" element might be required, but the "favorite-color" element might say minOccurs=0 in the xsd making it optional.</p>

<p>I have two ideas of how to handle this, neither of which seems particularly elegant.</p>

<p>In Version1 you must test for nullptr before dereferencing getFavoriteColor's return to see if FavoriteColor is being used by your Person object.  This seems like a bad approach because it makes it ridiculously easy to crash your program, especially because it might be hard to keep track of which members are optional and which are required.  You may end up having to test everything for null all the time.  This seems terrible.</p>

<p>In Version2 you can assume that the getters will not return nullptr's.  You must check the "HasFavoriteColor" bool to find out whether or not FavoriteColor exists.  But the tradeoff is that it seems to violate the rule of least surprise.  You may setFavoriteColor( "Blue" ) then be surprised when your XML output doesn't show Blue because you forgot to setHasFavoriteColor( true ).</p>

<p>Which of these designs seems better, or at least more idiomatic?  Is there a better solution?</p>

<p>Research: this is similar but doesn't fit https://stackoverflow.com/questions/3027067/optional-member-objects
This is similar, but again doesn't seem to fit Checking for the presence of optional properties of an object</p>

<p>Also, I discovered that boost::optional exists, but that doesn't seem like a fit and I am not using anything but std and STL for my project.</p>

<p>(Assume "Name" and "FavoriteColor" below are user classes and not just typedefs of std::string.)</p>

<pre class="lang-cpp prettyprint-override"><code>using Name = std::string;
using NamePtr = std::shared_ptr<Name>;

using FavoriteColor = std::string;
using FavoriteColorPtr = std::shared_ptr<FavoriteColor>;

/* Version 1: nullptr indicates FavoriteColor is absent,
 but Name will never be null because it is required */
class Person
{
public:
    Person()
    :myName( std::make_shared<Name>() )
    ,myFavoriteColor()
    {}

    NamePtr getName() const { return myName; }
    void setName( const NamePtr& value )
    {
        if( value )
        {
            myName = value;
        }
    }
    FavoriteColorPtr getFavoriteColor() const { return myFavoriteColor; }
    void setFavoriteColor( const FavoriteColorPtr& value ) { myFavoriteColor = value; }
private:
    NamePtr myName;
    FavoriteColorPtr myFavoriteColor;
};

/* Version 2: FavoriteColor and Name can both
 be trusted to not be null, but you have to
 check HasFavoriteColor bool to see whether
 the optional data is legit */
class Person2
{
public:
    Person2()
    :myName( std::make_shared<Name>() )
    ,myFavoriteColor( std::make_shared<FavoriteColor>() )
    {}

    NamePtr getName() const { return myName; }
    void setName( const NamePtr& value )
    {
        if( value )
        {
            myName = value;
        }
    }
    FavoriteColorPtr getFavoriteColor() const { return myFavoriteColor; }
    void setFavoriteColor( const FavoriteColorPtr& value )
    {
        if ( value )
        {
            myFavoriteColor = value;
        }
    }
    bool getHasFavoriteColor() const { return myHasFavoriteColor; }
    void setHasFavoriteColot( const bool value ) { myHasFavoriteColor = value; }
private:
    NamePtr myName;
    FavoriteColorPtr myFavoriteColor;
    bool myHasFavoriteColor;
};
</code>```