问:
How to structure this program?
I am struggling with the best way to structure a program that I am writing because the method I currently have feels very clunky and each part depends a lot on the others.  This is what the program needs to do:

Parse a word document to extract a list of contacts.

Get a list of contacts in a specified group from my Google account.

Compare the contacts from the word document to the Google contacts and either:

Update the contacts on Google with the details from the word document or

Create a new contact on Google if one doesn't exist or

Delete any contacts on Google that are not in the word document.

I also want

to have a GUI for this program that is responsive while all of the above is going on

the show a preview of the changes that steps 4-6 will make before actually performing them and allow the user to reject the changes (all or nothing would be fine for now)

Edit: everything from here on down is what I've currently done.  I added it so that specific refactoring advice could be given if that was what was deemed necessary.  If you have a way that I can structure this program then you can mostly likely skip the rest of it.

I've already made a start so allow me to explain how I've written the code so far.  The program is written in C#.

The parsing of the word document to get a list of contacts is in a class called WordDocumentParser.  It has a constructor which takes the path of the word document and a GetContacts method which performs all of the parsing and returns a list of contacts.  For what it's worth, I'm using COM to handle the parsing.

Steps 2-6 from above all start from a new class called GoogleContactsMaintainer with a constructor that takes the list of contacts from the word document, a couple of important groups on Google and a ContactsRequest which is used to retrieve/update/save/delete contacts on Google.  The first thing this class does is to get the contacts from Google and match them to the contacts from the word document.  The matched Google contact is saved in a property on the word document contact.

Then I do a preview which is the next 3 steps.

I start off with updating the contacts that have been matched from the word document to the Google contacts.  I create a ContactUpdater which updates the matched Google contact but doesn't commit the changes.  It also adds to a list of strings any changes that have been made and sets a boolean property on the contact to say that it has been updated.

Next is creating new Google contacts for those word document contacts that haven't been matched to a Google contact.  This is done in a ContactCreator class which creates a Google contact, sets it on the word document contact and sets a flag on that contact to indicate it is a new contact.  The new contact is not saved.

Last is to delete any Google contacts that haven't been matched to a contact from the word document.  To do this, I just find those contacts and add them to a list with a description saying that they will be deleted.

At the minute, I display these changes to the user and then immediately update them (I'll explain why the user can't stop this in a minute).  This is done in the following three stages.

Update: find any contacts from the word document that have the HasBeenUpdated property set, construct a new ContactUpdater and commit the changes.

Create: find any contacts from the word document that have the NewContact property set, construct a new ContactCreator and insert the new Google contact.

Delete: find any Google contacts that haven't been matched to a contact from the word document and delete them from Google.

In an attempt to keep my GUI responsive while all of this is going on, I created a MaintainContactsWorker class which subclasses BackgroundWorker and calls all of the above 10 steps from the OnDoWork method.  This is why I am unable to ask the user if they want to commit the changes or not.

To me, everything feels horribly inter-connected and completely untestable so I am hoping you can point out things that I should be doing to break the dependencies and make my code more maintainable and testable.

From writing this post, I one thing that I feel I should have is a repository which would handle the CRUD operations I am performing on the Google contacts.  Does this seem like a good place to start?

Thank you for any suggestions you can offer, hopefully I've explain myself well enough.

答:
Your process:

Parse a word document to extract a list of contacts.

Get a list of contacts in a specified group from my Google account.

Compare the contacts from the word document to the Google contacts and either:

Update the contacts on Google with the details from the word document or

Create a new contact on Google if one doesn't exist or

Delete any contacts on Google that are not in the word document.

One possible structure:

```<code>WordDocParser docParser = new WordDocParser(path);
IList<Contact> wordContacts = docParser.ParseContacts();

GoogleContactsManager googleManager = new GoogleContactsManager(accountInfo);
IList<Contact> googleContacts = googleManager.GetContacts();

ContactComparer comparer = new ContactComparer();
IList<ContactUpdateAction> updateActions =
    comparer.Compare(wordContacts, googleContacts);

//user can edit/alter update actions here

googleContacts.UpdateContacts(updateActions);
</code>```

For a responsive GUI, showing what step is happening with a progress bar should suffice up to the point where the user can see/alter the update actions. You can use a background/worker thread for each step, with a button to allow the user to cancel the process. The background thread can update a flag (Mutex or similar) when it is done to signal for the launch of the next background step.

Each UpdateAction can specify whether to add, delete, or change the contact information. This information can be displayed to the user before making changes to the Google account (at least in theory!) When the user is satisfied, they can click a Commit Changes button to execute the last step, or Cancel.

Title: How to structure this program?

Body:
Your process:

Parse a word document to extract a list of contacts.

Get a list of contacts in a specified group from my Google account.

Compare the contacts from the word document to the Google contacts and either:

Update the contacts on Google with the details from the word document or

Create a new contact on Google if one doesn't exist or

Delete any contacts on Google that are not in the word document.

One possible structure:

WordDocParser docParser = new WordDocParser(path);
IList<Contact> wordContacts = docParser.ParseContacts();

GoogleContactsManager googleManager = new GoogleContactsManager(accountInfo);
IList<Contact> googleContacts = googleManager.GetContacts();

ContactComparer comparer = new ContactComparer();
IList<ContactUpdateAction> updateActions =
    comparer.Compare(wordContacts, googleContacts);

//user can edit/alter update actions here

googleContacts.UpdateContacts(updateActions);

For a responsive GUI, showing what step is happening with a progress bar should suffice up to the point where the user can see/alter the update actions. You can use a background/worker thread for each step, with a button to allow the user to cancel the process. The background thread can update a flag (Mutex or similar) when it is done to signal for the launch of the next background step.

Each UpdateAction can specify whether to add, delete, or change the contact information. This information can be displayed to the user before making changes to the Google account (at least in theory!) When the user is satisfied, they can click a Commit Changes button to execute the last step, or Cancel.


问:
Where is the M in MVC?
I'm trying to refactor my application into MVC, but I'm stuck on the M part.

In a database-backed app, the model is implemented in the app code, right?

But then, what is in the database -- is that not also the model?

