问:
Do we need Logging when doing TDD?
When doing the Red, Green & Refactor cycle we should always write the minimum code to pass the test. This is the way I have been taught about TDD and the way almost all books describe the process.

But what about the logging?

Honestly I have rarely used logging in an application unless there was something really complicated that was happening, however, I have seen numerous posts that talk about the importance of proper logging.

So other than logging an exception I couldn't justify the real importance of logging in a proper tested application (unit/integration/acceptance tests).

So my questions are:

Do we need to log if we are doing TDD? won't a failing test reveal
what wrong with the application?

Should we add test for the logging process in each method in each class?

If some log levels are disabled in the production environment for example, won't that introduce a dependency between the tests and enviroment?

People talk about how logs ease debugging, but one of the main advantages about TDD is that I always know what's wrong due to a failing test.

Is there something I am missing out there?

答:
1) Do we need to log if we are doing TDD? won't a failing test reveal what wrong with the application?

That assumes you have every possible test your application needs, which is rarely true.  Logs help you track down bugs you did not write tests for yet.

2) Should we add test for the logging process in each method in each class?

If the logger itself is tested, it would not need to be retested in each class, similar to other dependencies.

3) If some log levels are disabled in the production environment for example, won't that introduce a dependency between the tests and environment?

Humans (and log aggregators) depend on the logs, the tests should not depend on them.  Typically there are several log levels, and some are used in production, and some additional levels are used in development, similar to:

"Rails log level is info in production mode and debug in development and test" - http://guides.rubyonrails.org/debugging_rails_applications.html

Other applications use a similar approach.

4) People talk about how logs ease debugging, but one of the main advantages about TDD is that I always know what's wrong due to a failing test.

Production bugs will have passed all the tests, so you may need some other reference to investigate those issues.

Title: Do we need Logging when doing TDD?