(I'm not using the database as a simple object store -- the data in the DB is an enterprise asset).

答:
The database is an implementation detail of the model. The model should be a full Domain Model and should combine data and process. The separation should be between difference concerns and not between a process and the data related to that process.

See also: http://martinfowler.com/bliki/AnemicDomainModel.html

ContentLicense: CC BY-SA 3.0

Title: Where is the M in MVC?


问:
Are null references really a bad thing?
I've heard it said that the inclusion of null references in programming languages is the "billion dollar mistake".  But why?  Sure, they can cause NullReferenceExceptions, but so what?  Any element of the language can be a source of errors if used improperly.

And what's the alternative?  I suppose instead of saying this:

Customer c = Customer.GetByLastName("Goodman"); // returns null if not found
if (c != null)
{
    Console.WriteLine(c.FirstName + " " + c.LastName + " is awesome!");
}
else { Console.WriteLine("There was no customer named Goodman.  How lame!"); }
```

<p>You could say this:</p>

<pre class="lang-cs prettyprint-override"><code>if (Customer.ExistsWithLastName("Goodman"))
{
    Customer c = Customer.GetByLastName("Goodman") // throws error if not found
    Console.WriteLine(c.FirstName + " " + c.LastName + " is awesome!");
}
else { Console.WriteLine("There was no customer named Goodman.  How lame!"); }
</code>```

But how is that better?  Either way, if you forget to check that the customer exists, you get an exception.

I suppose that a CustomerNotFoundException is a bit easier to debug than a NullReferenceException by virtue of being more descriptive.  Is that all there is to it?

答:
Nulls aren't so bad, unless you're not expecting them. You should need to explicitly specify in code that you're expecting null, which is a language-design problem. Consider this:

Customer? c = Customer.GetByLastName("Goodman");
// note the question mark -- 'c' is either a Customer or null
if (c != null)
{
    // c is not null, therefore its type in this block is
    // now 'Customer' instead of 'Customer?'
    Console.WriteLine(c.FirstName + " " + c.LastName + " is awesome!");
}
else { Console.WriteLine("There was no customer named Goodman.  How lame!"); }
```

If you try to invoke methods on a Customer?, you should get a compile-time error. The reason more languages don't do this (IMO) is that they don't provide for the type of a variable to change depending on what scope it's in. If the language can handle that, then the problem can be solved entirely within the type system.

There's also the functional way to handle this problem, using option-types and Maybe, but I'm not as familiar with it. I prefer this way because correct code should theoretically only need one character added to compile correctly.

Title: Are null references really a bad thing?

Body:
Nulls aren't so bad, unless you're not expecting them. You should need to explicitly specify in code that you're expecting null, which is a language-design problem. Consider this:

Customer? c = Customer.GetByLastName("Goodman");
// note the question mark -- 'c' is either a Customer or null
if (c != null)
{
    // c is not null, therefore its type in this block is
    // now 'Customer' instead of 'Customer?'
    Console.WriteLine(c.FirstName + " " + c.LastName + " is awesome!");
}
else { Console.WriteLine("There was no customer named Goodman.  How lame!"); }

If you try to invoke methods on a Customer?, you should get a compile-time error. The reason more languages don't do this (IMO) is that they don't provide for the type of a variable to change depending on what scope it's in. If the language can handle that, then the problem can be solved entirely within the type system.

There's also the functional way to handle this problem, using option-types and Maybe, but I'm not as familiar with it. I prefer this way because correct code should theoretically only need one character added to compile correctly.


问:
What is the role of traditional issue tracker when Scrum / Kanban board is used?
From a very high level view, to me it seems there are generally 2 types of Project Management tools:

Traditional issue trackers like Fogbugz, JIRA, BugZilla, Trac, Redmine etc.

Virtual card boards / agile project management tools like Pivotal Tracker, GreenHopper, AgileZen, Trello etc.

Sure, they overlap in one way or another, e.g. Pivotal Tracker tasks can be imported to JIRA, GreenHopper itself is implemented on top of JIRA issue base etc. but I think one can still see the difference in orientation between those two types of tools.

Traditional issue tracker seems to be used even in companies otherwise doing agile project management. My question is, why do they do that? I also feel that we should use an issue tracker in my company but when I'm thinking about it, I'm not actually sure why should we need it.

For example, Trello development seems to be managed by using Trello itself (see this virtual wall) even though they have access to Fogbugz, one of the best issue trackers around. So maybe we don't need traditional issue tracker when we'll be doing 100% of our work in an agile manner using one of the agile PM tools?

答: Some products are a little more optimized for stories and tasks versus defects, but really there isn't a big difference. Any good agile PM tool that doesn't impose too much overhead in terms of enforced structure or required fields, can easily be used for defect tracking. My current project is using a single tool for both tasks and defects and it works ok aside from the product being a POS :)</p>


问:
What implementation do you use when dealing with operators in .Net?
My question is in a sense a follow up of this post on Time Series in .Net.

Ideally, you would like to expand the basic TimeSeries<T> class by something like NumericTimeSeries<T>, where you would want to make sure that the type T implements the common numerical operators +, -, *, `/', ...

I am not sure what would be the most intelligent way to do this.

My first idea is to create an abstract class Number, with abstract methods Add(Number n1,Number n2), Multiply(Number n1,Number n2), Subtract(Number n1,Number n2), Divide(Number n1,Number n2) (and so on). I would then call these methods in the operator overloads.

I can then create concrete classes such as IntNumber, DoubleNumber, MatrixNumber, ComplexNumber (and so on).

The problem is that in the methods implementation, I would have to make an awful switch to determine what the class of each parameter is, and it's not very elegant.

Would you have done it the same way?

Has something already be done?

In F# I would use and inline functions, or pattern matching using discriminated unions. But what should I do in C#?

EDIT:

There was an awesome answer by Olivier Jacot-Descombes. However, I would like to specify a feature.

The numbers can apply the operator between them (you can multiply a matrix with a scalar for example). The problem with this is that you cannot know in advance the type of the result; multiplying a vector (transposed) and a vector returns a double...

答:
My way of dealing with this is to run screaming from the room.

No, I'm not kidding, exactly. I've seen people try to use operator overloading in several different projects. In each of those projects, there came a point where the programmers lost days, as in "multiple whole days", trying to figure out a problem that turned out to be caused by the "wrong" overloaded operator getting called in some situation where it wasn't expected. This is in addition to the hours it takes to set up all the conversions and versions of the operators so that you THINK it works.

"MyNumber.Add(otherNumber)" looks a little clunky, but it's readable. You know what's happening and what code is getting called. "MyNumber + otherNumber" could be any of half a dozen different things, and you have no idea which one it is until you get down into the assembly.

Don't ever use operator overloading.


问:
What programmers should do if there are no tasks in hand for few days?

Possible Duplicate:

How to productively spend “free time” at the office

Sometimes, especially at the end of the year in software companies, there is less work for the team, which happens in my company.

What should I ask my developers team to do during these cold days?

What things can they do to keep them focused and improve the company in the next year and future?

For sure there are tons of things to be done, I am just after some recommendations of new/fun things you do.

答:
There are tons of things to do for a team. Here is a small list for the team:

1-Close open issues (if any) in the projects delivered (if this is in your scope of work)

2-Prepare write-ups about mistakes in the last project(s) and how to avoid them in the future

3-Prepare educational sessions about new features in the tools that are useful but never found the time to use

4-Research best practices in different aspects of software development relevant to your software tools

5-Consider automation of repetitive tasks by building small utilities or templates

6-Research on new tools in the market that can make the next development faster and better

7-Ask the more experienced developers to teach the juniors some useful techniques

8-Attempt to do some team bonding and team building activities

9-Let people know more about each other's roles. Let developers see what DBAs do more closely and what issues they face. You may find that some of your developers could fill up for a dba for a holiday or so

10-Review your coding standards, backup strategy, disaster recovery, licenses, etc.

11-Study problems faced by the team members in detail and put plans to fix them

12-Consider building or using a Wiki for the team

13-Polish your company's web site

ContentLicense: CC BY-SA 4.0


问:
JavaScript (jQuery) And CSS Styling
Lets say I am building a jQuery plugin that has a number of non-configurable css attributes it needs to apply to DOM elements the plugin itself creates.  Is it consider better practice to apply the css directly in the javascript code or is it better to create a css file and just include both the css and javascript files?

I have always been under the impression that is would be better to have the css file just because it seems a bit cleaner and you don't really gain much from having everthing in the javascript code. I mean the size of the css + javascript files would probably be about the same as if you had applied the css through javascript so the only thing you gain is that you only need to request 1 resource file instead of 2 if you apply the css in javascript.  I am just curious as I an building a number of jQuery plugins and I have noticed some other plugins that are embedding a lot of css in the javascript instead of a css file.

答: As a user, I find it easier to manage jQuery plugins that are a single JavaScript file, rather than those that require additional CSS files.</p>

Title: JavaScript (jQuery) And CSS Styling


答:
Nothing style-wise is actually non-configurable. Someone using your plugin can always use a selector for the plugin and use !important when setting the style attribute.

Having said that, knowing that your CSS can be overridden, there's no reason to design around it and make it as difficult as possible for someone to alter it. Therefore, I'd say that separating your styles out into a .css file makes ultimate sense as it's easier for someone to alter your styles and it separates style from implementation.

Title: JavaScript (jQuery) And CSS Styling

Body:
Lets say I am building a jQuery plugin that has a number of non-configurable css attributes it needs to apply to DOM elements the plugin itself creates.  Is it consider better practice to apply the css directly in the javascript code or is it better to create a css file and just include both the css and javascript files?

I have always been under the impression that is would be better to have the css file just because it seems a bit cleaner and you don't really gain much from having everthing in the javascript code. I mean the size of the css + javascript files would probably be about the same as if you had applied the css through javascript so the only thing you gain is that you only need to request 1 resource file instead of 2 if you apply the css in javascript.  I am just curious as I an building a number of jQuery plugins and I have noticed some other plugins that are embedding a lot of css in the javascript instead of a css file.


问:
Resources on Writing Efficient C Code for Micro-controllers?
Serious help needed here. I love programming. I've been reading bunch of books(such as K&R) and articles/forums online for C language lately. Even tried looking into Linux code(although, i was lost where to start but peeking into small libraries helped?).

I started as a Java programmer and in Java it's pretty cut and dry; If programs gets too big, slice it in classes then further into functions. Guidelines like, keep the code readable and add comments. Use information hiding and OOP techniques. Some of which still applies for C.

I've been coding in C now and so far i get programs to work one way or the other. A lot of people talk about performance/efficiency, algorithm/design, optimization, and maintainability. Some people stress one more then the other but for non-professional software engineers you often hear something like e.g: Linux kernel dev won't just take any code.

My question is this: I plan on writing a code for 8-bit microcontroller without wasting any resources. Know that i'm coming from java background so things are not the same anymore... resources/books/links/tips will be much appreciated. Performance and size now matters. Resources/Tricks on efficient(within best practices) C code for 8-bit micro-controllers?

Also, inline assembly plays a vital role as well sticking close to micro-controllers standard. But are there general rule of thumb to efficiency that applies to all?

For example:
register unsigned int variable_name; is preferred over char anytime. Or using  uint8_t  if you don't need big numbers.

EDIT: Thank you so much for all the answers and suggestions. I appreciate everyone's effort for sharing knowledge.

答:
I have 20+ years of embedded systems, mostly 8 and 16 micros. The short answer to your question is the same as any other software development - don't optimise till you know you need to, and then don't optimise till you know what you need to optimise. Write you code so it's reliable, readable and maintainable first. Premature optimisation is as much, if not more of, a problem in embedded systems

When you program "without wasting any resources.", do you consider your time a resource? If not, who is paying you for your time, and if no one, do you have anything better to do with it. Once choice any embedded system designer has to make is the cost of hardware vs cost of engineering time. If you will be shipping 100 units, use a bigger micro, at 100,000 units, a $1.00 saving per unit is the same as 1 man year of software development (ignoring time to market, opportunity cost etc), at 1 Million units, you start getting ROI for being obsessive about resource usage, but be careful because many an embedded project never made the 1 million mark because they designed to sell 1 million (High initial investment with low production cost), and went bust before they got there.

That said, things you need to consider and be aware of with (small) embedded systems, because these will stop it working, in unexpected ways, not just make it go slow.

a) Stack - you usually have only a small stack size and often limited stack frame sizes. You must be aware of what your stack utilisation is at all times. Be warned, stack problems cause some of the most insidious defects.

b) Heap - again, small heap sizes so be careful about unwarranted memory allocation. Fragmentation becomes an issue. With these two, you need to know what you do when you run out - it does not happen on a large system due OS provided paging. i.e. When malloc returns NULL, do you check for it and what do you do. Every mallow needs a check and handler, code bloat?. As a guide - don't use it if there’s an alternative. Most small systems do not use dynmaic memory for these reasons.

c) Hardware interrupts - You need to know how to handle these in a safe and timely manner. You also need to know how to make safe re-entrant code. For instance, C standard libs are generally not re-entrant, so should not be used inside interrupt handlers.

d) Assembly - almost always premature optimisation. At most a small amount (inlined) is needed to achive something that C just cannot do. As an exercise, write a small method in hand crafted assembly (from scratch). Do the same in C. Measure the performance. I bet the C will be faster, I know it will be more readable, maintainable and extendable. Now for part 2 of the exercise - write a useful program in assembly and C.

As another exercise, have a look how much of the Linux kernal is assembler, nthen read ,the paragraph below about the linux kernel.

It is worth knowing how to do it, it might even be worth being proficient in the languages for one or two common micros.

e) "register unsigned int variable_name", "register" is, and always has been, a hint to the compiler, not an instruction, back in the early 70's (40 years ago), it made sense. In 2012, it's a waste of keystrokes as the compilers are so smart, and micros instructions sets so complex.

Back to your linux comment - the problem you have here is that we are not talking a mere 1 million units, we are talking 100's of millions, with a life time of forever. The engineering time and cost to get it as optimal as humanly possible is worth while. Although a good example of very best engineering practise, it would be commercial suicide for most embedded systems developers to be as pedantic as the linux kernal requires.


答: I vote for letting them relax and recharge their batteries. </p>

Title: What programmers should do if there are no tasks in hand for few days?

Body:
Possible Duplicate:

How to productively spend “free time” at the office

Sometimes, especially at the end of the year in software companies, there is less work for the team, which happens in my company.

What should I ask my developers team to do during these cold days?

What things can they do to keep them focused and improve the company in the next year and future?

For sure there are tons of things to be done, I am just after some recommendations of new/fun things you do.


问:
Modified Strategy Design Pattern
I've started looking into Design Patterns recently, and one thing I'm coding would suit the Strategy pattern perfectly, except for one small difference.

Essentially, some (but not all) of my algorithms, need an extra parameter or two passed to them.

So I'll either need to

pass them an extra parameter when I invoke their calculate method

or

store them as variables inside the ConcreteAlgorithm class, and be able to update them before I call the algorithm.

Is there a design pattern for this need / How could I implement this while sticking to the Strategy Pattern?

I've considered passing the client object to all the algorithms, and storing the variables in there, then using that only when the particular algorithm needs it. However, I think this is both unwieldy, and defeats the point of the strategy pattern.

Just to be clear I'm implementing in Java, and so don't have the luxury of optional parameters (which would solve this nicely).

答:
You need to clarify your strategy.

It all depends on how you use your algorithms. For your client class to use different strategy implementations interchangeably, they all need to have a common abstraction. If they don't follow the same interface, maybe what you need are different abstractions.

I've used configurable strategies before, where you parameterize the concrete classes on construction:

```<code>interface Strategy {
  int calculate();
}

class ConcreteStrategyThatNeedsAParameter implements Strategy {
  private final int param;
  public ConcreteStrategyThatNeedsAParameter(int param) {
    this.param = param;
  }
  public int calculate() {
    // uses param...
  }
}
</code>```

Now, someone still needs to create an instance of this class and pass it to your client. But your client still only needs to know about the Strategy interface.

It also works if your strategy method takes parameters, but then your client knows about those parameters and passes them to all implementations it works with.

ContentLicense: CC BY-SA 2.5

Title: Modified Strategy Design Pattern

Body:
You need to clarify your strategy.

It all depends on how you use your algorithms. For your client class to use different strategy implementations interchangeably, they all need to have a common abstraction. If they don't follow the same interface, maybe what you need are different abstractions.

I've used configurable strategies before, where you parameterize the concrete classes on construction:

interface Strategy {
  int calculate();
}

class ConcreteStrategyThatNeedsAParameter implements Strategy {
  private final int param;
  public ConcreteStrategyThatNeedsAParameter(int param) {
    this.param = param;
  }
  public int calculate() {
    // uses param...
  }
}

Now, someone still needs to create an instance of this class and pass it to your client. But your client still only needs to know about the Strategy interface.

It also works if your strategy method takes parameters, but then your client knows about those parameters and passes them to all implementations it works with.


答: Profile! Learn where the bottlenecks are in your application(s). Even if you don't do anything to fix the bottlenecks at this time (if you even can), at some time in the future it will be extremely useful to know.</p>


问:
Why is the Select before the From in an SQL query?
This is something that bothered me a lot at school.

Five years ago, when I learned SQL, I always wondered why we first specify the fields we want and then where we want them from.

According to my idea, we should write:

```<code>From Employee e
Select e.Name
</code>```

So why does the norm say the following?

```<code>Select e.Name -- Eeeeek, what does e mean?
From Employee e -- Ok, now I know what e is
</code>```

It took me weeks to understand SQL, and I know that a lot of that time was consumed by the wrong order of elements.

It is like writing in C#:

```<code>string name = employee.Name;
var employee = this.GetEmployee();
</code>```

So, I assume that it has a historical reason. Why?

答:
SQL is a structured query language targeted to English speakers.  SELECT, INSERT, UPDATE, and DELETE are imperative commands.  In English imperative commands begin the sentence or statement.  Compare:

```<code>West young man go!
</code>```

to

```<code>Go west young man!
</code>```

SQL follows the second (imperative) format.  Also the four imperative commands have three significantly different formats.  Consider:

```<code>FROM    employees a,
        accounts b
UPDATE  ...
</code>```

or

```<code>INTO    customers a
SELECT  ...
</code>```

If you know the action you are undertaking, it is easier to select the correct format.

In the case of select, you determine which attributes you want, then add the tables that have them.  As you build the selection criteria, you may add additional tables.   When you are dynamically adding criteria, it can usually be done at the end of a static portion of the query.

Body:
SQL is a structured query language targeted to English speakers.  SELECT, INSERT, UPDATE, and DELETE are imperative commands.  In English imperative commands begin the sentence or statement.  Compare:

West young man go!

to

Go west young man!

SQL follows the second (imperative) format.  Also the four imperative commands have three significantly different formats.  Consider:

FROM    employees a,
        accounts b
UPDATE  ...

or

INTO    customers a
SELECT  ...

If you know the action you are undertaking, it is easier to select the correct format.

In the case of select, you determine which attributes you want, then add the tables that have them.  As you build the selection criteria, you may add additional tables.   When you are dynamically adding criteria, it can usually be done at the end of a static portion of the query.


答:
I think the simple answer is that traditional issue tracking software helps you to manage the backlog, whereas the scrum board helps you track the current sprint and the release.

Of course it's possible to use either type of tool to do both, but you end up having to make some compromises.

Title: What is the role of traditional issue tracker when Scrum / Kanban board is used?

Body:
From a very high level view, to me it seems there are generally 2 types of Project Management tools:

Traditional issue trackers like Fogbugz, JIRA, BugZilla, Trac, Redmine etc.

Virtual card boards / agile project management tools like Pivotal Tracker, GreenHopper, AgileZen, Trello etc.

Sure, they overlap in one way or another, e.g. Pivotal Tracker tasks can be imported to JIRA, GreenHopper itself is implemented on top of JIRA issue base etc. but I think one can still see the difference in orientation between those two types of tools.

Traditional issue tracker seems to be used even in companies otherwise doing agile project management. My question is, why do they do that? I also feel that we should use an issue tracker in my company but when I'm thinking about it, I'm not actually sure why should we need it.

For example, Trello development seems to be managed by using Trello itself (see this virtual wall) even though they have access to Fogbugz, one of the best issue trackers around. So maybe we don't need traditional issue tracker when we'll be doing 100% of our work in an agile manner using one of the agile PM tools?


问:
Does it hurt to learn bits of many programming languages?
I started my programming career with BASIC, during 9th grade. I learned a bit of BASIC by writing simple programs to add, subtract and to print. Then I went to the university and took Computer Information and Systems Engineering. In the first year I was taught C, and I have good command over it.

Next I learned C++ in the second year. It just taught me some knowledge of OOP. Now I am doing PHP (along with HTML). I have not mastered C++, BASIC or PHP. I am now planning to move to mobile development. But I feel that I have not covered everything in the languages I learned.

Does it really matter?

答:
Well, C and C++ won't do you any harm in mobile programming. You can use both with Cocoa Touch or the Android NDK, and sometimes you need to for performance. Also, sometimes you want to because you want to write code that runs on several platforms. In that case, assuming you're not just using a third-party framework, you can write your app in C/C++, and compile in platform-specific wrappers for required native elements (for example, the only way to get an OpenGL context on iPhone is by using the native CAEAGLLayer class).

So it's like the more tools you have in the drawer, the more options you have for getting the job done. You'll find you have a preference for certain ones as you specialize in various kinds of tasks. If you only do UI stuff, C might not be your all-time favorite. I would worry more about the kinds of things I wanted to get done, and then learn any tool I needed to do them.


问:
Benchmarking databases
I see lots of discussions flying around about performance of db 'x' or that moving from 'x' to 'y' improved our site performance.

I'm yet to see proper benchmarking that works across different types of databases.

Is it possible to write a meaningful benchmark that could be used across multiple db types, such as Relational, Document-oriented, etc.

How would you go about designing such a benchmark?

答:
I would like to add few more reasons, why you can't benchmark all types of databases.

There are two major directions of database systems: OLAP and OLTP (see comparison).

As you said, there are also relational and document-oriented database systems. While RDBS strictly follow the ACID principle, in most document-oriented DBS you can decide that weak data is sufficient enough for your application. That makes locking and scheduling much easier.

In a short: You wouldn't argue, that a Lamborghini is the best car in the world. Think of volume of trunk, number of seats, or mileage.

As a side note: Here is a benchmark for OLTP databases systems.

Title: Benchmarking databases


问:
What's the point of adding Unicode identifier support to various language implementations?
I personally find reading code full of Unicode identifiers confusing. In my opinion, it also prevents the code from being easily maintained. Not to mention all the effort required for authors of various translators to implement such support. I also constantly notice the lack (or the presence) of Unicode identifiers support in the lists of (dis)advantages of various language implementations (like it really matters). I don't get it: why so much attention?</p>

答:
It would really make life easier (for some of us, anyway) if the compiler would not support Unicode. Right-to-left identifiers are awful. Combined Roman alphabet and right-to-left Unicode identifiers are even worse.

The bad thing about non-support is that certain GUI wizards take the text you put in for an item and automatically use that text as the item's identifier. So what exactly would they do with Unicode text on those items? No easy answer, I'm afraid.

Unicode right-to-left comments can be funny, too. For example, in VS 2010, XML  comments display (correctly) as RTL in the code... but when you use Intellisense to pull up the identifier elsewhere in code, the tooltip displays (incorrectly) LTR. Better, perhaps, if there were no support in the first place? Again, not an easy call.


答:
When you think unicode, you think Chinese or Russian characters, which makes you think of some source code written in Russian you've seen on the internet, and which was unusable (unless you know Russian).

But if unicode can be used in a wrong way, it doesn't mean it's bad by itself in source code.

When writing code for a specific field, with unicode, you can shorten your code and make it more readable. Instead of:

```<code>const numeric Pi = 3.1415926535897932384626433832795;
numeric firstAlpha = deltaY / deltaX + Pi;
numeric secondAlpha = this.Compute(firstAlpha);
Assert.Equals(math.Infinity, secondAlpha);
</code>```

you can write:

```<code>const numeric π = 3.1415926535897932384626433832795;
numeric α₁ = Δy / Δx + π;
numeric α₂ = this.Compute(α₁);
Assert.Equals(math.∞, α₂);
</code>```

which may not be easy to read for an average developer, but is still easy to read for a person who uses mathematical symbols daily.

Or, when doing an application related to SLR photography, instead of:

```<code>int aperture = currentLens.GetMaximumAperture();
Assert.AreEqual(this.Aperture1_8, aperture);
</code>```

you can replace the aperture by it's symbol ƒ, with a writing closer to ƒ/1.8:

```<code>int ƒ = currentLens.GetMaximumƒ();
Assert.AreEqual(this.ƒ1¸8, ƒ);
</code>```

This may be inconvenient: when typing general C# code, I would prefer writing:

```<code>var productPrices = this.Products.Select(c => c.Price);
double average = productPrices.Average()
double sum = this.ProductPrices.Sum();
</code>```

rather than:

```<code>var productPrices = this.Products.Select(c => c.Price);
double average = productPrices.x̅()
double sum = productPrices.Σ();
</code>```

because in the first case, IntelliSense helps me to write the whole code nearly without typing and especially without using my mouse, while in the second case, I have no idea where to find those symbols and would be forced to rely on the mouse to go and search them in the auto-completion list.

This being said, it's still useful in some cases. currentLens.GetMaximumƒ(); of my previous example can rely on IntelliSense and is as easy to type as GetMaximumAperture, being shorter and more readable. Also, for specific domains with lots of symbols, keyboard shortcuts may help typing the symbols quicker than their literal equivalents in source code.

The same, by the way, applies to comments. No one wants to read code full of comments in Chinese (unless you know well Chinese yourself). But in some programming languages, unicode symbols can still be useful. One example is footnotes¹.

¹ I certainly wouldn't enjoy footnotes in C# code where there is a strict set of style rules of how to write comments. In PHP on the other hand, if there are lots of things to explain, but those things are not very important, why not putting them at the bottom of the file, and create a footnote in the PHPDoc of the method?

Title: What's the point of adding Unicode identifier support to various language implementations?