问:
Is using a global to maintain the currently logged in user bad practice?
I'm doing some android development in a relatively inexperienced group, and all of the data used by our application is managed in a database. There are several instances where we'll need to pull info from the database using the identifying information/PK of the user who is currently logged in. Because the only info from the user that is important is the PK (IE we don't need an entire class to represent the types of users or any associated data), and because this ID is important throughout the scope of the application and will not change once it is defined, we considered storing the ID/PK of the user in a global on successful login.

Is the use of a global variable in a circumstance like this bad practice? Is there a better way to handle it?

答:
Using globals is sort of like risky driving.  It probably won't hurt you right away, but it makes you complacent about the ways you could get hurt down the road.  It might get you there a little faster now, but that's largely because other drivers are driving defensively, and there happens to not have been any unexpected obstacles in the road.

Using globals makes it very difficult to see when you are creating excess coupling, because there's no penalty at the time of writing.  If you have to pass a User object around, eventually you'll get annoyed at having to pass it around, and figure out a way to minimize the areas where it is used.  Usually you don't need a piece of data in as many places as you think you do.  For example, maybe you can avoid needing your user PK "throughout the application" by using a placeholder in your queries, then substituting the real PK in one place just before it gets run.

As for the idea that you have one logged in user, will only ever have that one user, and will only ever need the PK for that user, I give you the tale of Netflix.  When I first signed up for the service, they had one DVD queue per account.  That made it really difficult for my wife and I to share.  We had to constantly rearrange the queue in order to ensure one of us didn't miss out on DVDs for a stretch.

Then they introduced multiple queues, which was great.  However, after a while they said they had to discontinue that feature, because it was making it too unwieldy to maintain their code.  I wasn't privy to the details, but I imagine it was due to their architecture being similar to what you are asking about.  Maybe not using a global for the user, but making assumptions that made it effectively the case.  They painted themselves into a corner when the account user started potentially being different from the queue user.

By that time, video rental stores were stocking a lot more DVDs, so we cancelled our account.  We didn't come back until they fixed that problem.  It's only recently that they managed to fix it for streaming as well.

My point is you have no way of knowing what features you're going to need or want down the road.  Creating excess coupling is going to make it much more difficult to respond to market opportunities down the road.  It's worth the small extra effort it will take to avoid globals now.

ContentLicense: CC BY-SA 3.0


答:
Unless you have a 100% test cover, which is usually not the case, you can't know that your software will never crash (EDIT:and — as said in the comments — even if it does, something independent of your software might cause a crash); it's the same as thinking you can do a software that has absolutely no bugs (not even the NASA can do this).
So at the very least, you need to log possible failures in case your program crashes so you can know why.

The logging should be done by an external library or intern framework depending on the technology you're using. What I mean by that is that it should be something that has already been tested before and that you don't need to test yourself. It's overkill to test that every methods logs the things it's supposed to.

The logs are not meant for tests, there should be no dependency whatsoever. That said, you don't need to disable logging for tests if it feels like a constraint to you, though the logs should be kept in a file corresponding to the environement (you should have a different file for the testing, development and production environment at the very least).

An error might be very unclear and it's not always obvious what's gone wrong when one TDD test failed. Logs should be more precise. For instance, if you're doing a sorting algorithm and the entire test case fails, you should have logs for every single test of the algorithm which help you spot where the problem actually lies.

Title: Do we need Logging when doing TDD?

Body:
When doing the Red, Green & Refactor cycle we should always write the minimum code to pass the test. This is the way I have been taught about TDD and the way almost all books describe the process.

But what about the logging?

Honestly I have rarely used logging in an application unless there was something really complicated that was happening, however, I have seen numerous posts that talk about the importance of proper logging.

So other than logging an exception I couldn't justify the real importance of logging in a proper tested application (unit/integration/acceptance tests).

So my questions are:

Do we need to log if we are doing TDD? won't a failing test reveal
what wrong with the application?

Should we add test for the logging process in each method in each class?

If some log levels are disabled in the production environment for example, won't that introduce a dependency between the tests and enviroment?

People talk about how logs ease debugging, but one of the main advantages about TDD is that I always know what's wrong due to a failing test.

Is there something I am missing out there?


答:
The short answer to your main question is: as a general rule, bugs in your code will NOT be exposed by TDD. Some might, ideally many, but the absence of failing tests does not imply the absence of bugs. This is a very important maxim in software testing.

Since you cannot know whether you will have incorrect behavior in your system, maybe under rare conditions, logging is a useful tool that might help understand what's wrong when things inevitably do go wrong.

Logging and TDD address different concerns.

Title: Do we need Logging when doing TDD?


答:
Logging is useful to explain non-exceptional behavior of an application:

Event logs record events taking place in the execution of a system in order to provide an audit trail that can be used to understand the activity of the system and to diagnose problems. They are essential to understand the activities of complex systems, particularly in the case of applications with little user interaction (such as server applications)...

No matter how application was tested and no matter how well exceptions are logged, its users may ask,

output of your program is 0 while we expected it to be 1, why is that?

You need logging to verify what was application config, parameters and other runtime details to explain its (non-exceptional) behavior.

From above perspective, logging is more oriented on support than on development. After the application goes live, it is desirable to let someone else handle questions from users, to allow programmers focus on further development.

Logging what application does allows that someone else understand program behavior without digging into the code and without distracting developers by requests to explain what's going on.

Title: Do we need Logging when doing TDD?


答:
Most answers here focus on the aspect of correctness. But logging serves a different purpose, too: Logging can be a way to gather performanc relevant data.  So even when the system works without error a log can tell why it is slow. Even with full test coverage of all aspects a test suite won't tell.

Of course a performance critical system can/should provide key performance metrics to some operational dashboard, but "classic" logging can provide a different level of detail.

Title: Do we need Logging when doing TDD?


答:
TDD generally helps reduce coding bugs. It helps a lot less with bugs with the specification or just misunderstandings about how things work.

"Oh? You can get a data message before you get confirmation the logon succeeded? I never knew that, well it won't handle that!"... That kind of thing. Logging is very useful for telling you what the software tried to do so you can spot the thing you did wrong.

Title: Do we need Logging when doing TDD?


问:
How to put lessons learned, good practices, etc into the "work flow"
As the title states it, I would like to get some suggestions about putting knowledge into action.

We have many additional requirements that concern: coding practices feature development (all of them or only a subset), process, etc. The problem is that we have problems with introducing those practices into new projects and I want to help developers and reviewers to remember about it, but I don't want them to have everything just in theirs heads, but rather in some kind of a database that they can use easily.

The list of practices is already defined in Excel. I would like all team members to apply these practices in their work but I don't know how make those information easy to find. When developer starts working on a feature he should be able to easily find all practices that he should use in these feature.

To be clear with what I mean, I'm showing some examples of information we have to apply:

(new design request) every feature must output logs (and it must not contain any sensitive data);

(new design request) every feature has to have a flag in the configuration that allows to disable it;

(good practice) always update docs when feature is ready;

(retrospective feedback) QA must test only on release package (not in debug mode);

(retrospective feedback) make stress tests for every new feature implemented;

(lead's task) write release notes after each sprint that includes tasks completed and open bugs;

(design usage) every event from "ABCStoreManager" must be disconnected after being invoked;

(design usage) try-catch every event.Invoke() call;

I thought for a while about a wiki, but it's no good, because it doesn't support tagging/categories or querying and I'm afraid that everybody would ignore it (people must know exactly where to look).

My question can be summed up as how can I improve communication to our developers about the required development methodologies and practices listed above in an easy way?

Remarks:

this question is not about security issues or code smells per-se;

I'm not looking for any heavy process (like RUP) or any process for that matter, which forces you to go step by step. Preferably I am looking for an agile approach

Daniel Figueroa has suggested adding additional requirements to the "definition of done". And it seems like a good way. But the problem is that some features have, for instance, 20 additional requirements ("all GUI features"), some 10 ("all server requests"), etc. I would like to have this stuff aggregated in a one place and just use links ("see: 'GUI feature' ");

答:
We used a wiki at a previous job, and it worked well. Any changes were publicised to the developers, and the team leads discussed it at regular intervals.

It seems to be that in in your list, you have both programming best practice and process definitions. I'd suggest first writing up the process by which you develop software. Then you can add sub-polices regarding best practice or language-specific guidelines. The main policy should refer to it and make it clear that developers should follow the style practice guidelines unless there is good reason not too.

Note that I'm not advocating writing pages and pages of docs - just enough to make it clear what a dev is supposed to do at any particular point in a project. If you can't easily summarise it, I'd suggest the current process itself is vague.


问:
Where or when to start to write your own PHP framework?
First of all, I know that reinvent the wheel is, usually, a bad idea, but I want to write a PHP framework to learn OOP in PHP and to understand MVC in my free time.

Said that, I have to do two webs with few and simple database interactions, the basic funcionality of the webs is to show information.

I want to use my own framework in those webs, but I don't know where or when to start. Should I start doing the framework first and then the webs? or should I start doing one web, then the framework and then the other web?

答:
Developing your own site/app framework in PHP is quite a learning experience. Over time, i have built 3 from the ground up. I strongly advise to pursue it.. but set yourself a (measurable!) shortterm goal, as for features. You'll want to throw it aside soon to start over! :)

My way..

Number one was to learn and keep it simple to me.

Number two was to learn and make it hard for me.

Number three was to deliver fast. (I couldn't find a off-the-shelve ones that didnt over-complicate, so i built a custom KISS one-trick-pony.)

In hindsight i do not regret starting any of these, i learned a lot. My next framework will be Symphony2, i appreciate the "over-complicated" parts of more since building my last framework.


问:
Stopping spiders or unauthorised users from viewing files
I am stumped at this problem.

Users authenticate at my client's website. Then the users see their profile page on my client's website. My client now wants me to provide a URL on their profile page, for his users, to download information from an external website. (This external website is not controlled by my client). That is to say, upon clicking this URL, it links out to an external website and gets a personalized file.

As the information is considered sensitive (not critical), the external website should not allow anyone to download the personalized file.

I'm stumped at figuring out how to allow only one authentication (on my client's website) instead of having the end users authenticating twice (once on my client's website and separately on the external website)

Any ideas?

答: And what about having one login/password for your client at the second server? After a user is logging to your client server he can launch the connection to the second server by javascript, not seeing neither login nor password. Of course, it is not very safe, but more safe than no login/password.</p>


问:
Big Data: Can it be pre-processed?
My question is about "big data". Basically, big data involves the analysis of a large amount of data to make meaningful insights from it.

I would like to know:

Whether or not large amounts of data can be pre-processed? (like say for example you are running some matching service for people, so you take all the information you have on the people and you process it at a certain point for use later on)

If pre-processing is possible, how would you normally go about doing this?

To help narrow the scope of my question, please look at this hypothetical scenario.

Say I have a customer database and my company is a global retailer
  that is using some type of points system to reward the shoppers (for
  arguments sake, the points are tallied up on a type of electronic card
  or mobile app).

So based on my rewards system, I am now able to fully aware of exactly
  what a shopper is purchasing and when they normally make purchases of
  recurring items.

My database is growing all the time with this information and I would
  now like to make recommendations (or send notifications) to shoppers
  about special offers of products they buy or related products that may
  interest them, when they enter 1 of the stores.

Instead of processing all the accumulated data when a shopper enters
  the store, I would like to continually process the data-stream as the
  data comes in (meaning from previous shopping experiences), so that
  when it comes time to make a recommendation (for the next time a
  shopper walks into the store), it is simply a matter of retrieving the
  recommendations and providing a list of it to the shopper.

With this method in mind, I can easily space out my CPU-intensive
  tasks, instead of say: processing all customer data on a busy day when
  foot-traffic is at peak volumes.

By asking how I would do this, I would be referring to common methods available for achieving this. This can include any special databases or programming techniques or even specialized software that can carry out these timed calculations that can "pre-process" the data at specific times, in order to balance out CPU-intensive tasks.

You can consider the customer-recommendation scenario as the "situation". It is the best example scenario I could think of that would explain why "pre-processing" (or calculating the recommendations at specific times) would make sense.

答:
In a past job, I was a DBA for a global solutions company where databases with millions and billions of rows were the norm.

As datasets got larger, it became more and more problematic to turn around complex queries in a timely manner.

Among many strategies we adopted, 4 spring to mind:

Result sets for common queries were stored in what we called "strips". These were basically index-organised tables that stored keys to stop repeated joins in subsequent queries

Denormalising tables yielded huge benefits to reduce the number of joins

Tables were partitioned in line with common queries e.g. postcode/zip code etc

Whilst all data was available in the repository, only fully formed data and cleansed data was allowed thru to the mart for querying

On top of this you can overlay pre-calculated segments. For example - rather than try to pull say, all blue collar workers in the country, you can use segmentation to drill down only in those areas which are predominantly blue collar.

EDIT (following Joe's update)

In that case you might want a reporting mart in addition to the mart and repository I described above which is lean and mean and optimised for fast queries and MI reports.

Title: Big Data: Can it be pre-processed?


答: Sure that's what an incremental map reduce is for. Essentially you perform an operation on the collection which processes the existing documents and puts them in a new collection, and as you add new documents you merge those rows into the derived collection.</p>

Title: Big Data: Can it be pre-processed?

Body:
My question is about "big data". Basically, big data involves the analysis of a large amount of data to make meaningful insights from it.

I would like to know:

Whether or not large amounts of data can be pre-processed? (like say for example you are running some matching service for people, so you take all the information you have on the people and you process it at a certain point for use later on)

If pre-processing is possible, how would you normally go about doing this?

To help narrow the scope of my question, please look at this hypothetical scenario.

Say I have a customer database and my company is a global retailer
  that is using some type of points system to reward the shoppers (for
  arguments sake, the points are tallied up on a type of electronic card
  or mobile app).

So based on my rewards system, I am now able to fully aware of exactly
  what a shopper is purchasing and when they normally make purchases of
  recurring items.

My database is growing all the time with this information and I would
  now like to make recommendations (or send notifications) to shoppers
  about special offers of products they buy or related products that may
  interest them, when they enter 1 of the stores.

Instead of processing all the accumulated data when a shopper enters
  the store, I would like to continually process the data-stream as the
  data comes in (meaning from previous shopping experiences), so that
  when it comes time to make a recommendation (for the next time a
  shopper walks into the store), it is simply a matter of retrieving the
  recommendations and providing a list of it to the shopper.

With this method in mind, I can easily space out my CPU-intensive
  tasks, instead of say: processing all customer data on a busy day when
  foot-traffic is at peak volumes.

By asking how I would do this, I would be referring to common methods available for achieving this. This can include any special databases or programming techniques or even specialized software that can carry out these timed calculations that can "pre-process" the data at specific times, in order to balance out CPU-intensive tasks.

You can consider the customer-recommendation scenario as the "situation". It is the best example scenario I could think of that would explain why "pre-processing" (or calculating the recommendations at specific times) would make sense.


问:
OO design for a Windows application that communicates with an external machine via RS232
I'm after a bit of OO design advice... I'm about to start developing a Windows application that communicates with an external machine via RS232. The machine has an onboard "system controller" consisting of registers (addresses). You can write a value to a register (e.g. to turn a pump on), or read a value from a register (e.g. to see if the pump is running). The app will have a config file detailing each register's settings, e.g. its address, whether it's 16- or 32-bit, a friendly "alias" (so other code can deal with "Pump XYZ" rather than address "50B3D124"), and a few other things.

My first thought was to have a Register class reflecting these properties, plus Read() and Write() methods. But this raises the first question - should the Read() method have a return value, or should it populate a Value property on the class?

Similarly, should the Write() method include a "valueToWrite" parameter, or should it write whatever value is currently held in the Value property?

I could take this one step further (possibly even answering the above question) - instead of Read/Write methods, put the functionality into the Value property's getter and setter? Using a property for this purpose doesn't feel right though.

I then started wondering if a Register class was even necessary, as its read/write functionality would be doing little more than calling Read/Write methods on a SystemController class, e.g.

```<code>long ReadRegister(string registerAlias);
void WriteRegister(string registerAlias, long value);
</code>```

Why not just let the rest of the application read/write registers via the  SystemController class? (I suspect I would still need some concept of a Register class, even if it's just to represent a register's config settings).

答:
There are several approaches that can work; which is best may depend upon the speed and reliability of the communications protocol, the nature of the operations, and the extent to which use of the program will "flow" better if users wait for each request to complete before proceeding to the next, enter requests as fast as they wish and have the program handle them as fast as it can, or have users enter all requests and then process them as a group.

If you want the user interface to wait while each operation is performed, then having methods which simply perform operations directly but don't keep any state may be the simplest way of going about it.  Such an approach is simple, and generally makes it easy to tell what's going on.  If the remote end of the connection has a few important aspects of state which affect the way communications will be performed (e.g. communications mode settings, or packet sequence numbers), it may be helpful to have the class manage those aspects of state itself, but aspects of state other than communication should be managed by client code.

An alternative design is to have "read all state" and "write all state" methods, and have the UI flow entail loading state from a device or opting to create new state, editing the state without involving the device, and then storing the state to the device once editing is complete.  This approach will minimize the number of times a user will have to wait for the device, consolidating all delays into one wait at the beginning and one at the end.  The one limitation with this approach is that the user won't be able to see the effect of any changes on the remote device until the "apply" button is pressed (since no changes will actually be made until then).  Whether that's a good or bad thing will depend upon the nature of the device.

A final approach is to keep two main sets of state--what the device has last reported, and what changes (if any) have been requested but not yet applied; along with that one may wish to keep track of how many unsuccessful attempts have been made to perform various operations.  One might for each device property display a read-only and editable field next to each other.  A background task should scan the properties and determine what things need updating on either the screen or in the device, and perform any updates as it notices them.  I would suggest having the read-only text boxes implemented with a custom type with a method that can set the text at any time from any thread.  The method should immediately set a private variable to the new text and BeginInvoke an UpdateText method when no update request is already pending.  If multiple requests are made to set the text before the UI thread has started processing the first one, the repeated operations should not generate multiple BeginInvoke calls.  Instead, the later operations should simply change the string which will get shown when the UpdateText method finally starts executing.

An important thing to bear in mind is that while the actual properties of the physical device and the desired properties would ideally be one and the same, they are separate aspects of state and should probably be both maintained and (especially with the third approach) displayed as such.  If for whatever reason, reading back the device state after setting it doesn't yield the new value, code should be prepared to convey that information to the user.  The user might be confused as to why the value isn't updating, but will probably be less confused than if software suggests that the device holds the new value but it really doesn't.


问:
It is bad practice to hard couple logging interface/implementation with business model classes?
Should we avoid using logging in business model classes and try to move logging to service/controller classes?

In for example java context, logging in model classes will hard couple the code with dependencies on slf4j or commons-logging etc.

答: Aspect Oriented Programming is a great way to separate the logging concern with from your other code. It's relatively easy to implement trace and exception logging in a single class. </p>


问:
How to write camel case for words like "phonenumber", "motorcycle", "wavelength", etc
I am having a problem in understanding how to apply camelCase syntax to some of my variable names.

For example, how should I correctly write a word like "phonenumber" in camel case?  Is it phoneNumber or phonenumber?  Similarly with "username", is it username or userName?

I think it doesn't look right with camel case like motorCycle, passWord, sunDay, setUp or waveLength since these are just one word each. I think that could be why it's called hashMap but also hashtable in camel case without the capital in the last case because hashtable is one word while hash map is two words.

But if the motorcycle has a color then would it be motorcycleColor since a word is concatenated?  Is that correct or should it be phoneNUmber, waveLength, sunBlock and even sunDay for the Sunday of the week?

Why for instance is the method called getISOCountries while it says HttpHeaders e.g. it's not clear what becomes lowercase if we have a method like String camelCaseString = dog.toCamelCase() or interface CamelCase.

Related: https://english.stackexchange.com/questions/889/when-should-compound-words-be-written-as-one-word-with-hyphens-or-with-spaces

答:
You should capitalize the letter after where there would "normally" be a space (when you would write a letter instead of sourcecode). Your first assumption is correct:

It's a "motorcycle", not a "motor cycle", so your variable should be named motorcycle, not motorCycle.

It's a "hash map", so you go with hashMap, not hashmap.

"Sunday" is one word -> sunday, not sunDay.

While "day of week" would be dayOfWeek, not dayofweek.


问:
What's a nice explanation for pointers?
In your own studies (on your own, or for a class) did you have an "ah ha" moment when you finally, really understood pointers? Do you have an explanation you use for beginner programmers that seems particularly effective?

For example, when beginners first encounter pointers in C, they might just add &s and *s until it compiles (as I myself once did). Maybe it was a picture, or a really well motivated example, that made pointers "click" for you or your student. What was it, and what did you try before that didn't seem to work? Were any topics prerequisites (e.g. structs, or arrays)?

In other words, what was necessary to understand the meaning of &s and *, when you could use them with confidence? Learning the syntax and terminology or the use cases isn't enough, at some point the idea needs to be internalized.

Update: I really like the answers so far; please keep them coming. There are a lot of great perspectives here, but I think many are good explanations/slogans for ourselves after we've internalized the concept. I'm looking for the detailed contexts and circumstances when it dawned on you.

For example:

I only somewhat understood pointers
  syntactically in C. I heard two of my
  friends explaining pointers to another
  friend, who asked why a struct was
  passed with a pointer. The first
  friend talked about how it needed to
  be referenced and modified, but it was
  just a short comment from the other
  friend where it hit me: "It's also
  more efficient." Passing 4 bytes
  instead of 16 bytes was the final
  conceptual shift I needed.

答:
The key to explaining pointers is making sure that the people you are explaining to, already have an understanding of the concept of memory.  Whilst it would be nice if they really understood it low level, believing that memory exists as a massive array, and understanding that you can access any position in the array by its index location is enough.

The next step, having the concept of passing the index location rather than copying the entire memory makes sense to most people.  And that is enough to allow most people to understand why pointers are useful.

the final step to understand pointers is to explain how you can pass in as a parameter a memory index location for the method to store the index location for where all the data is stored.  I have found that this can be step too far for some people to cope with.

Once someone has grasped these basic steps its straight forward for them to grasp that you can chain pointers indefinitely, just as long as you keep track of how many times you need to look at addresses to find the actual data object.

Once someone has grasped pointers, the next thing that they must grasp quickly is the difference between heap memory and stack memory, and why pointers to stack memory are dangerous when passed outside the method.

ContentLicense: CC BY-SA 2.5

Title: What's a nice explanation for pointers?


问:
Is it bad practice to include two types of views in the same class?
In my app, I use a widget with a loading spinner to signify that a network request is waiting.

On failure, there will be another view which contains a failure message and a "Reload" button.

My question is, since both of these views/widgets will ALWAYS be used together (either one or the other will be shown - or neither), should I merge them into a single widget? Would that be bad or good practice? That way I wouldn't have to prepare them both whenever I want to use one of them.

The main disadvantage I see is that modularity would be destroyed.

答:
I realise this has been asked a while ago now but I thought I'd throw in my two cents.

I'm actually working on a project that is doing something like this at the minute. My opinion would be to not do this, unless all of your use cases can follow the same logic.

I've had an issue where there was a component that handles errors and displays messages to the user. It has inbuilt responses e.g.

Sorry couldn't connect to servers, please try again

We seem to be facing technical difficulties, please try again later

You don't seem to have a data connection

etc.

The main issue with this is that the error messages are being taken out of context. Receiving generic error A on the first screen might throw up "Sorry we couldn't connect", however I don't want to show that on the screen where you put through an order, I want to say, for example "Your order couldn't be processed at this time".

What you could very easily do is create a super class for error handlers, then you could create different subclasses and pass one of those into your "Single widget" as an init parameter, that would keep your modularity intact.

However the big issue I see with this is the overhead associated in doing this, given that customers always want little changes here and there. When this happens its a lot of reworking / creating another widget, just to get a piece of text to show up on the screen.

My opinion is that error situations are best off being handled by passing in a failure block of some kind. The logic to display a custom alert view can be abstracted away, but it can be common for different situations to necessitate different messages for the same error.

As I said earlier if you are in a situation where nothing will ever change (do you ever really know though?), then go for it, less duplicate code is always a plus.


答:
The simplest way to do camel case is on word delineation.

Examples from your question:

phone number: phoneNumber

motorcycle: motorcycle

sunday: sunday

wavelength: wavelength

If you wouldn't split it into two words, you also shouldn't use camel case on it.  Otherwise you're using it on syllables and how silly does this look

auxIlIArY (This is a 5 syllable word; not to mention the I's looking like crap on non-monospaced/sans-serif fonts).

Title: How to write camel case for words like "phonenumber", "motorcycle", "wavelength", etc

Body:
I am having a problem in understanding how to apply camelCase syntax to some of my variable names.

For example, how should I correctly write a word like "phonenumber" in camel case?  Is it phoneNumber or phonenumber?  Similarly with "username", is it username or userName?

I think it doesn't look right with camel case like motorCycle, passWord, sunDay, setUp or waveLength since these are just one word each. I think that could be why it's called hashMap but also hashtable in camel case without the capital in the last case because hashtable is one word while hash map is two words.

But if the motorcycle has a color then would it be motorcycleColor since a word is concatenated?  Is that correct or should it be phoneNUmber, waveLength, sunBlock and even sunDay for the Sunday of the week?

Why for instance is the method called getISOCountries while it says HttpHeaders e.g. it's not clear what becomes lowercase if we have a method like String camelCaseString = dog.toCamelCase() or interface CamelCase.

Related: https://english.stackexchange.com/questions/889/when-should-compound-words-be-written-as-one-word-with-hyphens-or-with-spaces


问:
Teaching myself, as a physicist, to become a better programmer
I've always liked physics, and I've always liked coding, so when I got the offer for a PhD position doing numerical physics (details are not relevant, it's mostly parallel programming for a cluster) at a university, it was a no-brainer for me.

However, as most physicists, I'm self taught. I don't have broad background knowledge about how to code in an object oriented way, or the name of that specific algorithm that optimizes the search in some kD tree.

Since all my work so far has been more concerned about the physics and the scientific results, I undoubtedly have some bad habits - more so because my coding is my own, and not really teamwork. I have mostly used C since it is very straightforward and "what you write is what you get" - no need for fancy abstractions. However, I have recently switched to C++ since I'd like to learn more about the power that comes with abstraction, and it's pretty C-like (syntax-wise at least).

How do I teach myself to code in a good, abstract way like a graduate in computer science?

I know my code is efficient, but I want it to be elegant as well, and readable. Keep in mind that I don't have time to read several 1000-page tomes about abstract programming. I need to spend time on actual, physics related research (my supervisor would laugh at me if he knew I spent time thinking about how to program elegantly). How do I assess if my work is also good from a programmer's perspective?

答:
As far as becoming a better programmers is concerned there is no magic bullet. If you're self-taught the key is self-awareness, which it sounds like you have. However, learning to code well mostly comes down to reading and practice.

Being critical of your own code is one of the best ways to get better. Always be asking yourself:

Will this be easy to change?

Is this easily testable?

Can I simplify this? Can I easily understand this when I see it again in 3 months?

My other suggestion would be don't lock yourself into C/C++. While those are good languages that are used for a reason, they require you to do a lot of things that are not subject matter related. Look into Matlab, I'd be surprised if the university doesn't have that available for you. Consider a scripting language like Python. Strongly consider picking up a functional language like Haskell - the paradigm is very mathematical in nature and would likely fit your problems like a glove. In short explore some other languages/paradigms. Even if they don't become a permanent tool in your belt they'll make you a better programmer.

You may also want to look into some algorithm design. I suspect having gotten the job, you're already relatively up to snuff on this, but algorithms are incredibly important when doing numerical analysis. In fact, I would suspect, there are resources specifically geared towards numerical analysis algorithms.

Never lose sight of your primary purpose in writing the code. You need to get things done. Becoming a better programmer is one method to do that. Selecting the right tools for the job is another.


答:
Part 1

I think what's tripping you up is that some of your examples are compound words, whereas others are not.

phone number // not a compound word

motorcycle // compound word

wavelength //compound word

sunblock // compound word

Sunday // compound word*

motorcycle color // compound word used with a non-compound word.

Compound words do not follow camel case notation for the second word in the compound word.

So it's motorcycle and not motorCycle.

But "phone number" is not a compound word and it would follow camel case notation. i.e. phoneNumber.

And that leads to: motorcycleColor.

If I can't recall if a word is truly a compound word or not, I look it up in the dictionary to be certain.

Part 2

Two of your examples are a little trickier.

hash map

hash table

They're tricky because some will spell them as "hashmap" and "hashtable".  According to the dictionary I just checked, they are separate words and not a compound word.  So the correct camel case would be hashMap and hashTable.  Not every team follows that convention, so you need to ask your teammates what they prefer and then stick with that.  Consistency is more important in this case than what a dictionary may say is correct.

 * Some will argue about Sunday being a compound word since other days of the week are not, but for the purposes of this question I'll sidestep that issue. It's easiest to treat as a compound word and therefore no camel case.


问:
How can Scrum be adapted to a Volunteer setting?
I have recently joined a young hackerspace still in the process of setting itself up.  We're fortunate because the space has a few internal projects that need working on and no shortage of volunteers to work on them.

There have been some discussions on how to organize these projects.  My most recent professional experience has been with Scrum so I'm considering pitching a Scrum approach for our software projects, but I'm not sure it will be a good fit.

Although I've seen Scrum work well for small full-time teams, the nature of this organisation is different:

The members are volunteers.  Some are full time students.  Others work jobs full time.  We can't expect a constant level of contribution from anyone as their real lives take priority.

While pretty much everyone has years of experience writing software, not many members have done so professionally or in teams.

There is no Product Owner.  The requirements for these projects are determined by a committee.  The members of this committee will also be working on the implementation.  This means we will have no single, dedicated, Product Owner.

We have no deadlines (soft or hard).  The projects will get done when they get done.

These are pretty significant differences, but I'm not convinced they will be blockers to applying Scrum.  I think some minor tweaking could get us over this hurdle:

If we change Sprints to have a fixed story-point size, but fluid duration (time), we can still benefit from iterative releases without putting unrealistic delivery pressure on volunteer devs.

We can ditch burndown charts and velocity calculation.  If I understand correctly, these are tools and metrics that work as a bridge between the dev team and the Management.  They serve to report progress in a form that is meaningful to both the developers and the stakeholders.  Considering we have no one to report to (no Project Manager, no Product Owner, and no outside stakeholders) I believe we can drop this altogether.

Things I think we could benefit from which won't require tweaking:

The Requirements Gathering meeting(s).  Where everyone sits around a table and discusses User Stories, sketches up UI mocks, and builds up a Product Backlog.

Sprint Retrospectives.  This will be an interesting way for us to converge on a development process that works for us as a team of volunteers.

Things I'm not sure about:

How should daily Stand-ups be treated?  I wonder if they would have much value at all in our setting.  My understanding of the stand-up ritual is that it helps communication by naturally disseminating information throughout the team.  Considering the fact that our Sprints will likely be delivering much less complexity than an average Sprint there might be less need to be abreast of all the other team members' progress/developments.

Should I push for XP things like Continuous Integration, Code Reviews, and TDD?  I'm concerned this will be asking for a lot.  I'd be more tempted to bring these concepts in on future projects once people are more familiar with Scrum and working as a team.

My Questions:

Can Scrum be adapted to a volunteer-based environment?

And, is my planned approach so far going in the right direction?

答:
You cannot adapt it the way you are describing and still call it SCRUM. but in my opinion, you can use Scrum like following for the setup you have described.

Have fixed iteration. So that you can measure your progress. This is
not for just for management but also for team to “know” how they are
performing.

Ask volunteers to share capacities at start of iteration. All team
needs to what members are committing otherwise certain member may
leave the team for work that more professionally done.

Having no deadline is fine. Scrum doesn’t forces that however what
you do at the end of each iteration should be potentially shippable.
This will let you decide what to do next based on what you have done
till then (which is working).

Having no product owner is can work as well.. You can have some sort of voting
system to stack rank backlog and accept/reject work done.

Requirement gathering is not part of Scrum. You can do it anyway you
want it. But do not include that as part of iteration scope. Have
separate capacity for that. So for a iteration, plan only that work for
which requirements are clear e.g. team knows the acceptance criteria.

Retrospective are good. So start Scrum as it but then using
retrospective see what is working and what is not e.g. you may need
to change iteration size, you may need to get rid of some volunteers
who are dragging everyone else..

Daily status are must. That gives opportunity to team to sync with
each other i.e. they will align their efforts so that no task gets
unnecessarily blocked due to unavailability of other member’s
deliverable.

XP is good. Have a strict definition of done based on XP e.g. no
code goes in unless reviewed. Do less to do more..


答:
As others have pointed out, camel case is used when joining words.  Do not use camel case within a single English word.  The problem comes in determining which compounds are single words.  Left to themselves, programmers will come to different decisions depending on their knowledge of English.  This leads to inconsistencies in capitalization, which are almost as annoying as misspellings.

To prevent this, programmers should use a standard reference (e.g. dictionary.com) when they are unsure about spelling or capitalization.  Even so, if consistency in naming is important to your project, there is no substitute for code review.

Picking a single reference for your project will resolve most arguments about capitalization and spelling. There are some compounds like Hashtable that are not in dictionaries, but are capitalized as single words in the Java and C# APIs.  You will want to make a project-wide decision about those.  In Java, it's "HashMap" but "Hashtable".  IMO "Hashtable" is clearly incorrect; it never appears as a single word except as a name in an API.


问:
Code ownership with multiple Scrum teams
If two Scrum teams use the same software component, who is responsible for providing a clear architectural vision of that component and maintain/develop this vision as the code base evolves? In Scrum you are supposed to have a collective code ownership, so how to make sure that development done by Team A doesn't interfere with development done by Team B?</p>

答:
I will take liberty to assume:

acceptance tests are automated..

component employs good OOP principles: Low Coupling and High Cohesion.

If above assumptions are correct then there should only occasionally be times when two teams would interfere with each other. They can solve it using the following ways:

As soon as acceptance test of the other team is failed, talk to them to understand if their usage of the shared component is correct or yours. If both usages are fine then see if common part of that usage is pushed up (refactored) to a common base component and variance in usage is managed using child classes or may be via composition.

Make one person responsible for the component while it is in active development. He should act as a shared resource between the teams.


答:
Yes, in the general case you need logging.

Logging is not about debugging. Well, OK, a part of logging is sometimes about debugging, and you can skip that part if you don't need it during development.

But the more important part of logging is about maintainability. Well-designed logging might answer the following questions:

Is the application still alive and kicking? (By logging a heartbeat every 1000 seconds.)

Did the performance of the application change over the last 10 months? (By logging statistics of the performance of use cases.)

Did the load of the appliation change over the last 10 months? (By logging the number of requests per request types.)

Did any of our interfaces change their performance characteristics?

Does the new release cause a different usage characteristic towards some of the subsystems?

Are we under a DoS attack?

What kinds of errors are happening?

All this can be achieved by logging. And yes, it should be planned and designed and tested, preferable automatic.

Logging is a feature that deservers treatment, just as other features.

Title: Do we need Logging when doing TDD?


问:
Viability of a Development Team with no *dedicated* Tester role
I've been thinking a lot lately about how to build a lean development team.  Ultimately, I'd like to open up my own little software house with a small number of like-minded people.  The goal won't be to become rich, but rather to have a healthy work environment.

So far, I'm defining a lean team as the following:

Small;

Self-organizing;

All members must have QA in mind;

Members must be capable of performing multiple roles

The last point is where I'm worried a bit because, as the mantra goes...

Developers make bad testers.

While I understand that, often, developers are "too close" to their code or their colleague's code to make higher-level assessments of quality, I'm not convinced they are de-facto bad testers.  On the contrary, I'm of the opinion that the qualities of a good developer overlap greatly with the qualities of a good tester.

Assuming this is correct, I've been thinking of different ways of getting around the dev/tester problem and I believe I have come up with a viable model.

My model requires:

A small software house with 2+ projects

An Agile (iterative) approach to development and delivery

1 team per project

All team members will be Software Developers

Their job description will clearly state Development, Quality Assurance, Testing, and Delivery as responsibilities

If all these preconditions have been met, then the projects can be organized in the following fashion (this example will refer to two projects, A and B):

Every team member will alternate between Developer role and Tester role

If a team member is a Developer on project A, they will be a Tester on project B

Members will work on only 1 project at a time, and therefore are expected to be acting as either a Dev or a Tester.

A Role Cycle consists of 3 iterations as a Dev and 2 iteration as a Tester (again, on two different projects)

Project teams would have 3 Devs and 2 Testers at all times.

Member Role Cycles should be out of phase by 1 iteration.

This minimizes the abruptness of the team changes.  For each iteration, 2 Devs and 1 Tester will remain the same as the previous iteration.

Given the above, I see the following Pros and Cons:

Pros

Distributes project knowledge throughout the company.

Ensures team members aren't testing the code they helped write.

Out-of-phase role cycles means no project ever has a 100% member switch.

Alternating roles breaks the monotony of boring projects.

Cons

The iterations of both projects are tightly coupled.  If one project were to cancel an iteration halfway through and start again, then the two projects would be out of sync.  This would make the role-cycle difficult to manage.

Hinges on hiring Developers open working as Testers as well.

I've received mixed reviews when discussing this approach with friends and colleagues.  Some believe that few developers would ever want to alternate roles like this, while others tell me they personally would love to try it.

So my question is:  Could such a model work in practice?  If not, could it be tweaked into a working model?

Note:  For the sake of brevity I have only focused on the Dev and Tester roles.  I'll expand on other roles if needed.

答:
I think it could work, but there are a couple of things I would make sure you do.

Be very upfront about the dual roles to candidates. It's not for everyone for many reasons. If you have too many people who don't like it, you'll have failure and turnover.

Have a plan where you can evaluate the best way to incorporate this with the team. Although I like to focus on one task/project at a time, I'm not sure I would want to not be programming for a very long period of time. Maybe testing is a nice vacation away from programming. Not that it is easier, just using some different brain cells for a change. Be prepared to try it in different ways before you decide on what is best.

Synchronizing projects doesn't seem like a practical solution. If someone is a testor on a project, they may be the best candidate to replace a programmer and vice versa.

This plan doesn't let the teams self-organize enough. One strategy probably won't fit all teams and all projects.


问:
How to explain the difference between a variable and a key in a key-value pair?
Variables and the keys in key-value pairs are both identifiers for values, so, on a conceptual level, these seem very similar ideas.

How would you explain the difference between these two concepts?

Is it that key-value pairs are used in collections?

Is it that variables point to a memory location?

Or something else?

(If I look at code, I'm instinctively aware of the difference, the question's more about how to explain it which doesn't seem too obvious given that they do similar things.)

答: I wouldn't say that's exactly the case. I personally think of key-value pairs as tuples (a, b) : A * B in the mathematical sense as an element of a cartesian product A x B. Variables don't even come into the picture. The key is just the first component of the tuple. Just because a key can be used as a convenient handle to the entire tuple is incidental and you're confounding these two cases. You could just as easily flip the whole thing around and use the value to refer to the entire tuple.


答:
There is no real difference.

We can think of a variable as a name that maps to some value. All visible variables together make up the environment. This observation is important in some theoretical discussions, and is also used in some language implementations (mostly dynamic languages like Ruby). Especially objects can be represented in different ways – some may be choose static structs where an object member is a field in that struct (e.g. C++), others may use dynamic dictionaries which contain all members as key-value pairs (e.g. JavaScript).

The nice thing is that these are mostly equivalent to each other: you can see one as a generalization, the other as a performance optimization.

In sane languages, the environment is known at compile time, whereas the keys in some dictionary are generally only known at run time. This is the only useful difference: when the key of some mapping is known.

Title: How to explain the difference between a variable and a key in a key-value pair?

Body:
Variables and the keys in key-value pairs are both identifiers for values, so, on a conceptual level, these seem very similar ideas.

How would you explain the difference between these two concepts?

Is it that key-value pairs are used in collections?

Is it that variables point to a memory location?

Or something else?

(If I look at code, I'm instinctively aware of the difference, the question's more about how to explain it which doesn't seem too obvious given that they do similar things.)


答:
I don't agree with

Developers make bad testers

Most of the teams I've worked on in my career have not had any QA support. This includes a couple of large, global corporations involving products like their global login and registration system. In another case, I had 30% of the company's revenue running through a box on my desk. (These practices are not advised BTW ;) ) These companies were reliant on the engineers to make sure their code worked correctly. And us, being detail-oriented, a bit compulsive, and proud of our work, would go to great lengths to make sure our software worked correctly.

Also, as a developer I do much more testing than the Testers. I routinely unit test my code to 90%, or 100% if I'm not working with Java.

I do like working with Testers because they do come at it from a different perspective, and catch things I didn't think of. But I really don't count on them to provide more than about 30-50% test coverage, while I hold myself responsible for 100%. (BTW That's not a slam on them -- they are usually spread thin across projects.)

Rather than ask the engineers in interviews if they want to do QA, ask: if a bug shows up in production, who's responsible? If I introduce a bug, and a customer experiences it, I feel bad and take responsibility. I don't blame the QA guys. IMHO That's the kind of engineer that you want to hire (and work with)

My method of ensuring quality is a) super aggressive unit testing (although I can't quite bring myself to do full Test-Driven Devlopment), b) a strong code review process really helps, and c) integrating an intolerace of and impatience with defects into the team culture.

It's always struck me that the most senior guys were the ones who were most dilligent and attentive to even minor issues, because they could point to a larger problem. But mainly they were the ones most willing to spend the time to get it right.

But most of the teams I've worked on, especially for small companies, have not had a formal QA component.

Title: Viability of a Development Team with no *dedicated* Tester role

Body:
I've been thinking a lot lately about how to build a lean development team.  Ultimately, I'd like to open up my own little software house with a small number of like-minded people.  The goal won't be to become rich, but rather to have a healthy work environment.

So far, I'm defining a lean team as the following:

Small;

Self-organizing;

All members must have QA in mind;

Members must be capable of performing multiple roles

The last point is where I'm worried a bit because, as the mantra goes...

Developers make bad testers.

While I understand that, often, developers are "too close" to their code or their colleague's code to make higher-level assessments of quality, I'm not convinced they are de-facto bad testers.  On the contrary, I'm of the opinion that the qualities of a good developer overlap greatly with the qualities of a good tester.

Assuming this is correct, I've been thinking of different ways of getting around the dev/tester problem and I believe I have come up with a viable model.

My model requires:

A small software house with 2+ projects

An Agile (iterative) approach to development and delivery

1 team per project

All team members will be Software Developers

Their job description will clearly state Development, Quality Assurance, Testing, and Delivery as responsibilities

If all these preconditions have been met, then the projects can be organized in the following fashion (this example will refer to two projects, A and B):

Every team member will alternate between Developer role and Tester role

If a team member is a Developer on project A, they will be a Tester on project B

Members will work on only 1 project at a time, and therefore are expected to be acting as either a Dev or a Tester.

A Role Cycle consists of 3 iterations as a Dev and 2 iteration as a Tester (again, on two different projects)

Project teams would have 3 Devs and 2 Testers at all times.

Member Role Cycles should be out of phase by 1 iteration.

This minimizes the abruptness of the team changes.  For each iteration, 2 Devs and 1 Tester will remain the same as the previous iteration.

Given the above, I see the following Pros and Cons:

Pros

Distributes project knowledge throughout the company.

Ensures team members aren't testing the code they helped write.

Out-of-phase role cycles means no project ever has a 100% member switch.

Alternating roles breaks the monotony of boring projects.

Cons

The iterations of both projects are tightly coupled.  If one project were to cancel an iteration halfway through and start again, then the two projects would be out of sync.  This would make the role-cycle difficult to manage.

Hinges on hiring Developers open working as Testers as well.

I've received mixed reviews when discussing this approach with friends and colleagues.  Some believe that few developers would ever want to alternate roles like this, while others tell me they personally would love to try it.

So my question is:  Could such a model work in practice?  If not, could it be tweaked into a working model?

Note:  For the sake of brevity I have only focused on the Dev and Tester roles.  I'll expand on other roles if needed.


答:
The difference depends on the context of the language. While there is a difference for compiled languages (variable names have meaning at compile-time, whereas keys have meaning at run-time), in Python there is no difference, as variable names are actually stored in a dict:</p>

```<code>>>> foo
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'foo' is not defined
>>> globals()['foo'] = 'bar'
>>> foo
'bar'
>>> type(globals())
<class 'dict'>
</code>```

Body:
The difference depends on the context of the language. While there is a difference for compiled languages (variable names have meaning at compile-time, whereas keys have meaning at run-time), in Python there is no difference, as variable names are actually stored in a dict:

>>> foo
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'foo' is not defined
>>> globals()['foo'] = 'bar'
>>> foo
'bar'
>>> type(globals())
<class 'dict'>


问:
System Programming & Object Oriented Design - what to look out for
Next semester (starts in just under 5 weeks), I'm taking System Programming and OOD concurrently.  Apparently SP centers on working in C on Linux, and I only have Java and Python experience, so I am a little nervous about this.

For those of you who've taken these at an university, what should I look out for?

I've already taken Computer Organization which included an overview of Assembly.  With the exception of implementing loops in assembly, I ranged from B+ to A- in the assembly projects.  Building circuitry was another story, however.

答:
If you haven't ever done any C or assembler, be prepared to understand Java and Python at a whole new level. Even though you already have some comp sci survey class that told you how memory and references work you almost certainly do not fully grok it. </p>

<p>Go into to it with an open mind, and an open heart. C is a beautiful thing, but not at all in the same way that Python is beautiful. It is beautiful more in the same way that a trench knife is beautiful. </p>

Body:
If you haven't ever done any C or assembler, be prepared to understand Java and Python at a whole new level. Even though you already have some comp sci survey class that told you how memory and references work you almost certainly do not fully grok it.

Go into to it with an open mind, and an open heart. C is a beautiful thing, but not at all in the same way that Python is beautiful. It is beautiful more in the same way that a trench knife is beautiful.


问:
Event Driven Programming?
I have been looking into event driven programming in C and the language doesn't seem to have support for it. This has led me to wonder about the nature of event driven programming. Is it correct to say that an event handler basically spawns a thread that waits for a response from I/O and sets a variable? Or is there something more complicated to the process. </p>

答:
Event driven architecture simply means, that the main loop AKA event loop does not understand much about the data a program processes. It understands only enough, that it can dispatch the right events for the data.

So for example, if event is triggered by data coming from a socket, all the event loop needs to know is, what callback to call, when data comes from that particular socket.

Typically the event loop is in some library, such as libevent. So application code consists of just setup code and event handlers which are called by the event loop in the library.

Since you mention threads: a classic simple event driven application is single threaded. Each event handler or callback just does its thing as fast as it can, when it gets called, and returns. Blocking or waiting or sleeping is a big no no, it will block all other events!

Threads are often used, when there is some processing which takes long time. If it were done in the main thread with event loop, the long operation would have to be chopped into small pieces, so other events could be handled too. So it's simpler to have a worker thread, which can do the processing without interfering with event processing, and then generate event for main event loop when done.

Other case of using threads is, when there is something which must block. It may for example be some other library, which only provides a blocking API. If that API was to be used from the main thread event loop, it would block other events. Solution is to use that library from another thread, which can block, and then generate event for the main event loop when there's something interesting for other parts of the application.

But in a sense, event driven architecture is sort of opposite of multi-threaded architecture:

In straightforward multi-threaded architecture, each thread waits for one kind of thing to process (a wait condition or a blocking queue or a blocking socket read or whatever, possibly with timeout to periodically check something), then does the processing, possibly signals other threads, and goes back to waiting. One thread does one thing, and there's a thread for each different kind of thing to do.

In event driven architecture there's just one main event loop (usually from a library) which waits for all kinds of events and calls handlers, all in the same thread. One thread does everything, and there's a non-blocking event handler for each different things to do. The good thing about this is, no synchronization is needed, because it's all in one thread, which avoids all-too-common multi-threading bugs, keeps code simpler and avoid synchronization overhead. Downside is not taking advantage of multiple CPU cores, and risk of blocking event loop if developer is careless.

Of course a complex application can have several event loops in different threads, and also threads which do just single thing, so these can be mixed.

Title: Event Driven Programming?


答:
First a caveat - I've worked professionally as both a QA engineer and software engineer.

Could such a model work in practice?

Anything is possible.

While I understand that, often, developers are "too close" to their code or their colleague's code to make higher-level assessments of quality, I'm not convinced they are de-facto bad testers. On the contrary, I'm of the opinion that the qualities of a good developer overlap greatly with the qualities of a good tester.

It depends on what sort of testing you need. If you need mind-numbing, monotonous, repetitive manual testing to make sure that every single screen or element really has been translated to Elbonian... you're going to have problems.

And in my experience, every project requires at least a little bit of this sort of testing (even if not every project did that sort of testing). The problem is that you don't get this sort of testing from people unfamiliar with QA best practices. Hell, you don't even get it from people who know best practices, but "trust" the developers.

As a tester, you can't trust the developers. I've lost count of the bugs I've found that "could not have possibly been caused by that change" or "works perfectly well on my dev box".

Even if you can find developers who can tolerate not doing what they love to do 2 weeks out of 5 you also will run into this core contradiction. Worse yet, you're spending time and energy to train people to be good at two jobs rather than one. Companies have a hard enough time finding and training people good at a single job, let alone two.

Maybe you're awesome in some way I've not yet encountered - but I doubt it.


问:
How did separation of code and data become a practice?
Please read the question carefully: it asks how, not why.

I recently came across this answer, which suggests using a database to store immutable data:

It sounds like many of the magic numbers you describe - particularly if they are part dependent - are really data, not code. [...]  It may mean an SQL type database, or it may simply mean a formatted text file.

It would seem to me that if you have data that is part of what your program does, then the thing to do is to put it in the program. For example, if your program's function is to count vowels, what's wrong with having vowels = "aeiou" in it? After all, most languages have data structures designed for precisely this use. Why would you bother to separate data by putting it in a "formatted text file", as suggested above? Why not just make that text file formatted in your programming language of choice? Now is it a database? Or is it code?

I'm sure some will think this is a dumb question, but I ask it in all seriousness. I feel like "separate code and data" is emerging culturally as some sort of self-evident truth, along with other obvious things like "don't give your variables misleading names" and "don't avoid using whitespace just because your language considers it insignificant".

Take for example, this article: The Problem with Separating Data from Puppet Code. The Problem? What problem? If Puppet is a language for describing my infrastructure, why can't it also describe that the nameserver is 8.8.8.8? It seems to me that the problem isn't that code and data are mingled,1 but that Puppet lacks sufficiently rich data structures and ways to interface to other things.

I find this shift disturbing. Object oriented programming said "we want arbitrarily rich data structures", and so endowed data structures with powers of code. You get encapsulation and abstraction as a result. Even SQL databases have stored procedures. When you sequester data into YAML or text files or dumb databases as if you are removing a tumor from the code, you lose all of that.

Can anyone explain how this practice of separating data from code came to be, and where it's going? Can anyone cite publications by luminaries, or provide some relevant data that demonstrates "separate code from data" as an emerging commandment, and illustrates its origin?

1: if one can even make such distinctions. I'm looking at you, Lisp programmers.

答:
I suggest reading this classic article by Oren Eini (a.k.a Ayende Rahien)

http://ayende.com/blog/3545/enabling-change-by-hard-coding-everything-the-smart-way

My own takeaway from it is to focus on simplicity and readability. This can mean that things that are unlikely to be reconfigured are best left hard-coded (readably). This allows you to use a programming language's full syntax to express the parameters, as well as gain beneficial side effects like code completion and compiler errors on misuse.

This way you potentially avoid the complexities of parsing/interpreting ("but someone else parses my YAML/JSON" - mapping parsed text into the specific API calls can be a form of interpreting), and avoid the complexity of another step between the "data" and its use.

Some cases do lend themselves to being expressed in data even in a scenario like this : for example, specifying thousands of points in 3D space may be better suited for a text file than code, although in some languages, including C using struct initializers, code can be appropriate even for that.


问:
What is the appropriate Data URI to use for 0-byte content?
I have a method that generates a data URI from a byte array. In the event the input byte array is empty, what is the appropriate thing for the method to do? Here are the options I see:

return data:,

return an empty String

throw an Exception

By my read, RFC 2397 doesn't explicitly address the notion of empty data.

答:
RFC 2397 explicitly allows empty data. Let's look at the relevant parts of the grammar:

```<code>dataurl    := "data:" [ mediatype ] [ ";base64" ] "," data
...
data       := *urlchar
</code>```

The * quantifier means zero or more repetitions, the square brackets contain optional parts. Therefore the shortest data-URL is indeed data:,. In your context, you'll probably use data:text/plain;base64, or something like that instead.

Body:
RFC 2397 explicitly allows empty data. Let's look at the relevant parts of the grammar:

dataurl    := "data:" [ mediatype ] [ ";base64" ] "," data
...
data       := *urlchar

The * quantifier means zero or more repetitions, the square brackets contain optional parts. Therefore the shortest data-URL is indeed data:,. In your context, you'll probably use data:text/plain;base64, or something like that instead.


问:
Pythonic version of Java interfaces
I fully acknowledge that Python and Java are different programming languages and should be used differently.  That said, "Program to an interface, not to an implementation" is good language-agnostic programming advice.

Say I have some DAO interface in Java:

```<code>public interface DataDao {

  Object load();
  void update();
  void delete();
}
</code>```

Programming to that interface allows me to persist data in files while I'm prototyping, swap that out for a database as I get further along, etc. rather painlessly as long as I honor the contract of the DataDao.

What's the Pythonic approach/version (if any) to programming to a contract to keep your classes orthogonal, modular, and enable frictionless implementation changes?

答:
You can use abstract base classes, if you really want to. Another option would be to use the mock library while developing and especially while testing.

An abstract base class is like a mix of a Java interface with a Java abstract class: it cannot be instantiated directly, subclasses need to implement all of its methods, but the parent methods may have an implementation that is callable via super.

Here is an example for an abstract DAO, with concrete file-backed and database-backed subclasses:

```<code>import abc

class DataAccess(object):
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def load(self):
        pass

    @abc.abstractmethod
    def update(self):
        pass

    @abc.abstractmethod
    def delete(self):
        pass

class FileDataAccess(DataAccess):

    def load(self):
        with open(self.filename, 'r') as infile:
            contents = infile.read()
        return contents

    def update(self):
        pass

    def delete(self):
        pass

class DbDataAccess(DataAccess):

    def load(self):
        pass

    def update(self):
        pass

    def delete(self):
        pass
</code>```

Title: Pythonic version of Java interfaces

Body:
You can use abstract base classes, if you really want to. Another option would be to use the mock library while developing and especially while testing.

An abstract base class is like a mix of a Java interface with a Java abstract class: it cannot be instantiated directly, subclasses need to implement all of its methods, but the parent methods may have an implementation that is callable via super.

Here is an example for an abstract DAO, with concrete file-backed and database-backed subclasses:

import abc

class DataAccess(object):
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def load(self):
        pass

    @abc.abstractmethod
    def update(self):
        pass

    @abc.abstractmethod
    def delete(self):
        pass

class FileDataAccess(DataAccess):

    def load(self):
        with open(self.filename, 'r') as infile:
            contents = infile.read()
        return contents

    def update(self):
        pass

    def delete(self):
        pass

class DbDataAccess(DataAccess):

    def load(self):
        pass

    def update(self):
        pass

    def delete(self):
        pass


问:
2D movement of an object based on speed and new vector
I am writing a small game where a sea vessel (like a battleship) can be moved in a 2D place based on orders that it receives. I wanted to know if someone can help direct me to a post or example code that will help me move a ship from one location to another based on the speed and new vector.

So, if I had a ship that is moving in a south westerly direction (say 120 degrees) and it receives new orders to turn to a new vector of 020 degrees, how can I calculate the turn. I don't want it to just turn on a dime and head in the new direction. I want it to make the wide turn (as if it were in water) and then when it reaches the new vector, start heading in that direction.

Any help would be appreciated.

答:
Keep the heading in an angular vector and increment the angle over time until it reaches the new angle. An angular vector is simply a vector kept in angular form. So you keep a field that contains the angle of the ship. Over time (at regular intervals, with changes as a function of elapsed time), modify this angle until it reaches the new heading.

Do not store the angular vector as a regular vector using x and y components, as it will degrade due to floating-point inaccuracies over time.


答:
Data scales much better, and can be queried and modified with much more ease, when it is separated from the code. Even if your data is codish in nature - for example, your data represents rules or commands - if you can store represent that code as structured data, you can enjoy the benefits of storing in separately:

permissions

If the data is hard-coded, you'll need to edit source file in order to edit that data. That means that either:

Only developers can edit data. This is bad - data entry is not something that requires developer's skills and knowledge.

Non-developers can edit source file. This is bad - they might screw the source file without even knowing it!

The data is hard-coded into separate source files, and non-developers have access only to those files. But this doesn't really count - now the data is separated from the code and stored in it's own files...

editing

So, regarding who can edit the data, it's best to store it separately. How about how they'll edit the data? If you have lot's of data, typing it by hand is tedious and error-prune. Having some UI for this is much better! Even if you still have to type everything, you won't have to type the boiler-plate of the format, so there's less chance you'll mess up the format and screw the whole file!

If the data is hard coded, creating that UI will mean that an automated tool will edit you hand-written source files. Let that sink in - an automated tool will open your source files, attempt to find where the data should be, and modify that code. Brrr... Microsoft introduced partial classes to C# just to avoid those things...

If the data is separate, your automated tool will just have to edit datafiles. I'd rather believe that computer programs editing datafiles is not that uncommon nowadays...

scaling

Code and data scale very differently. As your code grows, you want to separate it into more classes and methods(or data structures and functions), but your data - no matter how much it grows - you want to keep in one place. Even if you have to separate it to multiple files, you want to bundle those files together somehow, so it'll be easier to access that data from the code.

So, imagine that you have thousands of lines of data inside a source file. The compiler/interpreter has to go through all that data each time it reads the file, and parse it with it's expensive lexer&parser - even if you are not going to access that data in this particular run of the program. Also, when you edit the actual code in that file, you have to go around the data, which cumbersome the whole process. Also, datafiles can be indexed. Hard-coded data? Not so much...

searching

You have tons of data - it's only natural you'll want to search through it.

If you store it in a database - you can use the database query language.

If you store it in an XML file - you can use XPath.

If you store it in JSON/YAML - you can load it in your favorite scripting language's REPL and search it.

Even if you store it in plain old text file, since it has a structure your program can recognize you can use grep/sed/awk to search it.

While it's true that you can also grep/sed/awk through hard coded data in a source file, it doesn't work as well, since your query can match with other, non-related lines, or miss lines that were written differently because the programming language's data representation syntax allows it.

There are tools for searching through code, but they are good for finding declarations, not hard coded data.

That being said...

It's very important to distinguish between data and code. Just because something is written as code doesn't mean it can't be data. And just because something is written with a data representation doesn't mean it isn't, in fact, code.

I had a class when we had very strict rules about "magic numbers" - we couldn't have any numbers in our code. That means we had to do things like:

```<code>#define THE_NUMBER_ZERO 0
//....
for(int i=THE_NUMBER_ZERO;i<cout;++i){
//....
</code>```

which is outright ridiculous! Yes, 0 is technically "data", but it's just as part of the code as the rest of the for loop! So even though we can represent it as data and separate it from the code, that doesn't mean we should. Not because we want to leave data inside the code, but because it's not really a data - not any more than the rest of the code, which is also compiled to ones and zeros...

Title: How did separation of code and data become a practice?

Body:
Please read the question carefully: it asks how, not why.

I recently came across this answer, which suggests using a database to store immutable data:

It sounds like many of the magic numbers you describe - particularly if they are part dependent - are really data, not code. [...]  It may mean an SQL type database, or it may simply mean a formatted text file.

It would seem to me that if you have data that is part of what your program does, then the thing to do is to put it in the program. For example, if your program's function is to count vowels, what's wrong with having vowels = "aeiou" in it? After all, most languages have data structures designed for precisely this use. Why would you bother to separate data by putting it in a "formatted text file", as suggested above? Why not just make that text file formatted in your programming language of choice? Now is it a database? Or is it code?

I'm sure some will think this is a dumb question, but I ask it in all seriousness. I feel like "separate code and data" is emerging culturally as some sort of self-evident truth, along with other obvious things like "don't give your variables misleading names" and "don't avoid using whitespace just because your language considers it insignificant".

Take for example, this article: The Problem with Separating Data from Puppet Code. The Problem? What problem? If Puppet is a language for describing my infrastructure, why can't it also describe that the nameserver is 8.8.8.8? It seems to me that the problem isn't that code and data are mingled,1 but that Puppet lacks sufficiently rich data structures and ways to interface to other things.

I find this shift disturbing. Object oriented programming said "we want arbitrarily rich data structures", and so endowed data structures with powers of code. You get encapsulation and abstraction as a result. Even SQL databases have stored procedures. When you sequester data into YAML or text files or dumb databases as if you are removing a tumor from the code, you lose all of that.

Can anyone explain how this practice of separating data from code came to be, and where it's going? Can anyone cite publications by luminaries, or provide some relevant data that demonstrates "separate code from data" as an emerging commandment, and illustrates its origin?

1: if one can even make such distinctions. I'm looking at you, Lisp programmers.

Body:
Data scales much better, and can be queried and modified with much more ease, when it is separated from the code. Even if your data is codish in nature - for example, your data represents rules or commands - if you can store represent that code as structured data, you can enjoy the benefits of storing in separately:

permissions

If the data is hard-coded, you'll need to edit source file in order to edit that data. That means that either:

Only developers can edit data. This is bad - data entry is not something that requires developer's skills and knowledge.

Non-developers can edit source file. This is bad - they might screw the source file without even knowing it!

The data is hard-coded into separate source files, and non-developers have access only to those files. But this doesn't really count - now the data is separated from the code and stored in it's own files...

editing

So, regarding who can edit the data, it's best to store it separately. How about how they'll edit the data? If you have lot's of data, typing it by hand is tedious and error-prune. Having some UI for this is much better! Even if you still have to type everything, you won't have to type the boiler-plate of the format, so there's less chance you'll mess up the format and screw the whole file!

If the data is hard coded, creating that UI will mean that an automated tool will edit you hand-written source files. Let that sink in - an automated tool will open your source files, attempt to find where the data should be, and modify that code. Brrr... Microsoft introduced partial classes to C# just to avoid those things...

If the data is separate, your automated tool will just have to edit datafiles. I'd rather believe that computer programs editing datafiles is not that uncommon nowadays...

scaling

Code and data scale very differently. As your code grows, you want to separate it into more classes and methods(or data structures and functions), but your data - no matter how much it grows - you want to keep in one place. Even if you have to separate it to multiple files, you want to bundle those files together somehow, so it'll be easier to access that data from the code.

So, imagine that you have thousands of lines of data inside a source file. The compiler/interpreter has to go through all that data each time it reads the file, and parse it with it's expensive lexer&parser - even if you are not going to access that data in this particular run of the program. Also, when you edit the actual code in that file, you have to go around the data, which cumbersome the whole process. Also, datafiles can be indexed. Hard-coded data? Not so much...

searching

You have tons of data - it's only natural you'll want to search through it.

If you store it in a database - you can use the database query language.

If you store it in an XML file - you can use XPath.

If you store it in JSON/YAML - you can load it in your favorite scripting language's REPL and search it.

Even if you store it in plain old text file, since it has a structure your program can recognize you can use grep/sed/awk to search it.

While it's true that you can also grep/sed/awk through hard coded data in a source file, it doesn't work as well, since your query can match with other, non-related lines, or miss lines that were written differently because the programming language's data representation syntax allows it.

There are tools for searching through code, but they are good for finding declarations, not hard coded data.

That being said...

It's very important to distinguish between data and code. Just because something is written as code doesn't mean it can't be data. And just because something is written with a data representation doesn't mean it isn't, in fact, code.

I had a class when we had very strict rules about "magic numbers" - we couldn't have any numbers in our code. That means we had to do things like:

#define THE_NUMBER_ZERO 0
//....
for(int i=THE_NUMBER_ZERO;i<cout;++i){
//....

which is outright ridiculous! Yes, 0 is technically "data", but it's just as part of the code as the rest of the for loop! So even though we can represent it as data and separate it from the code, that doesn't mean we should. Not because we want to leave data inside the code, but because it's not really a data - not any more than the rest of the code, which is also compiled to ones and zeros...

ContentLicense: CC BY-SA 4.0


问:
Creation of an Assembler
I've always been interested in how programming languages are created, so for the past 8 months I've been researching, practicing and more about the creation of compilers. I've been able to quite easily write front ends for languages and withing the past couple months have been reading more about the back end pieces of a compiler. On top of this I've been reading about and practicing things with the x86 instruction set.

It has been my dream to write a compiler and I would like to make my first big step of understanding back ends, this being attempting a simple x86 assembler. My goal for this is to learn more about code generation, x86 instruction set, executables and figure out the best algorithms and techniques (don't answer anything about the front end, I have this down lol). Eventually to be satisfied in my progression I would like it to output an executable for my platform (Ubuntu Linux amd64, ELF executable) written in the assembly language.

This is a daunting task, though I feel I'm ready with some blood, sweat, and tears. So, my question is: what and in what way does my assembler need to do things? I know it needs to process the actual source, the what? From all I've gathered after processing the source I need to, write out a ELF header and then output the generated x86 code after this correct? I can't find much in depth information on how the x86 code is stored in the executable or even relocatable object files. I know there's sections of some sort but I can't find out how these are expressed, and what they're for.

Anyone willing to answer any of these questions or point me in the right direction is awesome, I've been working on this a long time.

答:
It's sort of difficult to work out what you already know and what you need to know, but I'll have a go anyway.  Sorry if I patronise you.

Architectures

The Intel Instruction Set Architecture is extremely large and complicated, for various historical reasons.  Fortunately, you only actually need to support a very small subset of the instruction set to be able to target a general purpose compiler at it.  That said it may be easier to target a different architecture, at least at first, because it is still a considerable undertaking.

There exist virtual architectures specifically designed to be good for compilers to target, such as Common Intermediate Language, the Java VM, or LLVM.  The first two run on a virtual machine with well defined object formats, and the third can be translated into machine code for many platforms using different back ends.

Targeting a RISC processor such as ARM or MIPS would be much easier than x86 too, as they have smaller, better designed and more uniform instruction sets.  Because of their ubiquity in mobile and embedded computing, emulators exist for these processors which would let you test the machine code your assembler outputs.  Another idea rather than jumping in at the deep end would be to target an old, obsolete architecture, as these tend to be smaller and well documented.  Again, emulators exist to let you test your code.

Or if you just want a hard thinking challenge rather than sheer volume of work challenge, you could try making a compiler target something esoteric like the One Instruction Set Computer...

Basic assembler operation

Since you've already got high level languages working, I'm going to assume you can parse and so on.  An assembler usually runs in two passes—first it translates most of the code, leaving blanks for the symbols it doesn't know yet.  After the first pass, it should know all the symbols, and it runs over the object file again and fills in the addresses of the symbols.

Typically one object file is generated for each assembler file.  Any symbols defined in other files are therefore listed in a symbol table in the object header, and the linker (often a separate program) uses this information to stitch the objects together into an executable afterwards.

Segments

You asked about sections.  In x86 (and other architectures) the memory is segmented to allow the amount of addressable memory to be much bigger than the actual amount of physical memory.  A program has a number of segments assigned to it by the operating system.  The stack gets its own segment, so that if you push more onto the stack than you've got space for, you don't start overwriting stuff.  Strings and other constants defined in your assembler file typically get put into the .rodata segment.  The .bss segment contains enough space for the uninitialised data in your application.  The actual code goes in the .text segment, and the operating system and hardware usually protects against writing in this segment to prevent various kinds of exploits.  The operating system is in charge of loading these segments out the object file and putting them in different places in virtual and physical memory.

If a program is sitting about in the background, then operating system may choose to remove some of the (lesser used) pages from physical memory and write them to disk, in order to make more space for other programs.  A memory access to those addresses then causes the processor to interrupt, and the operating system loads up the page from disk again and lets the program continue unaware that anything happened.

Other information

The Dragon Book is often considered 'the bible' on compiler development.  osdev.org has some useful information on ELF format.  Since you're interested in this sort of stuff, and since an appreciation of operating system structures is required once you start getting into things like object file formats, Operating Systems: Design and Implementation is like the bible on operating systems.

Oops, got a bit carried away...

Title: Creation of an Assembler