Body: I personally find reading code full of Unicode identifiers confusing. In my opinion, it also prevents the code from being easily maintained. Not to mention all the effort required for authors of various translators to implement such support. I also constantly notice the lack (or the presence) of Unicode identifiers support in the lists of (dis)advantages of various language implementations (like it really matters). I don't get it: why so much attention?</p>

Body:
When you think unicode, you think Chinese or Russian characters, which makes you think of some source code written in Russian you've seen on the internet, and which was unusable (unless you know Russian).

But if unicode can be used in a wrong way, it doesn't mean it's bad by itself in source code.

When writing code for a specific field, with unicode, you can shorten your code and make it more readable. Instead of:

const numeric Pi = 3.1415926535897932384626433832795;
numeric firstAlpha = deltaY / deltaX + Pi;
numeric secondAlpha = this.Compute(firstAlpha);
Assert.Equals(math.Infinity, secondAlpha);

you can write:

const numeric π = 3.1415926535897932384626433832795;
numeric α₁ = Δy / Δx + π;
numeric α₂ = this.Compute(α₁);
Assert.Equals(math.∞, α₂);

which may not be easy to read for an average developer, but is still easy to read for a person who uses mathematical symbols daily.

Or, when doing an application related to SLR photography, instead of:

int aperture = currentLens.GetMaximumAperture();
Assert.AreEqual(this.Aperture1_8, aperture);

you can replace the aperture by it's symbol ƒ, with a writing closer to ƒ/1.8:

int ƒ = currentLens.GetMaximumƒ();
Assert.AreEqual(this.ƒ1¸8, ƒ);

This may be inconvenient: when typing general C# code, I would prefer writing:

var productPrices = this.Products.Select(c => c.Price);
double average = productPrices.Average()
double sum = this.ProductPrices.Sum();

rather than:

var productPrices = this.Products.Select(c => c.Price);
double average = productPrices.x̅()
double sum = productPrices.Σ();

because in the first case, IntelliSense helps me to write the whole code nearly without typing and especially without using my mouse, while in the second case, I have no idea where to find those symbols and would be forced to rely on the mouse to go and search them in the auto-completion list.

This being said, it's still useful in some cases. currentLens.GetMaximumƒ(); of my previous example can rely on IntelliSense and is as easy to type as GetMaximumAperture, being shorter and more readable. Also, for specific domains with lots of symbols, keyboard shortcuts may help typing the symbols quicker than their literal equivalents in source code.

The same, by the way, applies to comments. No one wants to read code full of comments in Chinese (unless you know well Chinese yourself). But in some programming languages, unicode symbols can still be useful. One example is footnotes¹.

¹ I certainly wouldn't enjoy footnotes in C# code where there is a strict set of style rules of how to write comments. In PHP on the other hand, if there are lots of things to explain, but those things are not very important, why not putting them at the bottom of the file, and create a footnote in the PHPDoc of the method?


答:
Definitely not. Of course you also want to make sure you are learning different styles of languages. Learning both C# and Java will not expand your mind in any major way, but learning Java and Haskell will.

Check out the Book Seven Languages in Seven Weeks, which will walk you through an intro to Ruby, IO, Scala, Erlang, Prolog, Clojure and Haskell. IF you enjoy languages you will love that book.

Title: Does it hurt to learn bits of many programming languages?

Body:
I started my programming career with BASIC, during 9th grade. I learned a bit of BASIC by writing simple programs to add, subtract and to print. Then I went to the university and took Computer Information and Systems Engineering. In the first year I was taught C, and I have good command over it.

Next I learned C++ in the second year. It just taught me some knowledge of OOP. Now I am doing PHP (along with HTML). I have not mastered C++, BASIC or PHP. I am now planning to move to mobile development. But I feel that I have not covered everything in the languages I learned.

Does it really matter?


问:
Writing a lexer in C++
What are good resources on how to write a lexer in C++ (books, tutorials, documents), what are some good techniques and practices?

I have looked on the internet and everyone says to use a lexer generator like lex. I don't want to do that, I want to write a lexer by hand.

答:
Lexers are finite state machines. Therefore, they can be constructed by any general-purpose FSM library. For the purposes of my own education, however, I wrote my own, using expression templates. Here's my lexer:

```<code>static const std::unordered_map<Unicode::String, Wide::Lexer::TokenType> reserved_words(
    []() -> std::unordered_map<Unicode::String, Wide::Lexer::TokenType>
    {
        // Maps reserved words to TokenType enumerated values
        std::unordered_map<Unicode::String, Wide::Lexer::TokenType> result;

        // RESERVED WORD
        result[L"dynamic_cast"] = Wide::Lexer::TokenType::DynamicCast;
        result[L"for"] = Wide::Lexer::TokenType::For;
        result[L"while"] = Wide::Lexer::TokenType::While;
        result[L"do"] = Wide::Lexer::TokenType::Do;
        result[L"continue"] = Wide::Lexer::TokenType::Continue;
        result[L"auto"] = Wide::Lexer::TokenType::Auto;
        result[L"break"] = Wide::Lexer::TokenType::Break;
        result[L"type"] = Wide::Lexer::TokenType::Type;
        result[L"switch"] = Wide::Lexer::TokenType::Switch;
        result[L"case"] = Wide::Lexer::TokenType::Case;
        result[L"default"] = Wide::Lexer::TokenType::Default;
        result[L"try"] = Wide::Lexer::TokenType::Try;
        result[L"catch"] = Wide::Lexer::TokenType::Catch;
        result[L"return"] = Wide::Lexer::TokenType::Return;
        result[L"static"] = Wide::Lexer::TokenType::Static;
        result[L"if"] = Wide::Lexer::TokenType::If;
        result[L"else"] = Wide::Lexer::TokenType::Else;
        result[L"decltype"] = Wide::Lexer::TokenType::Decltype;
        result[L"partial"] = Wide::Lexer::TokenType::Partial;
        result[L"using"] = Wide::Lexer::TokenType::Using;
        result[L"true"] = Wide::Lexer::TokenType::True;
        result[L"false"] = Wide::Lexer::TokenType::False;
        result[L"null"] = Wide::Lexer::TokenType::Null;
        result[L"int"] = Wide::Lexer::TokenType::Int;
        result[L"long"] = Wide::Lexer::TokenType::Long;
        result[L"short"] = Wide::Lexer::TokenType::Short;
        result[L"module"] = Wide::Lexer::TokenType::Module;
        result[L"dynamic"] = Wide::Lexer::TokenType::Dynamic;
        result[L"reinterpret_cast"] = Wide::Lexer::TokenType::ReinterpretCast;
        result[L"static_cast"] = Wide::Lexer::TokenType::StaticCast;
        result[L"enum"] = Wide::Lexer::TokenType::Enum;
        result[L"operator"] = Wide::Lexer::TokenType::Operator;
        result[L"throw"] = Wide::Lexer::TokenType::Throw;
        result[L"public"] = Wide::Lexer::TokenType::Public;
        result[L"private"] = Wide::Lexer::TokenType::Private;
        result[L"protected"] = Wide::Lexer::TokenType::Protected;
        result[L"friend"] = Wide::Lexer::TokenType::Friend;
        result[L"this"] = Wide::Lexer::TokenType::This;

        return result;
    }()
);

std::vector<Wide::Lexer::Token*> Lexer::Context::operator()(Unicode::String* filename, Memory::Arena& arena) {

    Wide::IO::TextInputFileOpenArguments args;
    args.encoding = Wide::IO::Encoding::UTF16;
    args.mode = Wide::IO::OpenMode::OpenExisting;
    args.path = *filename;

    auto str = arena.Allocate<Unicode::String>(args().AsString());
    const wchar_t* begin = str->c_str();
    const wchar_t* end = str->c_str() + str->size();

    int line = 1;
    int column = 1;

    std::vector<Token*> tokens;

    // Some variables we'll need for semantic actions
    Wide::Lexer::TokenType type;

    auto multi_line_comment
        =  MakeEquality(L'/')
        >> MakeEquality(L'*')
        >> *( !(MakeEquality(L'*') >> MakeEquality(L'/')) >> eps)
        >> eps >> eps;

    auto single_line_comment
        =  MakeEquality(L'/')
        >> MakeEquality(L'/')
        >> *( !MakeEquality(L'\n') >> eps);

    auto punctuation
        =  MakeEquality(L',')[[&]{ type = Wide::Lexer::TokenType::Comma; }]
        || MakeEquality(L';')[[&]{ type = Wide::Lexer::TokenType::Semicolon; }]
        || MakeEquality(L'~')[[&]{ type = Wide::Lexer::TokenType::BinaryNOT; }]
        || MakeEquality(L'(')[[&]{ type = Wide::Lexer::TokenType::OpenBracket; }]
        || MakeEquality(L')')[[&]{ type = Wide::Lexer::TokenType::CloseBracket; }]
        || MakeEquality(L'[')[[&]{ type = Wide::Lexer::TokenType::OpenSquareBracket; }]
        || MakeEquality(L']')[[&]{ type = Wide::Lexer::TokenType::CloseSquareBracket; }]
        || MakeEquality(L'{')[[&]{ type = Wide::Lexer::TokenType::OpenCurlyBracket; }]
        || MakeEquality(L'}')[[&]{ type = Wide::Lexer::TokenType::CloseCurlyBracket; }]

        || MakeEquality(L'>') >> (
               MakeEquality(L'>') >> (
                   MakeEquality(L'=')[[&]{ type = Wide::Lexer::TokenType::RightShiftEquals; }]
                || opt[[&]{ type = Wide::Lexer::TokenType::RightShift; }])
            || MakeEquality(L'=')[[&]{ type = Wide::Lexer::TokenType::GreaterThanOrEqualTo; }]
            || opt[[&]{ type = Wide::Lexer::TokenType::GreaterThan; }])
        || MakeEquality(L'<') >> (
               MakeEquality(L'<') >> (
                      MakeEquality(L'=')[[&]{ type = Wide::Lexer::TokenType::LeftShiftEquals; }]
                   || opt[[&]{ type = Wide::Lexer::TokenType::LeftShift; }] )
            || MakeEquality(L'=')[[&]{ type = Wide::Lexer::TokenType::LessThanOrEqualTo; }]
            || opt[[&]{ type = Wide::Lexer::TokenType::LessThan; }])

        || MakeEquality(L'-') >> (
               MakeEquality(L'-')[[&]{ type = Wide::Lexer::TokenType::Decrement; }]
            || MakeEquality(L'=')[[&]{ type = Wide::Lexer::TokenType::MinusEquals; }]
            || MakeEquality(L'>')[[&]{ type = Wide::Lexer::TokenType::PointerAccess; }]
            || opt[[&]{ type = Wide::Lexer::TokenType::Minus; }])

        || MakeEquality(L'.')
            >> (MakeEquality(L'.') >> MakeEquality(L'.')[[&]{ type = Wide::Lexer::TokenType::Ellipsis; }]
            || opt[[&]{ type = Wide::Lexer::TokenType::Dot; }])

        || MakeEquality(L'+') >> (
               MakeEquality(L'+')[[&]{ type = Wide::Lexer::TokenType::Increment; }]
            || MakeEquality(L'=')[[&]{ type = Wide::Lexer::TokenType::PlusEquals; }]
            || opt[[&]{ type = Wide::Lexer::TokenType::Plus; }])
        || MakeEquality(L'&') >> (
               MakeEquality(L'&')[[&]{ type = Wide::Lexer::TokenType::LogicalAnd; }]
            || MakeEquality(L'=')[[&]{ type = Wide::Lexer::TokenType::BinaryANDEquals; }]
            || opt[[&]{ type = Wide::Lexer::TokenType::BinaryAND; }])
        || MakeEquality(L'|') >> (
               MakeEquality(L'|')[[&]{ type = Wide::Lexer::TokenType::LogicalOr; }]
            || MakeEquality(L'=')[[&]{ type = Wide::Lexer::TokenType::BinaryOREquals; }]
            || opt[[&]{ type = Wide::Lexer::TokenType::BinaryOR; }])

        || MakeEquality(L'*') >> (MakeEquality(L'=')[[&]{ type = Wide::Lexer::TokenType::MulEquals; }]
            || opt[[&]{ type = Wide::Lexer::TokenType::Multiply; }])
        || MakeEquality(L'%') >> (MakeEquality(L'=')[[&]{ type = Wide::Lexer::TokenType::ModulusEquals; }]
            || opt[[&]{ type = Wide::Lexer::TokenType::Modulus; }])
        || MakeEquality(L'=') >> (MakeEquality(L'=')[[&]{ type = Wide::Lexer::TokenType::EqualTo; }]
            || opt[[&]{ type = Wide::Lexer::TokenType::Assignment; }])
        || MakeEquality(L'!') >> (MakeEquality(L'=')[[&]{ type = Wide::Lexer::TokenType::NotEquals; }]
            || opt[[&]{ type = Wide::Lexer::TokenType::LogicalNOT; }])
        || MakeEquality(L'/') >> (MakeEquality(L'=')[[&]{ type = Wide::Lexer::TokenType::DivEquals; }]
            || opt[[&]{ type = Wide::Lexer::TokenType::Divide; }])
        || MakeEquality(L'^') >> (MakeEquality(L'=')[[&]{ type = Wide::Lexer::TokenType::BinaryXOREquals; }]
            || opt[[&]{ type = Wide::Lexer::TokenType::BinaryXOR; }])
        || MakeEquality(L':') >> (MakeEquality(L'=')[[&]{ type = Wide::Lexer::TokenType::VarAssign; }]
            || opt[[&]{ type = Wide::Lexer::TokenType::Colon; }]);

    auto string
        =  L'"' >> *( L'\\' >> MakeEquality(L'"') >> eps || !MakeEquality(L'"') >> eps) >> eps;

    auto character
        =  L'\'' >> *( L'\\' >> MakeEquality(L'\'') >> eps || !MakeEquality(L'\'') >> eps);

    auto digit
        =  MakeRange(L'0', L'9');

    auto letter
        =  MakeRange(L'a', L'z') || MakeRange(L'A', L'Z');

    auto number
        =  +digit >> ((L'.' >> +digit) || opt);

    auto new_line
        = MakeEquality(L'\n')[ [&] { line++; column = 0; } ];

    auto whitespace
        =  MakeEquality(L' ')
        || L'\t'
        || new_line
        || L'\n'
        || L'\r'
        || multi_line_comment
        || single_line_comment;

    auto identifier
        =  (letter || L'_') >> *(letter || digit || (L'_'));
        //=  *( !(punctuation || string || character || whitespace) >> eps );

    bool skip = false;

    auto lexer
        =  whitespace[ [&]{ skip = true; } ] // Do not produce a token for whitespace or comments. Just continue on.
        || punctuation[ [&]{ skip = false; } ] // Type set by individual punctuation
        || string[ [&]{ skip = false; type = Wide::Lexer::TokenType::String; } ]
        || character[ [&]{ skip = false; type = Wide::Lexer::TokenType::Character; } ]
        || number[ [&]{ skip = false; type = Wide::Lexer::TokenType::Number; } ]
        || identifier[ [&]{ skip = false; type = Wide::Lexer::TokenType::Identifier; } ];

    auto current = begin;
    while(current != end) {
        if (!lexer(current, end)) {
            throw std::runtime_error("Failed to lex input.");
        }
        column += (current - begin);
        if (skip) {
            begin = current;
            continue;
        }
        Token t(begin, current);
        t.columnbegin = column - (current - begin);
        t.columnend = column;
        t.file = filename;
        t.line = line;
        if (type == Wide::Lexer::TokenType::Identifier) { // check for reserved word
            if (reserved_words.find(t.Codepoints()) != reserved_words.end())
                t.type = reserved_words.find(t.Codepoints())->second;
            else
                t.type = Wide::Lexer::TokenType::Identifier;
        } else {
            t.type = type;
        }
        begin = current;
        tokens.push_back(arena.Allocate<Token>(t));
    }
    return tokens;
}
</code>```

It's backed by an iterator-based, back-tracking, finite state machine library which is ~400 lines in length. However, it's easy to see that all I had to do was construct simple boolean operations, like and, or, and not, and a couple of regex-style operators like * for zero-or-more, eps to mean "match anything" and opt to mean "match anything but don't consume it". The library is fully generic and based on iterators. The MakeEquality stuff is a simple test for equality between *it and the value passed in, and MakeRange is a simple <= >= test.

Eventually, I'm planning to move from backtracking to predictive.

Title: Writing a lexer in C++

Body:
Lexers are finite state machines. Therefore, they can be constructed by any general-purpose FSM library. For the purposes of my own education, however, I wrote my own, using expression templates. Here's my lexer:

static const std::unordered_map<Unicode::String, Wide::Lexer::TokenType> reserved_words(
    []() -> std::unordered_map<Unicode::String, Wide::Lexer::TokenType>
    {
        // Maps reserved words to TokenType enumerated values
        std::unordered_map<Unicode::String, Wide::Lexer::TokenType> result;

        // RESERVED WORD
        result[L"dynamic_cast"] = Wide::Lexer::TokenType::DynamicCast;
        result[L"for"] = Wide::Lexer::TokenType::For;
        result[L"while"] = Wide::Lexer::TokenType::While;
        result[L"do"] = Wide::Lexer::TokenType::Do;
        result[L"continue"] = Wide::Lexer::TokenType::Continue;
        result[L"auto"] = Wide::Lexer::TokenType::Auto;
        result[L"break"] = Wide::Lexer::TokenType::Break;
        result[L"type"] = Wide::Lexer::TokenType::Type;
        result[L"switch"] = Wide::Lexer::TokenType::Switch;
        result[L"case"] = Wide::Lexer::TokenType::Case;
        result[L"default"] = Wide::Lexer::TokenType::Default;
        result[L"try"] = Wide::Lexer::TokenType::Try;
        result[L"catch"] = Wide::Lexer::TokenType::Catch;
        result[L"return"] = Wide::Lexer::TokenType::Return;
        result[L"static"] = Wide::Lexer::TokenType::Static;
        result[L"if"] = Wide::Lexer::TokenType::If;
        result[L"else"] = Wide::Lexer::TokenType::Else;
        result[L"decltype"] = Wide::Lexer::TokenType::Decltype;
        result[L"partial"] = Wide::Lexer::TokenType::Partial;
        result[L"using"] = Wide::Lexer::TokenType::Using;
        result[L"true"] = Wide::Lexer::TokenType::True;
        result[L"false"] = Wide::Lexer::TokenType::False;
        result[L"null"] = Wide::Lexer::TokenType::Null;
        result[L"int"] = Wide::Lexer::TokenType::Int;
        result[L"long"] = Wide::Lexer::TokenType::Long;
        result[L"short"] = Wide::Lexer::TokenType::Short;
        result[L"module"] = Wide::Lexer::TokenType::Module;
        result[L"dynamic"] = Wide::Lexer::TokenType::Dynamic;
        result[L"reinterpret_cast"] = Wide::Lexer::TokenType::ReinterpretCast;
        result[L"static_cast"] = Wide::Lexer::TokenType::StaticCast;
        result[L"enum"] = Wide::Lexer::TokenType::Enum;
        result[L"operator"] = Wide::Lexer::TokenType::Operator;
        result[L"throw"] = Wide::Lexer::TokenType::Throw;
        result[L"public"] = Wide::Lexer::TokenType::Public;
        result[L"private"] = Wide::Lexer::TokenType::Private;
        result[L"protected"] = Wide::Lexer::TokenType::Protected;
        result[L"friend"] = Wide::Lexer::TokenType::Friend;
        result[L"this"] = Wide::Lexer::TokenType::This;

        return result;
    }()
);

std::vector<Wide::Lexer::Token*> Lexer::Context::operator()(Unicode::String* filename, Memory::Arena& arena) {

    Wide::IO::TextInputFileOpenArguments args;
    args.encoding = Wide::IO::Encoding::UTF16;
    args.mode = Wide::IO::OpenMode::OpenExisting;
    args.path = *filename;

    auto str = arena.Allocate<Unicode::String>(args().AsString());
    const wchar_t* begin = str->c_str();
    const wchar_t* end = str->c_str() + str->size();

    int line = 1;
    int column = 1;

    std::vector<Token*> tokens;

    // Some variables we'll need for semantic actions
    Wide::Lexer::TokenType type;

    auto multi_line_comment
        =  MakeEquality(L'/')
        >> MakeEquality(L'*')
        >> *( !(MakeEquality(L'*') >> MakeEquality(L'/')) >> eps)
        >> eps >> eps;

    auto single_line_comment
        =  MakeEquality(L'/')
        >> MakeEquality(L'/')
        >> *( !MakeEquality(L'\n') >> eps);

    auto punctuation
        =  MakeEquality(L',')[[&]{ type = Wide::Lexer::TokenType::Comma; }]
        || MakeEquality(L';')[[&]{ type = Wide::Lexer::TokenType::Semicolon; }]
        || MakeEquality(L'~')[[&]{ type = Wide::Lexer::TokenType::BinaryNOT; }]
        || MakeEquality(L'(')[[&]{ type = Wide::Lexer::TokenType::OpenBracket; }]
        || MakeEquality(L')')[[&]{ type = Wide::Lexer::TokenType::CloseBracket; }]
        || MakeEquality(L'[')[[&]{ type = Wide::Lexer::TokenType::OpenSquareBracket; }]
        || MakeEquality(L']')[[&]{ type = Wide::Lexer::TokenType::CloseSquareBracket; }]
        || MakeEquality(L'{')[[&]{ type = Wide::Lexer::TokenType::OpenCurlyBracket; }]
        || MakeEquality(L'}')[[&]{ type = Wide::Lexer::TokenType::CloseCurlyBracket; }]

        || MakeEquality(L'>') >> (
               MakeEquality(L'>') >> (
                   MakeEquality(L'=')[[&]{ type = Wide::Lexer::TokenType::RightShiftEquals; }]
                || opt[[&]{ type = Wide::Lexer::TokenType::RightShift; }])
            || MakeEquality(L'=')[[&]{ type = Wide::Lexer::TokenType::GreaterThanOrEqualTo; }]
            || opt[[&]{ type = Wide::Lexer::TokenType::GreaterThan; }])
        || MakeEquality(L'<') >> (
               MakeEquality(L'<') >> (
                      MakeEquality(L'=')[[&]{ type = Wide::Lexer::TokenType::LeftShiftEquals; }]
                   || opt[[&]{ type = Wide::Lexer::TokenType::LeftShift; }] )
            || MakeEquality(L'=')[[&]{ type = Wide::Lexer::TokenType::LessThanOrEqualTo; }]
            || opt[[&]{ type = Wide::Lexer::TokenType::LessThan; }])

        || MakeEquality(L'-') >> (
               MakeEquality(L'-')[[&]{ type = Wide::Lexer::TokenType::Decrement; }]
            || MakeEquality(L'=')[[&]{ type = Wide::Lexer::TokenType::MinusEquals; }]
            || MakeEquality(L'>')[[&]{ type = Wide::Lexer::TokenType::PointerAccess; }]
            || opt[[&]{ type = Wide::Lexer::TokenType::Minus; }])

        || MakeEquality(L'.')
            >> (MakeEquality(L'.') >> MakeEquality(L'.')[[&]{ type = Wide::Lexer::TokenType::Ellipsis; }]
            || opt[[&]{ type = Wide::Lexer::TokenType::Dot; }])

        || MakeEquality(L'+') >> (
               MakeEquality(L'+')[[&]{ type = Wide::Lexer::TokenType::Increment; }]
            || MakeEquality(L'=')[[&]{ type = Wide::Lexer::TokenType::PlusEquals; }]
            || opt[[&]{ type = Wide::Lexer::TokenType::Plus; }])
        || MakeEquality(L'&') >> (
               MakeEquality(L'&')[[&]{ type = Wide::Lexer::TokenType::LogicalAnd; }]
            || MakeEquality(L'=')[[&]{ type = Wide::Lexer::TokenType::BinaryANDEquals; }]
            || opt[[&]{ type = Wide::Lexer::TokenType::BinaryAND; }])
        || MakeEquality(L'|') >> (
               MakeEquality(L'|')[[&]{ type = Wide::Lexer::TokenType::LogicalOr; }]
            || MakeEquality(L'=')[[&]{ type = Wide::Lexer::TokenType::BinaryOREquals; }]
            || opt[[&]{ type = Wide::Lexer::TokenType::BinaryOR; }])

        || MakeEquality(L'*') >> (MakeEquality(L'=')[[&]{ type = Wide::Lexer::TokenType::MulEquals; }]
            || opt[[&]{ type = Wide::Lexer::TokenType::Multiply; }])
        || MakeEquality(L'%') >> (MakeEquality(L'=')[[&]{ type = Wide::Lexer::TokenType::ModulusEquals; }]
            || opt[[&]{ type = Wide::Lexer::TokenType::Modulus; }])
        || MakeEquality(L'=') >> (MakeEquality(L'=')[[&]{ type = Wide::Lexer::TokenType::EqualTo; }]
            || opt[[&]{ type = Wide::Lexer::TokenType::Assignment; }])
        || MakeEquality(L'!') >> (MakeEquality(L'=')[[&]{ type = Wide::Lexer::TokenType::NotEquals; }]
            || opt[[&]{ type = Wide::Lexer::TokenType::LogicalNOT; }])
        || MakeEquality(L'/') >> (MakeEquality(L'=')[[&]{ type = Wide::Lexer::TokenType::DivEquals; }]
            || opt[[&]{ type = Wide::Lexer::TokenType::Divide; }])
        || MakeEquality(L'^') >> (MakeEquality(L'=')[[&]{ type = Wide::Lexer::TokenType::BinaryXOREquals; }]
            || opt[[&]{ type = Wide::Lexer::TokenType::BinaryXOR; }])
        || MakeEquality(L':') >> (MakeEquality(L'=')[[&]{ type = Wide::Lexer::TokenType::VarAssign; }]
            || opt[[&]{ type = Wide::Lexer::TokenType::Colon; }]);

    auto string
        =  L'"' >> *( L'\\' >> MakeEquality(L'"') >> eps || !MakeEquality(L'"') >> eps) >> eps;

    auto character
        =  L'\'' >> *( L'\\' >> MakeEquality(L'\'') >> eps || !MakeEquality(L'\'') >> eps);

    auto digit
        =  MakeRange(L'0', L'9');

    auto letter
        =  MakeRange(L'a', L'z') || MakeRange(L'A', L'Z');

    auto number
        =  +digit >> ((L'.' >> +digit) || opt);

    auto new_line
        = MakeEquality(L'\n')[ [&] { line++; column = 0; } ];

    auto whitespace
        =  MakeEquality(L' ')
        || L'\t'
        || new_line
        || L'\n'
        || L'\r'
        || multi_line_comment
        || single_line_comment;

    auto identifier
        =  (letter || L'_') >> *(letter || digit || (L'_'));
        //=  *( !(punctuation || string || character || whitespace) >> eps );

    bool skip = false;

    auto lexer
        =  whitespace[ [&]{ skip = true; } ] // Do not produce a token for whitespace or comments. Just continue on.
        || punctuation[ [&]{ skip = false; } ] // Type set by individual punctuation
        || string[ [&]{ skip = false; type = Wide::Lexer::TokenType::String; } ]
        || character[ [&]{ skip = false; type = Wide::Lexer::TokenType::Character; } ]
        || number[ [&]{ skip = false; type = Wide::Lexer::TokenType::Number; } ]
        || identifier[ [&]{ skip = false; type = Wide::Lexer::TokenType::Identifier; } ];

    auto current = begin;
    while(current != end) {
        if (!lexer(current, end)) {
            throw std::runtime_error("Failed to lex input.");
        }
        column += (current - begin);
        if (skip) {
            begin = current;
            continue;
        }
        Token t(begin, current);
        t.columnbegin = column - (current - begin);
        t.columnend = column;
        t.file = filename;
        t.line = line;
        if (type == Wide::Lexer::TokenType::Identifier) { // check for reserved word
            if (reserved_words.find(t.Codepoints()) != reserved_words.end())
                t.type = reserved_words.find(t.Codepoints())->second;
            else
                t.type = Wide::Lexer::TokenType::Identifier;
        } else {
            t.type = type;
        }
        begin = current;
        tokens.push_back(arena.Allocate<Token>(t));
    }
    return tokens;
}

It's backed by an iterator-based, back-tracking, finite state machine library which is ~400 lines in length. However, it's easy to see that all I had to do was construct simple boolean operations, like and, or, and not, and a couple of regex-style operators like * for zero-or-more, eps to mean "match anything" and opt to mean "match anything but don't consume it". The library is fully generic and based on iterators. The MakeEquality stuff is a simple test for equality between *it and the value passed in, and MakeRange is a simple <= >= test.

Eventually, I'm planning to move from backtracking to predictive.


问:
What can we learn from inactive assembly languages?
There are still groups of programmers who support old microprocessors, e.g., Z80, 6510, 68000, etc. What can we learn from old assembly languages at a time when functional programming is becoming fashionable?

EDIT

I imagine that there would be more to learn for embedded systems. However, assembler styles apply to a more limited extent for web programming where caching is used and the size of routines does not really matter. The guideline for best practice for embedded and web styles differs massively (server and client styles being different). For example, the optimising a sprite multiplexer to run in say, under 30 bytes, is different to types of optimising we would make for code we intend to run on a web server. The types of optimisation are very different. The sprite multiplexer is written with memory usage as the main priority but regarding our web server routine, we want maximum performance which has little to do with efficient use of memory unless we are talking about shared resources.

答:
The primary thing for these older platforms is essentially:

```<code>How to code efficiently when memory is tight and cpu cycles are few.
</code>```

In an age where the typical response is "just upgrade the hardware" it is very interesting to see how to squeeze maximum performance out of what you have.

The Z80 and 6502/6510's are good starting points as they were very wide spread (CP/M-80 and C64 respectively) with a lot of code written.

See for instance the chess programs listed here - http://chessprogramming.wikispaces.com/Z80 - Sargon played a respectable game, and the ZX81 ran in 1 KB of RAM.

Body:
The primary thing for these older platforms is essentially:

How to code efficiently when memory is tight and cpu cycles are few.

In an age where the typical response is "just upgrade the hardware" it is very interesting to see how to squeeze maximum performance out of what you have.

The Z80 and 6502/6510's are good starting points as they were very wide spread (CP/M-80 and C64 respectively) with a lot of code written.

See for instance the chess programs listed here - http://chessprogramming.wikispaces.com/Z80 - Sargon played a respectable game, and the ZX81 ran in 1 KB of RAM.


答:
First of all, there are different things going on here:

splitting the bare characters list into tokens

recognizing those tokens (identifying keywords, literals, brackets, ...)

verifying a general grammar structure

Generally, we expect a lexer to do all 3 steps in one go, however the latter is inherently more difficult and there are some issues with automation (more on this later).

The most amazing lexer I know of is Boost.Spirit.Qi. It uses expression templates to generate your lexer expressions, and once accustomed to its syntax the code feels really neat. It compiles very slowly though (heavy templates), so it's best to isolate the various portions in dedicated files to avoid recompiling them when they haven't been touched.

There are some pitfalls in performance, and the author of the Epoch compiler explains how he got a 1000x speed-up by intensive profiling and investigation in how Qi works in an article.

Finally, there are also generated code by external tools (Yacc, Bison, ...).

But I promised a write-up on what was wrong with automating the grammar verification.

If you check out Clang, for example, you will realize that instead of using a generated parser and something like Boost.Spirit, instead they set out to validate the grammar manually using a generic Descent Parsing technic. Surely this seems backward?

In fact, there is a very simple reason: error recovery.

The typical example, in C++:

```<code>struct Immediate { } instanceOfImmediate;

struct Foo {}

void bar() {
}
</code>```

Notice the error ? A missing semi-colon right after the declaration of Foo.

It is a common error, and Clang recovers neatly by realizing that it is simply missing and void is not an instance of Foo but part of the next declaration. This avoid hard to diagnose cryptic error messages.

Most automated tools have no (at least obvious) ways on specifying those likely mistakes and how to recover from them. Often recovering requires a little syntactic analysis so it's far from evident.

So, there is trade-off involved in using an automated tool: you get your parser quickly, but it is less user-friendly.

Title: Writing a lexer in C++

Body:
What are good resources on how to write a lexer in C++ (books, tutorials, documents), what are some good techniques and practices?

I have looked on the internet and everyone says to use a lexer generator like lex. I don't want to do that, I want to write a lexer by hand.

Body:
First of all, there are different things going on here:

splitting the bare characters list into tokens

recognizing those tokens (identifying keywords, literals, brackets, ...)

verifying a general grammar structure

Generally, we expect a lexer to do all 3 steps in one go, however the latter is inherently more difficult and there are some issues with automation (more on this later).

The most amazing lexer I know of is Boost.Spirit.Qi. It uses expression templates to generate your lexer expressions, and once accustomed to its syntax the code feels really neat. It compiles very slowly though (heavy templates), so it's best to isolate the various portions in dedicated files to avoid recompiling them when they haven't been touched.

There are some pitfalls in performance, and the author of the Epoch compiler explains how he got a 1000x speed-up by intensive profiling and investigation in how Qi works in an article.

Finally, there are also generated code by external tools (Yacc, Bison, ...).

But I promised a write-up on what was wrong with automating the grammar verification.

If you check out Clang, for example, you will realize that instead of using a generated parser and something like Boost.Spirit, instead they set out to validate the grammar manually using a generic Descent Parsing technic. Surely this seems backward?

In fact, there is a very simple reason: error recovery.

The typical example, in C++:

struct Immediate { } instanceOfImmediate;

struct Foo {}

void bar() {
}

Notice the error ? A missing semi-colon right after the declaration of Foo.

It is a common error, and Clang recovers neatly by realizing that it is simply missing and void is not an instance of Foo but part of the next declaration. This avoid hard to diagnose cryptic error messages.

Most automated tools have no (at least obvious) ways on specifying those likely mistakes and how to recover from them. Often recovering requires a little syntactic analysis so it's far from evident.

So, there is trade-off involved in using an automated tool: you get your parser quickly, but it is less user-friendly.


答:
As far as I am concerned, this is purely for marketing reasons. And additionally may make our lives harder.

The marketing arguments

You know this crazy lists of features that most languages boast off ? It's pretty much useless in general, because it's so far from the language that it does not provide much information on specific, but it does allow one to quickly dress tables with ticks and crosses and rightfully conclude that since X has more ticks than Y it must be better.

Well, Unicode support for the identifiers is one of those lines. It does not matter that compared to Lambda support, Generic programming support, etc... it might not be much, people drawing the tables don't care about the quality of each line, only about the number of them.

And thus they can boast: "Ah, with Y you do not have Unicode support for your identifiers! In X we do, so for students it's much easier!"

The fallacy of accessibility

Unfortunately, the argument of accessibility is fallacious.

Oh, I do understand that being able to write "résultatDuJetDeDé" instead of "diceThrowResult" (yes I am French) might seem like a win in the short term... however there are drawbacks!

Programming is about communicating

Your program is not only meant for the compiler (which could care less about the identifiers you use), it is also meant for your fellows. They need to be able to read it, and understand it.

reading it implies being able to visualize the characters you used, Unicode is not so well supported by all fonts

understanding it do mean relying on identifiers -- unless you supplement them with lenghty comments, but that is violating the DRY rule.

Of course, your classmate may speak the same language you do (not obvious, I had programming classes with Germans, Spanishs, Libanes and Chineses), and so may your teacher... but suppose that somehow you are working on it at home and suddenly need help: Internet is great, you may speak to thousands of thousands of people that know the solution, they will only answer if they understand your question though. And you need to understand their answer as well.

Programming requires understanding

Accessibility and initiation require basing yourself on libraries to do the heavylifting for you: you don't want to reinvent an IO layer to read from/write to the console on your first assignment.

In which language are those libraries written ?

In which language are those libraries documented ?

If you answer Morrocan Arabic, I will be surprised.

Unless you only rely on the lectures you assist to, and those present comprehensive documentation on every library feature you will need to use (and perhaps even translated libraries), then you will have to learn a modicrum of the English language. But then, you probably did already long before you started this programming course anyway.

English is...

... the lingua franca of programmers (and most scientists).

The sooner one admits it, and goes along with it rather than fighting against it, the sooner one can truly learn and progress.

Some will inevitably raise against this, and rightly defend their right to speak the language of their choice (their maternal language usually), however, as Babel demonstrated, the more languages are used, the more difficult communication gets.

Still...

Yes, as it had been argued over and over, some Unicode support (mainly symbols) can greatly ease comprehension for people having to translate mathematical or physics formulas, for example, into code. There is the drawback that some symbols are overloaded, but it could still help.

So why ?

Well, as said, it's not really about user convenience, as much as it is about marketing claims. It's dead easy too, since the parser is already Unicode aware for strings and comments anyway, so most take the jump.

And there might be a benefit for certain users.

But I personally will only deal with code written with English identifiers. I don't care if you need my help with your piece of code or if your library is just awesome and I could gain much by using it: if I cannot understand it, I'll just have to ignore it.


答:
Since you want to learn how lexers work, I presume you actually want to know how lexer generators work.

A lexer generator takes a lexical specification, which is a list of rules (regular-expression-token pairs), and generates a lexer. This resulting lexer can then transform an input (character) string into a token string according to this list of rules.

The method that is most commonly used mainly consists of transforming a regular expression into a deterministic finite automata (DFA) via a nondeterministic automata (NFA), plus a few details.

A detailed guide of doing this transformation can be found here. Note that I haven't read it myself, but it looks quite good. Also, just about any book on compiler construction will feature this transformation in the first few chapters.

If you are interested in lecture slides of courses on the topic, there are no doubt an endless amount of them from courses on compiler construction. From my university, you can find such slides here and here.

There are few more things that are not commonly employed in lexers or treated in texts, but are quite useful nonetheless:

Firstly, handling Unicode is somewhat nontrivial. The problem is that ASCII input is only 8 bits wide, which means that you can easily have a transition table for every state in the DFA, because they only have 256 entries. However, Unicode, being 16 bits wide (if you use UTF-16), requires 64k tables for every entry in the DFA. If you have complex grammars, this may start taking up quite some space. Filling these tables also starts taking quite a bit of time.

Alternatively, you could generate interval trees. A range tree may contain the tuples ('a', 'z'), ('A', 'Z') for example, which is a lot more memory efficient than having the full table. If you maintain non-overlapping intervals, you can use any balanced binary tree for this purpose. The running time is linear in the number of bits you need for every character, so O(16) in the Unicode case. However, in the best case, it will usually be quite a bit less.

One more issue is that the lexers as commonly generated actually have a worst-case quadratic performance. Although this worst-case behaviour is not commonly seen, it might bite you. If you run into the problem and want to solve it, a paper describing how to achieve linear time can be found here.

You'll probably want to be able to describe regular expressions in string form, as they normally appear. However, parsing these regular expression descriptions into NFAs (or possibly a recursive intermediate structure first) is a bit of a chicken-egg problem. To parse regular expression descriptions, the Shunting Yard algorithm is very suitable. Wikipedia seems to have an extensive page on the algorithm.

Title: Writing a lexer in C++


答:
Keep in mind that every finite state machine corresponds to a regular expression, which corresponds to a structured program using if and while statements.

So, for example, to recognize integers you could have the state machine:

```<code>0: digit -> 1
1: digit -> 1
</code>```

or the regular expression:

```<code>digit digit*
</code>```

or the structured code:

```<code>if (isdigit(*pc)){
  while(isdigit(*pc)){
    pc++;
  }
}
</code>```

Personally, I always write lexers using the latter, because IMHO it is no less clear, and there is nothing faster.

Title: Writing a lexer in C++

Body:
Keep in mind that every finite state machine corresponds to a regular expression, which corresponds to a structured program using if and while statements.

So, for example, to recognize integers you could have the state machine:

0: digit -> 1
1: digit -> 1

or the regular expression:

digit digit*

or the structured code:

if (isdigit(*pc)){
  while(isdigit(*pc)){
    pc++;
  }
}

Personally, I always write lexers using the latter, because IMHO it is no less clear, and there is nothing faster.


问:
Is this technique a design pattern? If so, what's it called?
I'll use C# as an example, but it should apply globally. Say I have a string value that should be one of a few constants, but I also want the client to set which string value to use so:

```<code>private int foo;
private string bar;

public int Foo {
    get { return foo; }
    set
    {
        foo = value;
        bar = getStringValueFromDatabase(value);
    }
}

public string Bar { get { return bar; } }
</code>```

I use this technique quite a lot and want to know if it's considered as any formal concept.

答:
The concept that I think of is something similar to a database foreign key, an association between two entities where one entity refers to the other through a handle (the key). More generally, you can call it an indirect association.

There are two time dependent aspects to this:

Setting the handle, thus creating the association

Retrieving the associated entity through that handle

There're many ways to code this, with eager or lazy loading, by fetching the related entity every time it's requested, by using a proxy, with or without temporal coupling, and also whose responsibility it is to carry out the two aspects above (could be the referring entity or the client code), which could potentially turn it into a direct association from the referring entity to the referred-to entity (the indirection would only be know to the client code).


答:
Null references are a mistake because they allow non-sensical code:

```<code>foo = null
foo.bar()
</code>```

There are alternatives, if you leverage the type system:

Maybe<Foo> foo = null
foo.bar() // error{Maybe<Foo> does not have any bar method}
```

The generally idea is to put the variable in a box, and the only thing you can do is unboxing it, preferably enlisting the compiler help like proposed for Eiffel.

Haskell has it from scratch (Maybe), in C++ you can leverage boost::optional<T> but you can still get undefined behaviour...

Title: Are null references really a bad thing?

Body:
I've heard it said that the inclusion of null references in programming languages is the "billion dollar mistake".  But why?  Sure, they can cause NullReferenceExceptions, but so what?  Any element of the language can be a source of errors if used improperly.

And what's the alternative?  I suppose instead of saying this:

Customer c = Customer.GetByLastName("Goodman"); // returns null if not found
if (c != null)
{
    Console.WriteLine(c.FirstName + " " + c.LastName + " is awesome!");
}
else { Console.WriteLine("There was no customer named Goodman.  How lame!"); }
```

<p>You could say this:</p>

<pre class="lang-cs prettyprint-override"><code>if (Customer.ExistsWithLastName("Goodman"))
{
    Customer c = Customer.GetByLastName("Goodman") // throws error if not found
    Console.WriteLine(c.FirstName + " " + c.LastName + " is awesome!");
}
else { Console.WriteLine("There was no customer named Goodman.  How lame!"); }
</code>```

But how is that better?  Either way, if you forget to check that the customer exists, you get an exception.

I suppose that a CustomerNotFoundException is a bit easier to debug than a NullReferenceException by virtue of being more descriptive.  Is that all there is to it?

Body:
Null references are a mistake because they allow non-sensical code:

foo = null
foo.bar()

There are alternatives, if you leverage the type system:

Maybe<Foo> foo = null
foo.bar() // error{Maybe<Foo> does not have any bar method}

The generally idea is to put the variable in a box, and the only thing you can do is unboxing it, preferably enlisting the compiler help like proposed for Eiffel.

Haskell has it from scratch (Maybe), in C++ you can leverage boost::optional<T> but you can still get undefined behaviour...


答:
With all the money at stake with the major database companies and the large group of developers on open source db apps, if there was a way to do it, they would have figured it out by now (And blasted the results all over the internet.).

I wouldn't. Instead, create specific benchmarks for specific needs and environments.

At some point, the amount of money available and the designer's expertise with a particular database may determine the limitations more than anything. A good Oracle dba will out perform most junior developers regardless of what platform they choose.

Title: Benchmarking databases

Body:
I see lots of discussions flying around about performance of db 'x' or that moving from 'x' to 'y' improved our site performance.

I'm yet to see proper benchmarking that works across different types of databases.

Is it possible to write a meaningful benchmark that could be used across multiple db types, such as Relational, Document-oriented, etc.

How would you go about designing such a benchmark?


答:
Your question ("without wasting resources") is too general, so it's hard to give much advice. Taken literally, if you don't want to waste resources, maybe you should take a step back and evaluate whether you need doing anything at all, i.e., whether you can solve the problem in other ways.

Also, useful advice is very dependent on your constraints - what kind of system are you building and what kind of CPU are you using? Is it a hard real-time system? How much memory do you have for code and data? Does it support natively all C operations (most notably, multiplication and division) and for what types? More generally: read the entire data sheet and understand it.

The most important advice: keep it simple.

E.g.: forget about complex data structures (hashes, trees, possibly even linked lists) and use fixed-size arrays. Use of more complicated data structures is warranted only after you have proven by measurement that arrays are too slow.

Also, don't overdesign (something Java/C# devs have a tendency to do): write straightforward procedural code, without too much layering. Abstraction has a cost!

Get comfortable with the idea of using global variables and goto [very useful for cleanups in the absence of exceptions] ;)

If you have to deal with interrupts, read about reentrancy. Writing reentrant code is very non-trivial.

Title: Resources on Writing Efficient C Code for Micro-controllers?

Body:
Serious help needed here. I love programming. I've been reading bunch of books(such as K&R) and articles/forums online for C language lately. Even tried looking into Linux code(although, i was lost where to start but peeking into small libraries helped?).

I started as a Java programmer and in Java it's pretty cut and dry; If programs gets too big, slice it in classes then further into functions. Guidelines like, keep the code readable and add comments. Use information hiding and OOP techniques. Some of which still applies for C.

I've been coding in C now and so far i get programs to work one way or the other. A lot of people talk about performance/efficiency, algorithm/design, optimization, and maintainability. Some people stress one more then the other but for non-professional software engineers you often hear something like e.g: Linux kernel dev won't just take any code.

My question is this: I plan on writing a code for 8-bit microcontroller without wasting any resources. Know that i'm coming from java background so things are not the same anymore... resources/books/links/tips will be much appreciated. Performance and size now matters. Resources/Tricks on efficient(within best practices) C code for 8-bit micro-controllers?

Also, inline assembly plays a vital role as well sticking close to micro-controllers standard. But are there general rule of thumb to efficiency that applies to all?

For example:
register unsigned int variable_name; is preferred over char anytime. Or using  uint8_t  if you don't need big numbers.

EDIT: Thank you so much for all the answers and suggestions. I appreciate everyone's effort for sharing knowledge.


答:
Manttz answer puts it very well the most key points about how to do "close-to-hardware" programming. This is after all what C is meant for.

However, i would like to add that while strict keyword of "Class" doesn't exists in C - it is quite straight forward to think in terms of Object oriented programming in C even when you are close to hardware.

You may consider this answer : OO best practices for C programs which explains this point.

Here are some resources which will help you write good object oriented code in C.

a. Object oriented programming in C

b. this is a good place where people exchange ideas

c. and here is the full book

Another good resource i would like to suggest you is :

The Write Great Code Series. This is two volume book. The first book covers very essential aspects of machines at lower level works. The second book touches upon is "Thinking low level - writing high level"


答:
(I'm a Java programmer, not C#.)

I would have a class ContactSet, with subclasses GoogleContactSet and WordFileContactSet.  The interface would have 4 methods: iterator(), add(Contact c), remove(Contact c) and update(Contact c).   Internally, ContactSet would store a Set (in Java, but I guess C# has an equivalent unordered set).  The method iterator() just returns the iterator of the underlying Set, and the same for add(...) and remove(...).
The contacts from Google at read in the creator new GoogleContactSet(googleID) and those from the file are read in the creator new WordFileContactSet(fileName).

I would also add a fifth method in ContactSet: findExtraneous(ContactSet d) that returns a ContactSet of all Contacts that are in the current set, but not in d.  This would be implemented as a double loop over the iterators of this and d.

It seems quite easy to devise some tests for the above classes and methods.

findExtraneous(...) would be called at the start of the GUI code.  Then you have some code that seeks user confirmation and puts the Contacts that will be added/removed/updated in 3 corresponding ContactSet's.  Finally, you call googleContactSet.add(someContact) or remove or update on the corresponding set of contacts.  You'll have to set this up in some methods in the C# GUI (I'm not familiar with that part).

You don't need classes ContactUpdater, ContactRemover; that would be excessive OO.

Testing GUI code is always difficult, if not impossible.

EDIT: on top of contactSet.add(Contact c), put(Contact c) and remove(Contact c), you could add 3 corresponding methods that take a set as argument, e.g. contactSet.add(ContactSet set).  But I would not add a method that directly returns the underlying Set instead of the iterator, since the Set could then be manipulated directly by anyone.

Title: How to structure this program?

Body:
I am struggling with the best way to structure a program that I am writing because the method I currently have feels very clunky and each part depends a lot on the others.  This is what the program needs to do:

Parse a word document to extract a list of contacts.

Get a list of contacts in a specified group from my Google account.

Compare the contacts from the word document to the Google contacts and either:

Update the contacts on Google with the details from the word document or

Create a new contact on Google if one doesn't exist or

Delete any contacts on Google that are not in the word document.

I also want

to have a GUI for this program that is responsive while all of the above is going on

the show a preview of the changes that steps 4-6 will make before actually performing them and allow the user to reject the changes (all or nothing would be fine for now)

Edit: everything from here on down is what I've currently done.  I added it so that specific refactoring advice could be given if that was what was deemed necessary.  If you have a way that I can structure this program then you can mostly likely skip the rest of it.

I've already made a start so allow me to explain how I've written the code so far.  The program is written in C#.

The parsing of the word document to get a list of contacts is in a class called WordDocumentParser.  It has a constructor which takes the path of the word document and a GetContacts method which performs all of the parsing and returns a list of contacts.  For what it's worth, I'm using COM to handle the parsing.

Steps 2-6 from above all start from a new class called GoogleContactsMaintainer with a constructor that takes the list of contacts from the word document, a couple of important groups on Google and a ContactsRequest which is used to retrieve/update/save/delete contacts on Google.  The first thing this class does is to get the contacts from Google and match them to the contacts from the word document.  The matched Google contact is saved in a property on the word document contact.

Then I do a preview which is the next 3 steps.

I start off with updating the contacts that have been matched from the word document to the Google contacts.  I create a ContactUpdater which updates the matched Google contact but doesn't commit the changes.  It also adds to a list of strings any changes that have been made and sets a boolean property on the contact to say that it has been updated.

Next is creating new Google contacts for those word document contacts that haven't been matched to a Google contact.  This is done in a ContactCreator class which creates a Google contact, sets it on the word document contact and sets a flag on that contact to indicate it is a new contact.  The new contact is not saved.

Last is to delete any Google contacts that haven't been matched to a contact from the word document.  To do this, I just find those contacts and add them to a list with a description saying that they will be deleted.

At the minute, I display these changes to the user and then immediately update them (I'll explain why the user can't stop this in a minute).  This is done in the following three stages.

Update: find any contacts from the word document that have the HasBeenUpdated property set, construct a new ContactUpdater and commit the changes.

Create: find any contacts from the word document that have the NewContact property set, construct a new ContactCreator and insert the new Google contact.

Delete: find any Google contacts that haven't been matched to a contact from the word document and delete them from Google.

In an attempt to keep my GUI responsive while all of this is going on, I created a MaintainContactsWorker class which subclasses BackgroundWorker and calls all of the above 10 steps from the OnDoWork method.  This is why I am unable to ask the user if they want to commit the changes or not.

To me, everything feels horribly inter-connected and completely untestable so I am hoping you can point out things that I should be doing to break the dependencies and make my code more maintainable and testable.

From writing this post, I one thing that I feel I should have is a repository which would handle the CRUD operations I am performing on the Google contacts.  Does this seem like a good place to start?

Thank you for any suggestions you can offer, hopefully I've explain myself well enough.


问:
How would you design a generic layer for OpenGL windowing that does not care how the program underneath actually looks like?
The kind of OpenGL windowing that I refer to is like GLFW, GLUT, SDL, etc.

Imagine three C++ programs: (ignore syntax)

Game A:

```<code>class Game
    resize()
    keyEvent()
</code>```

Game B:

```<code>resize()
keyEvent()
</code>```

Game C:

```<code>class Key
    event()
class Display
    resize()
</code>```

In A, everything is in one class, in B nothing is in a class, and in C the two events have their own classes. Another that is not shown is subclassing a provided 'Game' class that has predefined functions overloaded.

I would like any reasonable means that can allow the main library to delegate out to each of these three cases. The solution should not require separate storage of a 'this' parameter.

I have thought of a couple of ideas, one is a way of mapping the function in a functor. Another was lambdas. Lambdas are not okay, because they are not widely implemented yet. (Compilers of choice are VC 2010, gcc (latest stable), and Apple's clang).

Any new C++ 2011 features that are widely implemented are reasonable thoughts.

A gentleman at the local university suggested something similar to the functor, but I cannot remember the function he was talking about.

答:
I have thought of a couple of ideas, one is a way of mapping the
  function in a functor.

std::function -> problem solved. If you have a compiler that doesn't support std::function, then use boost::function. Lambdas have nothing to do with it- if you're using a compiler that supports them then you can do it, else use boost::bind.


问:
What unit test frameworks exist for Java?
I've used TestNG and JUnit.  What other frameworks are out there? What makes them special and/or different from the rest?</p>

答:
A whole new world of testing frameworks for Java exists via JRuby.  For example:
Test::Unit, RSpec, Shoulda, ZenTest, RSpec Mocks, Mocha and Cucumber.

I think it is great to write Java tests in the elegant Ruby language. Of course, this depends if you are in the mood of having multiple languages in your SCM.