问:
When systems get larger and larger, how do you keep a global understanding of your system?
Today I realized painfully that for some decisions you need a good overall understanding of the system. Otherwise, the risk is too high that your assumption turn out to be wrong.

Imagine that you are a developer for an online web shop. To understand the system, you have to understand many connected subsystems, for example:

How to receive and process product information from various suppliers

How the customer can search and order products on your web shop

How the orders are processed and managed by your customer service

How your SAP system handles the invoicing process

...

The larger the system becomes, the more you have to understand.

If that knowledge is lacking, sub-optimal solutions where developed when specialized teams worked together. Specialized teams, which only understood their part of the system in detail.

To deal with that problem, our company changed the strategy, so that one development team always has to be responsible for all aspects of a feature. Even if it involves the complete process chain. (It is kind of a feature team, but not from teams working on different subsystems.)

What are effective strategies for developers to keep their system and operational process knowledge up to date?

I think good system documentation is a key, but I'm afraid that there is a point where the human mind cannot scale as fast as the system involves. At some point to have to simply, but that simplified assumptions can turn out to be costly mistakes. When you have to implement and maintain the code, you just have to know the exact details.

As a developer, I currently have to face a difficult conflict of interests:

I need to spend more time to understand our system and operational process.

I need to develop and maintain our code.

As time is limited, 2) mostly wins. The result is that I mostly gain deeper knowledge along the way, and some half knowledge from casual conversations.

Do you know how huge companies like Amazon solve that problem? I would assume that no single human is capable of understanding such a complex process, and be able to contribute code at multiple subsystems at the same time.

答:
Understand the components

Let's start with a fundamental assertion that, I hope, is already self-evident:

Smaller teams are more efficient, creative and productive than larger ones.

And a simple, but fundamentally important maxim:

To cook an elephant, break it into smaller pieces.

In programming terms, we do this by encapsulating, abstracting and modularizing, always striving to create coherent, easily understandable, functional and consistent interfaces between components.  Each part of the elephant gets its own conceptual black box; once the internals of each black box are designed, coded, tested and accepted, nobody has to think about the box's insides anymore, unless a new requirement dictates a functional change.

This works at the people level as well.  Because smaller teams are more efficient than larger teams, larger companies (especially matrix organizations) frequently have several smaller workgroups, each of which is responsible for a certain part of the software.

Document the interactions

As you've probably already figured out, the problem is not the black boxes, it is understanding the interactions between the black boxes.  There are a number of techniques for dealing with this, all of which involve more time and resources:

Class documentation

Integration tests

Architectural diagrams

Data flow diagrams

And so forth.  Of course, you have to have people who can read and understand this documentation and apply it at a high level.  It must be available to the software developers, and kept up to date.

We're dreaming, right?

Where I work, we run a pretty lean shop.  But we do our best to create these artifacts.  They are, in fact, required elements of the overall software product.

Divide and conquer

Basically, what you want is for your small teams (and your software baby elephant) to look more or less like this:

And your overall software team (and grown software elephant) to look something like this:

The real challenge is this: how do you maintain your information flow at a high level of quality and productivity?  Within systems design, the way that is done is by carefully designing the communications between nodes, so that the interfaces, actors, objects and messages are well-defined, consistent, clear and understandable.

Remember, we're talking about black boxes here.  We already understand the black boxes; the key is to understand the interactions that take place between the black boxes.  A sensible architecture can mean the difference between well-organized and easily understandable software, and a big ball of mud.

And finally...

Be clear about what you want

If you have unclear or underspecified software requirements, it's not realistic to have a good understanding of the software.  Requirements should be understandable in a way that the stakeholder can clearly understand when a feature is fully completed, and there are specific, identifiable and measurable metrics by which success can be declared.


问:
Why is (f . l) not allowed and equal to (apply f l)?
In scheme when defined

```<code>(define f (lambda (a b c d) d))

(define l (list 'a 'b 'c 'd))
</code>```

Why it does not do argument destructuring? I mean arguments should evaluate first, why destructuring is not part of that?

```<code>(f . l)
</code>```

What are reasons that this expression is not equal with following?

```<code>(apply f l)
</code>```

Edited to make it clearer, according suggestions in comments

答:
The main reason is, (f . l) can only work if l is an identifier (or some other atomic literal), and not if it's a more complicated expression. Consider a function for calculating the sum-of-squares:

```<code>(define (sum-of-squares . nums)
  (apply + (map square nums)))
</code>```

Here, you cannot rewrite the (apply + (map square nums)) into (+ . (map square nums)), because that is equivalent (at read-time, way before evaluation happens) to (+ map square nums). And map, square, and nums are not numbers. ;-)

Therefore, implementations have to supply an apply procedure. And once you do, there's no real point to supporting a dotted procedure-application expression that only works some of the time, when apply works correctly all the time.

(Many thanks to Brian Mastenbrook for explaining this to me years ago, back when I had a similar question myself.)

ContentLicense: CC BY-SA 3.0

Body:
The main reason is, (f . l) can only work if l is an identifier (or some other atomic literal), and not if it's a more complicated expression. Consider a function for calculating the sum-of-squares:

(define (sum-of-squares . nums)
  (apply + (map square nums)))

Here, you cannot rewrite the (apply + (map square nums)) into (+ . (map square nums)), because that is equivalent (at read-time, way before evaluation happens) to (+ map square nums). And map, square, and nums are not numbers. ;-)

Therefore, implementations have to supply an apply procedure. And once you do, there's no real point to supporting a dotted procedure-application expression that only works some of the time, when apply works correctly all the time.

(Many thanks to Brian Mastenbrook for explaining this to me years ago, back when I had a similar question myself.)


问:
Is using Git Stash as a workflow an antipattern?
I've recently been looking at how me and my team uses Git and how our workflows work. We currently use a feature-branch workflow which seems to work well.

I've also seen some individuals on our team use workflow based on git stash. The workflow goes something like this:

Work on a main branch (like master)

Make commits as you go

If you need to get changes or switch branches, push your uncommitted changes onto the stash

Once your updating is done, pop the changes off the stash.

I should mention that this workflow is used instead of a feature branch workflow. Instead of taking a branch and working on it, developers here only ever work on a single branch and push/pop off the stack as they see fit.

I actually don't think this is a great workflow, and branching would be more appropriate than using git stash in this way. I can see the value of git stash as an emergency operation, but not for using it in a daily, regular workflow.

Would using git stash regularly be considered an anti-pattern? If so, what are some specific problems that could arise? If not, what are the benefits?

答:
git stash is a tool.  It in itself is not a pattern, nor an anti-pattern.  It is a tool, much like a hammer is a tool.  Using a hammer to drive nails is a pattern and using a hammer to drive screws is an anti pattern.  Likewise, there are workflows and environments where git stash is the correct tool to use, and workflows and environments where it is wrong.

The 'everyone commit and push to the mainline' workflow is one that works quite reasonably where there are no high risk changes.  Its often seen used in svn environments where there is one authoritative central server that has the code.

Git, however, ties to do away with the one central server.  Having all the developers doing commit, pull (or rebase if you're into that), push all the time can make a big mess.

The biggest problems come up with you've got something in progress thats broken and you need to work on a priority bug.  This means you need to set that work aside for a bit, grab the latest, work on that without having the previous work in progress cause issues with the build that you're trying to do.

For this, git stash would be the proper tool to use.

There is, however, a bigger problem lurking at the heart of this workflow.  Is is that all the roles of version control branches are on a single branch.  Mainline, development, maintenance, accumulation, and packaging are all on Master.  This is a problem.  (See Advanced SCM Branching Strategies for more on this approach to branches)

And yes, you did call out that its not a great workflow and that there are problems with it.  However, the problem isn't with the tool git stash.  The problem is the lack of distinct branching for roles or for incompatible policies.

git stash however, is something that I've used when I've had a situation where I've built for a bit, got in a sticky state that I wasn't sure if it was the right one... so I stashed my changes and then explored another approach to solving the issue.  If that worked - great, discard the stuff on the stash and continue on.  If the other exploration got stickier then reset back to the previous change and reapply the stash to work on that.  The alternative would be to commit, checkout, branch and then either continue on this new branch or go back and reset it.  The question is is it really worth putting that into the history when its just something I want to explore for a bit?

git stash isn't an anti pattern.  Using git stash as an alternative to branching while everyone commits to the Master is an anti pattern - but not because of git stash.

If you haven't hit it yet, just wait until you have issues with builds, when someone needs to make significant architectural changes to lots of files (and the merge conflicts) or some untested work in progress code that leaks out to production for this anti-pattern to catch up with you.


问:
Reducing dependency cycles and reducing coupling
I'm trying to learn how to produce quality object-oriented code and have been studying concepts like SOLID. I'm currently working on an entity-component-process system for a small game engine.

Currently, I have it implemented as such:

There is a ScreenStack which contains a stack of Screens

Each Screen has an EntityManager and ProcessManager.

The ProcessManager updates all Processes given entities from the EntityManager.

A Process handles all game logic, so it needs to be able to use ScreenStack to possibly push and pop screens. It can also create, remove, and change entities, so it needs the EntityManager from Screen. Basically a Process needs to know everything about the game since it affects so much of it, but it feels wrong.

How do I go about implementing this better? It seems like everything has a clear dependency hierarchy until you get to a process, where it gets thrown out the window.

There also seems to be tight coupling when I would want to push a new screen. Say, for example, I have a process in a MainMenu screen that checks for menu choice. If "New Game" is clicked, I need to push a new screen, which gets created at that moment. I've read that I shouldn't randomly throw in new which this seems to be doing.

答:
A process doesn't need to know everything about the game, but it might need to know about what the game does.  This is the basic difference between classes and interfaces.  (Part of the "D" in solid.)

Imagine all of your objects as separate and completely unrelated. Then, imagine a single connection from each of those to a service that provides interface instances.  Each link between your classes can be broken and replaced with a set of behaviors (called an interface). Call something like GetInstance(...) to get an existing interface or create a new instance of one.  The consuming object doesn't care which, where, or when the instance was created -- but it does know how to use it. (If you have looked into Dependency Injection, this might be starting to sound familiar.)

Back to reality now.  While separating objects' dependencies by using Interfaces is a generally good idea, you don't need to rework everything you already have.  Just refactor what is difficult or not working properly, and keep moving forward.


问:
Are mutliple database calls really significant with a network call for a web API?
At one of my employers, we worked on a REST (but it also applies to SOAP) API. The client, which is the application UI, would make calls over the web (LAN in typical production deployments) to the API. The API would make calls to the database.

One theme that recurs in our discussions is performance: some people on the team believe that you should not have multiple database calls (usually reads) from a single API call because of performance; you should optimize them so that each API call has only (exactly) one database call.

But is that really important? Consider that the UI has to make a network call to the API; that's pretty big (order of magnitude of milliseconds). Databases are optimized to keep things in memory and execute reads very, very quickly (eg. SQL Server loads and keeps everything in RAM and consumes almost all your free RAM if it can).

TLDR: Is it really significant to worry about multiple database calls when we are already making a network call over the LAN? If so, why?

To be clear, I'm talking about order of magnitude -- I know that it depends on specifics (machine hardware, choice of API and DB, etc.) If I have a call that takes O(milliseconds), does optimizing for DB calls that take an order of magnitude less, actually matter? Or is there more to the problem than this?

Edit: for posterity, I think it's quite ridiculous to make claims that we need to improve performance by combining database calls under these circumstances -- especially with a lack of profiling. However, it's not my decision whether we do this or not; I want to know what the rationale is behind thinking this is a correct way of optimizing web API calls.

答:
We have a couple of applications which are very, very chatty.  There is a database call for every.  Single.  Little.  Thing.  Serving reference data again and again and again is a major part of the workload on the system.  All that scheduling of worker threads, acquiring and dropping locks, plan cache checking etc. adds up even if there is not actual disk IO.  Contention is higher because transactions have to hold locks across multiple DB calls and so throughput is much lower than it could be.  Those teams are now looking at having to buy new, very expensive DB servers because of this.

So, although the majority of the elapsed time in your system's current configuration is taken with REST API calls, ignoring performance at the DB level is storing problems for the future.


问:
Data persistence for transactional customer emails
I'm developing a system to handle sending transactional emails to our customers. This is how it works:

An event occurs during the order's life cycle, for example 'shipped'

This event will trigger the creation of an email in the database (email queue)

A separate windows service is polling the db table for new emails to send. When it finds one it calls a Web service with all the required data. It's the Web service's responsibility to handle the actual sending of the email.

My question relates to step 2. When an email triggering event occurs, should I take a snapshot of all the data required by the service (thereby duplicating data and introducing new tables) or should I get the required data from the transactional db tables only at the point where I'm ready to call the Web service.

[Note: I'm not at all concerned with the body of the email, all I'm doing is passing the data to a Web service. The email templates etc are handled by another system. My concern is purely to queue the email notification and pass the required data to the Web service.]

答:
Record the values at the point of triggering and pass those to the emailer.  Then the email's content will be consistent.

To expand on your example - an order enters the state 'shipped' and an email is queued.  Before the email is send the customer cancels.  The order is now in state 'cancelled'.  Now the email is sent.  What will its content be "Dear Customer, your order is now 'cancelled' and will be with you on 5th September"?  That's less forgivable than getting confirmation of an order which has since been changed.


问:
BDD based on 1-liner requirements
In our team, the product owner only gives us a 1-liner summary. The developers then come up with a PoC, which the product owner reviews and the cycle repeats.

The challenge here is 2-fold:

Time: We have to create the PoCs asap, so that they can be reviewed

The Developers are actually defining the look and feel of the product - a lot of UI PoC and some controller development to back it up. But BDD doesn't gel into the "creative" process of product envisioning. Typically, BDD requires you to take a product specification, and then codify it using Jasmine etc.

Can any BDD experts please suggest a way to blend the creative thinking process with BDD?

答:
Forget a moment about the "BDD buzzword", and let's talk about what we called it the last decades: functional specs. If you think the specs you get from your PO are not detailed enough, write the missing parts on your own. This gives you lots of room for creative discussion, before actually starting to code, but will also help you to remember what decisions you have made, for example, a week ago. It can also help you to plan which tests you will have to write. This leaves you also with some room to fix errors or inconsistencies you might detect in your "detailed spec" when you are or actually implementing the related features (maybe with some prototyping first). So the fact you are writing some things down before you start to code has nothing to do with any "waterfall model".

Just be careful to stay pragmatic and don't dive more into the details as your team needs to work effectively. To find the right balance, you will probably need to gather some experience by doing it. And if you think the "BDD kind" of spec writing is the right tool for you and your team, try it out . But be careful, BDD is a somewhat formal way of writing specs, which has a high risk of becoming non-pragmatic. If you are under the impression it will hinder your creativity, don't use it.

Joel Spolsky wrote a great four-part series about software specs, why you need them and how to write them painlessly, here is the link to the first part, you will find there links to the other parts as well. Though more than 10 years old (far older than the term BDD), I heavily recommend to read it.

Title: BDD based on 1-liner requirements


问:
Fire domain events after transaction completes
I am trying to implement a domain event system that only fires its events when the associated unit-of-work commits successfully.

The main reason why I want to do this is because I have other sub-systems that read the database that expect those changes to be in place when handling the event.

I also don't want event handlers to do their work (send out emails, etc.) if the commit fails.

The system is multiple ASP.NET applications (WebForms, MVC, and WebAPI).

```<code>public class Order
{
    private Payment _payment;

    public int ID { get; private set; }

    public decimal Amount { get; private set; }

    public void PayUsing(IPaymentProcessor processor)
    {
        if (_payment != null)
        {
            throw new InvalidOperationException(
                "You can't pay for an order twice");
        }

        // the Process method may also raise domain events
        _payment = processor.Process(Amount);

        // Raise saves the event into a static Queue<T>
        DomainEvents.Raise(new OrderPaidEvent(this));
    }
}

public class OrderFulfillmentService
{
    private readonly IUnitOfWorkFactory _unitOfWorkFactory;
    private readonly IPaymentProcessor _paymentProcessor;

    public OrderFulfillmentService(
        IUnitOfWorkFactory unitOfWorkFactory,
        IPaymentProcessor paymentProcessor)
    {
        _unitOfWorkFactory = unitOfWorkFactory;
        _paymentProcessor = paymentProcessor;
    }

    public void Fulfill(int orderId)
    {
        using (var unitOfWork = _unitOfWorkFactory.Create())
        {
            var order = unitOfWork.OrdersRepository.GetById(orderId);

            order.PayUsing(_paymentProcessor);

            unitOfWork.Commit();

            // I only want the events to be raised if the Commit succeeds
        }
    }
}
</code>```

With the current implementation, it's problematic because right now the domain raising must put it into a queue as it must not fire immediately.  The queue is static which causes problems for multi-user systems.  I do not want to accidentally fire events for another users, which is a definite problem as all users access the same queue.

This is the list of solutions I have investigated, and why they won't work in my scenario

Have a non-static DomainRaiser class that gets passed into the PayUsing method.

Passing a DomainRaiser is very difficult to do from the domain objects, sometimes events can be raised by changing properties and passing this object around is cumbersome.

Have all entities contain an Events enumeration that is read by the UnitOfWork.Commit call as discussed on http://lostechies.com/jimmybogard/2014/05/13/a-better-domain-events-pattern/

Tracking every single entity that has possibly changed is very difficult, I am off-loading this change tracking to the ORM.  This also clutters up the domain objects.

Using HttpContext.Current.Items to store user-specific events

This is currently the best suggestion, however, it's not possible to unit-test, and it locks my domain to using asp.net, which I have plans in the future to release a desktop app.

My question is, how do I queue and dispatch these events up in a multi-user environment reliably while taking into consideration that I only want to fire events if the overall unit-of-work succeeds?

答:
First, I would ensure each entity has reference to DomainRaiser. It would be best if this was set when the entity is created or materialized in repository. Each user/request context would have it's own instance which would then be injected into all entities, that are worked by the context. I don't know what Repository/UnitOfWork implementation are you using, so it might be impossible to do. But I think it might be automated if you can give your entities an interface which can be called during construction.

This would also solve your second problem of only executing the events if UnitOfWork succeeds. If DomainRaiser and UnitOfWork have single instance in one context, they can interact with each other, which would make this simple method call.

Last thing that comes to my mind is that you could separate some events into two parts: prepare and execute. Prepare would be called in same UnitOfWork as the event was raised from. This would allow you to read relevant data for the event, without waiting for commit. Execution would then be called outside with a flag if UnitOfWork committed successfully and would use the prepared data.


问:
Where is the 'this' variable stored?
Let's take this simple c++ program as an example:

```<code>#include <vector>
class A
{
  void fun() { a = this + 1; }
  A* a;
};

main()
{
  std::vector<A> vec;
  vec.resize(100);
}
</code>```

Forgetting for the moment that this is a pointer to some instantiation of A, this question is about the variable this itself, and where its contents (the value of the memory address) is stored.

It can't be a literal because the compiler can't know the location of class A, so its contents must be allocated somewhere on the stack or heap as a variable. In this case the instances of A are allocated on the heap; does this mean the this pointer is also on the heap?

Looking at the resize call of vec, a compiler might do something like this under the hood:

```<code>std::vector<A>::resize(this, 100); // where 'this' is a pointer to vec
</code>```

So my question is, where does the this pointer come from? Ie. where is the contents of this (the 32/64-bit memory address value) stored in memory for it to be passed to this method?

I would presume it can't be a normal member; so my first thought was that it's a mangled global variable of some sort, but then by storing an unknown number of them in a vector I don't see how the compiler could achieve this either. Where is the contentx of the this variable stored in relation to the contents of the A class member to which it refers?

If the standard has something to say about this, I would prefer an answer with reference to the standard.

Note that the reason I want to know is because I am concerned about false sharing of the memory allocated for the this object if I modify member variables. Knowledge of where the this object is stored might affect padding decisions.

答:
Arman, looking over your questions, I'd like to emphasize one aspect I think you're missing. Your instinct that the this pointer has to be stored somewhere is absolutely right. But, your mistake is in limiting the choices to "heap" and "stack". These are certainly common places for data to be stored, but there are others (and there are other besides these!):

CPU registers

"the global section"

thread-local storage

When arguments are passed to a function, usually the first "couple" arguments or so are placed in CPU registers. The details vary depending on many things: the processor architecture (x86 vs ARM vs ARM64 vs ...), the number of arguments, the types and sizes of the arguments (integers, pointers, floats, vector types), and on certain code annotations the function may have (which you're rarely going to have to deal with).

In almost every processor you're likely to program for, the 'this' pointer will be passed as the implicit 0th argument of a member function, which means it will be passed via a CPU register. In x86_64 that is register ECX, and in ARM that would be register r0.

Title: Where is the 'this' variable stored?


问:
Automatic denormalization for a NoSQL database application
We have a use case where we store table-like data, but we know about the schema of the data only at runtime. In our application, a power user defines a schema and normal user can create records and make relations between those records.
The problem that we are having is that when doing complex queries over the related "tables" (one could say, joins) with filtering and sorting, stuff becomes slow, which is logical.

To give an example.
Lets say we have an article table with 1M rows and supplier table with 500 rows. Some suppliers have 50K articles. Now lets say I want to get all the articles from that supplier, but sorted by the article name. This query is slow. Which is not strange as it basically creates some sort of temporary table/result, which it needs to sort, but it cannot do it fast as there is no index on a temporary table/result.

Our current solution uses Neo4j, but I also tried this on orientdb and in both cases queries are slow, because of the same behavior.

Now we have solved this problem for now by creating indexes at runtime based on the schema the user provided, denormalizing articles and suppliers in one index. The problem is that keeping this index up-to-date is very cumbersome. Lets say we want to filter on the articles table on its supplier name. Now the supplier name is in the index, but it has been replicated 50K times in the above example. So if the user changes the name of the supplier, 50K index records need to be updated. Giving us all sorts of performance headaches and timing issues such as what happends if the user in a couple of seconds changes the supplier name twice, we need to queue the operations. Etc etc.

So my actual question, is there a clean, non-cumbersome way to automatically denormalize or index out of the box, for a system where the DB schema can change at run-time?

答:
You're asking for an SQL operation on a NOSQL database which seems rather illogical to me. That's approaching NOSQL with an SQL mindset, which you should let loose.

For the problem you state I would say look into a NOSQL solution that provides full-text indexing like Apache Solr or Sphinx


问:
How can a new programmer impress the software engineer (boss)?
I'm working at my first programming job. My boss is a very smart software engineer, and I feel
like I have very little to offer compared to him. Problem is, he is always busy, and needs someone to help him out. I feel like I'm not good enough, but I still want to succeed. I want to be a great programmer.

What can I do to impress him?

Thank you.

答: If you have the freedom to do so: Write grants, bring in some grant money from outside, or start a cooperation that has business value, with new partners that consider you a competent programmer or -at least- valueable employee.</p>

ContentLicense: CC BY-SA 2.5


问:
Is a complete rewrite against the license/eula of this software?
Okey, so I have for a while now been thinking of rewriting the minecraft server, this time in a language called golang. It's just a example project that I can learn a new programming language through..

My question is: If I -rewrite- the minecraft -server-,:

I read the Minecraft EULA, and they only talking about -modifying- the the current server or client. Nothing about re-creating it. Do I violate the EULA if I recreate the minecraft server in golang?

If I ever will finish the project, having a completley functional rewritten server. Can I release it?

And if I can release it, can I earn money from it in form of donations?

Thanks!

答:
I am not a lawyer, but I would have thought that a port of the existing code constitutes a modification of it so is covered by

Any tools you write for the Game from scratch belong to you. . Modifications to the Game ("Mods") (including pre-run Mods and in-memory Mods) and plugins for the Game also belong to you and you can do whatever you want with them, as long as you don‘t sell them for money / try to make money from them. We have the final say on what constitutes a tool/mod/plugin and what doesn‘t.

You can do this, but you can't try to make money for it.

If you had never seen their code and created a server based on reverse engineering the client/server protocol that probably would be a different thing, but I would consider a port to be a modification rather than an independent effort.


答:
You can do this.

What you want to do is called cloning a software. Considering that your server and the original server aren't even in the same programming language, your server is a completely new piece of work and you have all the rights to it.

At least in the united states, the DMCA grants a safe harbor to reverse engineer software for the purposes of interoperability with other software. So when you find out how the Minecraft protocol works by reverse-engineering and re-implement it in your server, this is perfectly legal.

By the way, I am not a lawyer, but I think the clause "We have the final say on what constitutes a tool/mod/plugin and what doesn‘t" is unlikely to hold up in court.

Title: Is a complete rewrite against the license/eula of this software?

Body:
Okey, so I have for a while now been thinking of rewriting the minecraft server, this time in a language called golang. It's just a example project that I can learn a new programming language through..

My question is: If I -rewrite- the minecraft -server-,:

I read the Minecraft EULA, and they only talking about -modifying- the the current server or client. Nothing about re-creating it. Do I violate the EULA if I recreate the minecraft server in golang?

If I ever will finish the project, having a completley functional rewritten server. Can I release it?

And if I can release it, can I earn money from it in form of donations?

Thanks!


问:
Should putting html tags into localization files be avoided?
We are localizing all parts of our website to many languages. We use XML localization files. I think this scenario is so common, and even there should be a standard solution to this, but still I couldn't find any good advice, and every developer here has different opinion about it, so I'm asking you.

Suggest the following example:

If you have question, please ask our Customer support or write an email to Jane Doe  our specialist.

Or a long, formatted text:

```<code> <p>A very long, marketing blah-blah. A very long, marketing blah-blah. A very long, marketing blah-blah concluding in a list:</p>
 <ul>
   <li>1</li>
   <li>2</li>
   <li>3</li>
</ul>
</code>```

1) Would you put html tags into the localization XML file?

My concern is the View is separated into 2 files: your page and your localization file. People will forget to check the localization file. Also, there's logic and style embedded in html as well (see target="_blank", or the fact that the above mentioned list is unordered...)

2) Or splitting it to smaller parts?

<msg id="IfYouHaveQuestion">If you have question, please ask our</msg>

<msg id="CustomerSupport">Customer support</msg>

<msg id="OrWrite">or write an email to</msg>
...

Now, View can contain all the markup and style. It's easy to change it, flexible.

But.

There's absolutely no guarantee that the word order will be the same in all languages. Also, this would make the translator's work a nightmare, making it a puzzle.

3) Or introduce BB-style markdown?

<msg id="HaveQuestion">If you have question, please ask our [link url="{customersupportlink}" title="{{CustomerSupportTitle}}">Customer support[/link] or write an email ...</msg>

But probably this is over-complicating the issue, and also we have to write our own parser for this (though, I think it wouldn't be so hard). And probably does not solve the long, formatted text problem.

4) ??? (Your golden solution here) :)

答:
It seems there is no mainstream choice, so here is my suggestion :

Localisation files could be used more like semantic data than just text strings.
It seems reasonnable to expect identifiying a list , tagging a paragraph, a name or a part of a phrase being part of the localization team work.
So it could contain semantic (but no logical) html tags and use semantic span tags ( like  ) in localization files.
Note : I here suggest span, wich is a valid html tag and so could be manipulated easily as a DOM element, but nothing stop you to use your own tags to parse.

Doing so you can in your view logic code, when extracting the text from the localization file, identify the seo-name, and adding the html link tags properly.

You may even, since some prior answer have made a security point about leting potentialy unknown people writting html code on your website, have a security parser wich check only limited safe tags (
,
,
,
, , ...) are present in localization files.

An exemple to illustrate :

```<code>If you have question, please ask our <a href="blabla" target="_blank" title="Our nice Customer Support + boilerplate SEO bullshit">Customer support</a> or write an email to <a href="mailto:XXX">Jane Doe </a> our specialist.
</code>```

Could became in the file with thoses convention :

```<code>If you have question, please ask our <span id="customer-support">Customer support</span><span id ="customer-support-description">Our nice Customer Support + boilerplate SEO bullshit</span> or write an email to <span id="specialist-name">Jane Doe </span> our specialist.
</code>```

Wich (I think) ins't really difficult to localize in french for (bad since closely-related) exemple by :

```<code>Si vous avez des question, n'hésitez pas à rencontrer notre <span id="customer-support">Service client</span><span id ="customer-support-description">Notre super service client + habituel blabla commercial</span> ou à contacter par email notre spécialiste <span id="specialist-name">Jane Doe </span>.
</code>```

And a ugly controller pseudocode exemple :

```<code>$customerServiceParagraph = getLocalizedText("customerServiceContact",$lang);
$customerSupportDescription = getTextContentByElementIdThenDelete("customer-support-description"));
linkify($customerServiceParagraph,"customer-support",$customerSupportDescription,"blabla","_blank");
mailto_ify($customerServiceParagraph, "specialist-name","XXX" );
</code>```

I'm far from being a localization expert but think the ordered or unordered list choice is a matter of cultural convention, and so is also a part of localization team work, even if I agree thoses tags are a legacy of dirty old styling/semantic tags collection of prior html versions.

ContentLicense: CC BY-SA 4.0

Body:
It seems there is no mainstream choice, so here is my suggestion :

Localisation files could be used more like semantic data than just text strings.
It seems reasonnable to expect identifiying a list , tagging a paragraph, a name or a part of a phrase being part of the localization team work.
So it could contain semantic (but no logical) html tags and use semantic span tags ( like <span id="seo-name"> ) in localization files.
Note : I here suggest span, wich is a valid html tag and so could be manipulated easily as a DOM element, but nothing stop you to use your own tags to parse.

Doing so you can in your view logic code, when extracting the text from the localization file, identify the seo-name, and adding the html link tags properly.

You may even, since some prior answer have made a security point about leting potentialy unknown people writting html code on your website, have a security parser wich check only limited safe tags (<p>, <ul>, <ol>, <li>, <span id="blabla">, ...) are present in localization files.

An exemple to illustrate :

If you have question, please ask our <a href="blabla" target="_blank" title="Our nice Customer Support + boilerplate SEO bullshit">Customer support</a> or write an email to <a href="mailto:XXX">Jane Doe </a> our specialist.

Could became in the file with thoses convention :

If you have question, please ask our <span id="customer-support">Customer support</span><span id ="customer-support-description">Our nice Customer Support + boilerplate SEO bullshit</span> or write an email to <span id="specialist-name">Jane Doe </span> our specialist.

Wich (I think) ins't really difficult to localize in french for (bad since closely-related) exemple by :

Si vous avez des question, n'hésitez pas à rencontrer notre <span id="customer-support">Service client</span><span id ="customer-support-description">Notre super service client + habituel blabla commercial</span> ou à contacter par email notre spécialiste <span id="specialist-name">Jane Doe </span>.

And a ugly controller pseudocode exemple :

$customerServiceParagraph = getLocalizedText("customerServiceContact",$lang);
$customerSupportDescription = getTextContentByElementIdThenDelete("customer-support-description"));
linkify($customerServiceParagraph,"customer-support",$customerSupportDescription,"blabla","_blank");
mailto_ify($customerServiceParagraph, "specialist-name","XXX" );

I'm far from being a localization expert but think the ordered or unordered list choice is a matter of cultural convention, and so is also a part of localization team work, even if I agree thoses tags are a legacy of dirty old styling/semantic tags collection of prior html versions.


问:
optimise my solution
I just solve this but want know more efficient way to do matrix multiplication

```<code>M :
------
1 1 0
0 0 5
3 2 0
</code>```

f[n] = M^n

I have implemented using Exponentiation_by_squaring

Is there more efficient then this ?

答:
Since N can be as large as 10^12 it should have been clear that iterating from 1 to N is not the desired solution.  The key insight is that the recursion can be rewritten as V(i) = M * V(i-1), where

```<code> V(i) = [RR(i), MM(i), PP(i)] (a column vector)
</code>```

so

```<code> V(0) = [ 3 1 0 ]
</code>```

and

```<code> M = | 1 0 3 |
     | 1 0 2 |
     | 0 5 0 |
</code>```

Now V(N) = M^N * V(0)

We can calculate M^N in log(N) time by repeatedly squaring:

```<code>M^2 = M * M
M^4 = M^2 * M^2
...
</code>```

Perform all calculations mod 100000006 to avoid accumulating large numbers.

To arrive at this solution, it helps to have a basic familiarity with linear algebra.

Title: optimise my solution

Body:
Since N can be as large as 10^12 it should have been clear that iterating from 1 to N is not the desired solution.  The key insight is that the recursion can be rewritten as V(i) = M * V(i-1), where

 V(i) = [RR(i), MM(i), PP(i)] (a column vector)

so

 V(0) = [ 3 1 0 ]

and

 M = | 1 0 3 |
     | 1 0 2 |
     | 0 5 0 |

Now V(N) = M^N * V(0)

We can calculate M^N in log(N) time by repeatedly squaring:

M^2 = M * M
M^4 = M^2 * M^2
...

Perform all calculations mod 100000006 to avoid accumulating large numbers.

To arrive at this solution, it helps to have a basic familiarity with linear algebra.


问:
Communication/Updates between units of works/entity framework contexts, colliding with user changes
I'm developing a WPF application using Entity Framework for my database communication. The application has a hierarchy of tabs where each tab has a db context. Each tab allows the user to view some particular object, make changes, save, update, close or open tabs for related objects.

My crux is that some tabs contain information that depends on other objects as well, an object O of type A may be related to other A objects (o_1,...,o_n) in a parent-children relationship, where I have a graphic indicator showing wether property P in O has the same value as P in all the children of O, for example. Red indicator means they are unequal, green means they are equal.

The "overlap" in information between contexts will only be presentation-wise, the different tabs will not be stepping on each others toes by changing each others data.

Now if I have Tabs for O and o_3 opened, change P in o_3 and save, the indicator in the tab for O might need to change as well, but since that tab has its own (now outdated) db context, the indicator still shows the old value, which might be confusing to the user.

This seems like a very general and common problem considering how many application has this architecture.

How would you solve the problem? I see a few different but flawed/problematic solutions.

A. Make the user aware of the fact that tabs can become "obsolete" when saving others. This feels like a forfeit to technological troubles.

B. Force updates of related tabs when saving. This would either overwrite user changes or I would have to build some system (repository pattern?) that maintains information of user changes and mediates what to update and what to keep, what to tell the user and so on.

C. Indicate outdated:ness of related tabs. This seems like the pragmatic solution, but the user is left to wonder what would be affected if updating the tab manually, and any changes made would have to be dismissed

D. Communicate the change to the other tab without involving the db context. This would sort of work, but could lead to complicated problems by introducing state that is not related to the db context, overall it would increase complexity

答:
I think the real problem here is that you are allowing an implementation detail ( the relationship between a DBContext and interface tabs ) to restrict your thinking about the user experience of your application. There is nothing to tie your database connections methodology to the interface. In fact for practicality and testability you should definitely be decoupling them as far as you possibly can. The simple act of separating your data layer from  your interface and implementing your logic as an intermediate layer would cause this problem to evaporate.

There is also something I don't quite understand about your second option - why would updating other tabs when the user saves overwrite their changes? Surely if you do it whenever a user saves, then it shouldn't overwrite their data at all? If the dependencies are strong enough that changing Item 2 on Tab 3 overwrites Item 1 on Tab 1, you need to go back to first principles and rethink your interface design - surprising users is bad, surprising users by overwriting data they previously input without letting making it obvious this will happen is a major no-no.

With all that said, I would be thinking about two things here- firstly user experience, secondly application structure.

If I am using a piece of software and I update it, I expect the software to reflect my update across the board. ( Tangential but important for architectural considerations, I also expect the software to be able to undo that change. )

From this we can immediately discount options A and C from your list. In case C if I open a tab and it indicates it is obsolete the only thing I can do is refresh it anyway.

I am strongly inclined to always do more work so that my users can do less work.

Thinking about B and D comes down to your application structure. If it is client-server, is your business logic on the server? If the business logic is on the client then you can go for D because you already have all the information you need to update neighbouring data available right there. If you have the business logic on the server then you're going to need to go back to the server to reload data in the other tabs.

Edited to add: You might want to provide some kind of locking facility, just to prevent two users editing the same thing at the same time. With a low user count you don't need to worry too much about having a lot of database or network traffic, so you can do this fairly safely.


问:
How important is the ability to touch-type?
Did you learn to touch-type when you were already working as a programmer?  If so how did it affect your productivity?  Or are you still unable to touch type and do you think it holds you back?

According to Steve Yegge it is essential,

Personally I did not notice much difference, possibly because I was spending less than 25% of my work time actually typing (I was working on a large legacy project at the time and I was spending more time on reading and debugging existing code.)

答:
The more you type the less you have to look at your keyboard, its a natural process.
Thats why some people on machanical keyboards are able to use blank keys, 1 is because it look clean, 2 is more effieicent that force you to not look at the keyboard. These keyboards such as the DAS proffesional, HHKB pro2...

You can use aids such as AHK, texter.
texter is only just a simple GUI based text expander made on top of AHK, simple and easy, but + AHK standalone you get more controls, such as im using the space combo script which allows me to use space bar as a modifer key,

eg when holding my space more than 0.2seconds than use others keys such as

on the home row- --hjkl  = left, down, up, right navigation

going a row up,----yuio  = back-space, undo, redo,

a row down,--------bnm,. = ?('"{

and many many more upon your own liking espeialy for keys like home, end.. or any hard to reach keys, that make your hand out of the home row. such as remap the control key on capslock and another backspace combo for left hand(shift+space)
here is the thread about the space bar combo script

http://www.autohotkey.com/forum/post-406030.html#406030

i might put up the script im using after i finish designing my blog, visit my blog after the end of 2010 it might be there. :)

OwnerDisplayName: Devric


答:
It depends on whether you read the source code of the Minecraft server and translate it "word for word", then your code is basically a modification of the Minecraft code.

If instead you only see what the server does and then invent your own code to do the same thing, you're fine.

This is why in the past sometimes code has been playing by companies using two teams, one team to read the code and describe it in functional terms, the other only using the functional description to write new code that does the same thing. But you can't do that as one person.


问:
Git structure for java project
I know there are multiple posts on similar problems, so please be kind with me.
I am asking for suggestions I have not thought of and for good advice.

I have a java "project" that is structured in a server-client pattern.
There are some classes that are common to both parts but most of it is either server (backend and control logic) or client (GUI). To not write one monolithic application (where the client depends on JDBC jars) I decided to split the project into multiple eclipse projects:

proj_server (where JDBC is needed)

proj_client

proj_common

proj_test

In proj_test there is some code to test parts of the client/server. I setup eclipse such that both server and client depend on the common module.

Now I use git to manage all these eclipse projects.
My first thought was to manage all parts in separate repos as suggested by git.
But I have the problem that I loose the relation of the different commits: Any modification in the proj_common will typically reflect some data type modification. This makes the new version incompatible with old commit in the other projects.

I then thought of several possibilities:

Using of git submodules

Using of git subtree

Using of a single git repository that contains the modules together

Using different repositories and adding regularly tags to the commits to keep track of the relations

The solution 1 has the problem that it is quite heavy to maintain. Especially if you want to check in multiple small modifications you have to go multiple times "around the folders" to add/commit and then pack it all in the main repository up.

The subtree method might work well as far as I have read with a bunch of scripts to manage the git calls. The main disadvantage is that subtree fits perfectly if most work is done in the main repository and updates to the imported repos happen occasionally. If the updates happen merely in the subprojects the history of the main projects gets quite crumpled.
On branching it might get complicated.

The solution number 3 is my favourite solution so far as everything is kept tight and branching is done without any problem.

The solution 4 is mainly what is done now, but no tag have been created yet (pre-alpha, thus no sense in creating tags). As the project grows this might get also very nasty and error-prone.

What would you suggest? One of the given solutions or did I miss the one perfect solution?

Edit:

I have found the following related posts: a, b, c and d.
Unfortunately the big picture is not clear.
In a (second answer!) and d the advice tends towards multiple repositories in different files.
But they do not explicitly state how to manage the relationship of the different commits.
That is you cannot go for the running configuration at Jan 10th 2013 for example without looking at the history explicitly for all subprojects.
In b solution 3 is suggested.
In c a comparision is made. They first had something like multiple manually managed projects but then migrated to solution 3.

In all these Q&As the first two solutions were at most mentioned but not suggested by the answering persons.
In the net you can find much about git submodule that tells you that you get quite much overhead by it (e.g. here or here).

The problem is (as far as I can see) that in practice I will have not only one project with some reference to one or more libraries.
In this case (hopefully) the library would not change regularly and the coding takes place in the "main" repository.
In this case all the advances solutions (subtree, submodules, ...) might be very useful.

In my case on the contrary I have only a dummy master repository that holds the four subprojects in sync.
These subprojects themselves are under high development.
So it is a critical part that the contribution to the different online  (sub)projects is straight forward.
Not much additional magic with git.

In another site they suggest solution 1 but I am unsure what their main development goal is: The main repository or the imported library.

I want to avoid getting into trouble and later have to migrate to another system. Thus I ask for advice of somebody who might have done similar things.

答:
For this kind of work, I suggest you to learn about Maven which deals with dependency compatibility (among a lot of other things).

With Maven, you could for example use one of these two ways:

Either the "standard" way: by creating a Nexus repository which you will feed with a compiled jar of each version of your common package and to fully exploit maven/nexus the version(s) of JDBC you use. Each time you build your project from any commit, the versioned config file will be read and will use the good revision of your library "common".

(I say Nexus, but there are others software doing the same thing, I've also heard of one named Artifactory).

Or, you could make a more complicated script which, when you build your server or client project, go download the source of your library on your "common" repository and build it.

Maven may seems a bit overkill and is (in my opinion) a pain to learn alone, but it's handy, powerful, and is the professional standard in Java and works seamlessly with Eclipse.

As for the Maven usage, the Nexus way is (depending on your scripting skill) a bit heavier to init, but more standard, easy to scale with your project, and can be used for all your other future projects.

If you're working in a professional context, I strongly advise to look for a colleague knowing it to help you mavenize your project.

Title: Git structure for java project


问:
Can I use FontAwesome fonts with my own custom CSS files?
I want to use the WebFont icon library FontAwesome (FA) for all the icons in my project. They are using SIL OFL 1.1 for the fonts and MIT License for the source files. My problem is that, I want to use the FA fonts in my project, however I don't want to be tied to their CSS files. Is it possible for me to use the fonts with my own custom CSS styles, without having any dependence on the FA's CSS files. I don't even mind endorsing the project on my website, but if even do that, I want to only endorse the art not the code.

FontAwesome License: http://fortawesome.github.io/Font-Awesome/license/

答:
Yes, you can use those fonts with your own CSS files.

The licenses used by FontAwesome allow you to create derived works, and that also gives you the right to delete files that you don't need.

Licensing wise, the implications are:

If you delete everything that is licensed under the MIT license (everything in the font-awesome/css, font-awesome/less and font-awesome/scss folders), you can also delete all references to the MIT license itself.

If you make modifications to the files licensed under the SIL OFL (the files under font-awesome/fonts), you may need to change the name of the font as it is presented to the user. I haven't checked if they made the font name reserved.

The fonts may not be sold separately, so if you separate the fonts from the rest of FontAwesome and you sell your software, you should ensure that the fonts are distributed in the same package to reduce the risk of violating that requirement.


答:
I'm going to park your first point if I may. If the deliverable is highly desirable and will produce tangible benefit, there will naturally be a clamour for it to be delivered speedily.

The second point interests me. To my mind, if the business have a requirement for an interface to behave in a certain way, this too should come under BDD's given-when-then process.

However, it sounds to me like they're unsure of what they actually want until they see it, so I'd be inclined to decouple this from rest of the development so it can be produced in isolation. Then once the business is happy with the interface, stitch this into the main deliverable and test as normal.

Title: BDD based on 1-liner requirements

Body:
In our team, the product owner only gives us a 1-liner summary. The developers then come up with a PoC, which the product owner reviews and the cycle repeats.

The challenge here is 2-fold:

Time: We have to create the PoCs asap, so that they can be reviewed

The Developers are actually defining the look and feel of the product - a lot of UI PoC and some controller development to back it up. But BDD doesn't gel into the "creative" process of product envisioning. Typically, BDD requires you to take a product specification, and then codify it using Jasmine etc.

Can any BDD experts please suggest a way to blend the creative thinking process with BDD?


问:
What to learn to become freelancer?
I have around 8 years experience in software development. I have worked on application development all these years using C++ /win32.

I few years from now I want to become a freelancer . I tried to look into some freelance sites and I am not able to find any projects on C++/win32. I feel C++/Win32 wont help me much and I need to learn new skills . But my job require me to still work on C++ , so I cann't learn any new skills. I can still learn some new skills but without good project , it won't make any sense.

I have shortlisted few skills

php

jquery

perl

python

android

I am still confused which one to start and how to get expertise in them.Any suggestions?

答:
Subscribe to the hacker news RSS feed.

You'll get lots of links such as this one.

Graphic Design matters most to clients, "unfair but true".

Title: What to learn to become freelancer?


问:
Static Test-Data in Helper Class: Getter vs Public Constant
Currently I am in process of writing large batches of Unit-Tests in the Project I am working on. Additionally we recently Introduced GUI-Testing. This project is a relatively simple webapp, on basis of Java EE 6.

Now my colleage did write quite some GuiTests, and I did write quite some Unit-Tests.

For both tests we need data to run them. Now we both took different approaches on how we "store" the data we use.

Solution 1:

```<code>public class UnitTestData {
     public static final String DATA_PIECE = "some data";
     public static final String MORE_DATA = "and more data";
     //... and so on
}
</code>```

Solution 2:

```<code>public class GuiTestData {
    public String getDataPiece() {
        return "some data";
    }

    public String getMoreData() {
        return "and more data";
    }
    //... and so on.
}
</code>```

My question now is: Which of these approaches is the objectively better one?

答:
There are two arguments for using accessor methods instead of public variables:

Access Control: A method allows us to publish only a getter method, or to add value-based (in contrast to type-based) constraints on a setter. With public final identifiers, this point is irrelevant, as that value can't be changed, and invariants therefore can't be broken by client code.

Uniform Access: The method of accessing data in an object should not leak information on whether this data is stored  or computed on the fly. In languages without “properties” – such as Java – this means that any data should be accessed via methods, and that no variable should ever be public. But why does the Uniform Access Principle exist in the first place? It's meant to reserve leeway for reimplementing the internals of an object without changing the public interface, so that client code using that object will not break.

But in your situation there is no client code that uses a public API, you are just storing data for tests. Because there is no API to maintain, the justification for the UAP falls away (although following it does still get you all the advantages).

So there are no compelling arguments for using either solution. It's a pretty level field, and you can choose freely.

Note that using methods is absolutely required when each access needs to return a new instance. This is irrelevant for immutable objects such as strings. Otherwise, you should consider accessing all test data through methods for consistency's sake.

My personal opinion would be to choose the simplest thing possible, which would be using public final variables. However, please use member variables and not static ones, so that your tests can pass test data around as a first class object. (I know you can access static data via instances, but I consider that to be an anti-pattern).

If there are people on your team who are not familiar with Java, it might be better to store the test data in a configuration file (I recommend the YAML format). Instead of writing your own class, you would then be using the configuration file parser to access the data.


问:
Is 'design with types first' ultimately the same as 'design with interfaces first'?
In Object Oriented Programming, we're taught to think in terms of Polymorphism (the idea that the implementation is decoupled from  the interface - and that it makes sense to think of the interface first).

This is expressed by Eric Gamma (author of Gang of Four Design Patterns Book)

Program to an interface, not an implementation

Later we learned that this is a way of solving The Expression Problem.

Other languages such as Clojure address 'The Expression Problem' using protocols.

Now coming out of Scala (and perhaps influenced by Haskell) we're seeing a design movement that says "Design your Types First". When I heard that - I thought I'd heard it before.

My question is: Is 'design with types first' ultimately the same as 'design with interfaces first'?

答:
A small case study: a pattern matcher in two pseudo-languages, first OO and then functional

In OOP, designing interface-first

```<code>interface IPatternMatcher {
    bool Match(string toMatch)
}
</code>```

The above is the only interface we need, but it doesn't really give us any clues about how to design our implementing types. You will probably want some composites, so you implement an OrPatternMatcher

```<code>class OrPatternMatcher : IPatternMatcher {
    OrPatternMatcher(list<IPatternMatcher> submatchers);

    bool Match(string toMatch) {
        return _submatchers.Any(s => s.Match(toMatch));
    }
}
</code>```

But now you have started implementing functionality, without having finalised your hierarchy. It is kind of impossible to separate these two stages in OOP because your functions and types are so intertwined

In FP, designing type-first

```<code>type PatternMatcher =
| OrPatternMatcher        : list<PatternMatcher>
| AndPatternMatcher       : list<PatternMatcher>
| SubstringPatternMatcher : string
| RegexPatternMatcher     : regex
</code>```

Here, we have all the necessary types designed up-front, covering all the cases you can think of. The PatternMatcher union type in FP is related to the IPatternMatcher interface OOP, with the crucial difference that the union declares its type structure with no functions, whereas the interface declares its functions with no type structure.

It is now trivial to implement a match function just by looking at the shape of the types you have laid out. If you have designed the types sensibly for your domain, you are already most of the way to having a working component. This is what it means to design your types first - to accurately model your domain without worrying about behaviour

In my opinion OOP interfaces and FP types are almost the opposite of each other, and the benefits gained from designing your FP types up-front are very different to those gained by programming to an interface.

As an additional afterthought, the phrase "design your interfaces first" is not the same as "program to an interface" and so the latter cannot be truly compared to "design your types first".

"program to an interface" advocates the decoupling of components by not assuming implementation details

"design your types first" is advice on modelling your domain sensibly before implementing behaviour

The two really aren't that comparable, as they relate to different situations

Body:
A small case study: a pattern matcher in two pseudo-languages, first OO and then functional

In OOP, designing interface-first

interface IPatternMatcher {
    bool Match(string toMatch)
}

The above is the only interface we need, but it doesn't really give us any clues about how to design our implementing types. You will probably want some composites, so you implement an OrPatternMatcher

class OrPatternMatcher : IPatternMatcher {
    OrPatternMatcher(list<IPatternMatcher> submatchers);

    bool Match(string toMatch) {
        return _submatchers.Any(s => s.Match(toMatch));
    }
}

But now you have started implementing functionality, without having finalised your hierarchy. It is kind of impossible to separate these two stages in OOP because your functions and types are so intertwined

In FP, designing type-first

type PatternMatcher =
| OrPatternMatcher        : list<PatternMatcher>
| AndPatternMatcher       : list<PatternMatcher>
| SubstringPatternMatcher : string
| RegexPatternMatcher     : regex

Here, we have all the necessary types designed up-front, covering all the cases you can think of. The PatternMatcher union type in FP is related to the IPatternMatcher interface OOP, with the crucial difference that the union declares its type structure with no functions, whereas the interface declares its functions with no type structure.

It is now trivial to implement a match function just by looking at the shape of the types you have laid out. If you have designed the types sensibly for your domain, you are already most of the way to having a working component. This is what it means to design your types first - to accurately model your domain without worrying about behaviour

In my opinion OOP interfaces and FP types are almost the opposite of each other, and the benefits gained from designing your FP types up-front are very different to those gained by programming to an interface.

As an additional afterthought, the phrase "design your interfaces first" is not the same as "program to an interface" and so the latter cannot be truly compared to "design your types first".

"program to an interface" advocates the decoupling of components by not assuming implementation details

"design your types first" is advice on modelling your domain sensibly before implementing behaviour

The two really aren't that comparable, as they relate to different situations


答:
In my opinion solution depends how complex your testing environment is.

Solution no1 looks simpler and you are able to use import static to simplify it even more this way you follow kiss principle
Also solution no1 performs better as you do not need to create another stack call on thread that takes place in solution no 2.

However solution no2 seems to work better in more advanced testing scenarios when you have to test sets of data. In this case you would have to externalize an interface e.g. GuiDataSource which GuiTestData would have to implement in order to create more implementations similar to GuiTestData and inject them on runtime during the testing phase.
You can easily also mock up everything in solution no2 and use reflections to do a lot of magic.

Title: Static Test-Data in Helper Class: Getter vs Public Constant

Body:
Currently I am in process of writing large batches of Unit-Tests in the Project I am working on. Additionally we recently Introduced GUI-Testing. This project is a relatively simple webapp, on basis of Java EE 6.

Now my colleage did write quite some GuiTests, and I did write quite some Unit-Tests.

For both tests we need data to run them. Now we both took different approaches on how we "store" the data we use.

Solution 1:

```<code>public class UnitTestData {
     public static final String DATA_PIECE = "some data";
     public static final String MORE_DATA = "and more data";
     //... and so on
}
</code>```

Solution 2:

```<code>public class GuiTestData {
    public String getDataPiece() {
        return "some data";
    }

    public String getMoreData() {
        return "and more data";
    }
    //... and so on.
}
</code>```

My question now is: Which of these approaches is the objectively better one?


问:
Why are the  and  tags deprecated?
This question came up in one of my college classes. The professor only gave the answer that it was more descriptive, but it seems as though  and  are rather explicit in their meaning and is easier to type than  and .

What were the official arguments for the deprecation of these tags?

答:
As Doval says, they are not deprecated. They still exist but should be used differently from what many people where used to before HTML5.

It's about 'semantic' html. It should describe 'what' it is, instead of how it should look. The browser or theoretically any other display medium (say a reading app for the blind) should be able to decide how exactly  should be interpreted.

This is similar why you should not use CSS class names like "red" and use more descriptive classes that describe the functional idea behind using a different color here. You may later decide that green is better (and maybe anything "strong" should be shown by using red color instead of bold text). Or a color blind user may have some special browser settings where colors are replaced by other means.

Title: Why are the <b> and <i> tags deprecated?


问:
How to model users accounts in ASP MVC Framework with Entity Framework using code-first approach?
How do you design your code-first approach in entity framework when your code should include user data (name, password...) without repeating/overwriting what the MVC framework will generate in terms of user tables, classes, authorization and authentication

As simple example, if i have an application that will manage user projects, my models will contain the following classes, and have the following relationship:

```<code>- One user may be assigned one or MANY projects
- One project may be assigned to one or MANY users
</code>```

( just to show the many to many ERD relationship nature)

```<code>class User{
    //implementation of the class members and
    // navigation properties to alow Entity Framework to create the DB tables
}

class Project{
    //implementation of the class members and
    // navigation properties to alow Entity Framework to create the DB tables
}
</code>```

Now the problem i am facing is that EF and ASP .net MVC framework takes care of creating user tables, profiles and roles entities...

I am not sure whether it is possible to design my classes in a way to show an automatic (conventional)  relationship with the rest of the tables created by MVC.

In the case this needs to be done by writting custom Authentication classes, can you please explain how? because i have found many articles that discuss the same issue but without pointing out the relationship between the project classes and the MVC created classes / tables, plus  if the developer choses to use the MVC way he will end up with two data contexts, even after merging both tables in one sql server catalog.

答:
If I'm reading your question correctly, then what you're looking for is SimpleMembership.

Here's an explanation of SimpleMembership; from the section Easy to with[sic] Entity Framework Code First:

SimpleMembership ... allows[changed] you to use any table as a user store. That means you're in control of the user profile information, and you can access it however you'd like ....

So you can define your own User class to hold extra user data and relationships, e.g.:

```<code>[Table("UserProfile")]
public class UserProfile
{
    // required fields:
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int      UserId { get; set; }
    public string   UserName { get; set; }

    // your extra fields:
    public List<Project> Projects { get; set; }
    // more here...
}
</code>```

and the regular, built-in Membership stuff is used for handling logins, passwords, and everything else behind the scenes.

There's a little more to be done to integrate it, but it wasn't too bad when I used it.

Note that this works from MVC4 on.

Edit to address questions asked in the comments:

From what I can tell looking at my code (it's been a while since I worked on the project and, between it being my first MVC project and other projects scrambling my brain since then, things are a bit fuzzy):

It looks like I ended up creating my own context that inherited from the DbContext class (as outlined in this tutorial) and contained all my application data; e.g.:

```<code>public class MyContext : DbContext
{
    public MyContext()
        : base("DefaultConnection")
    {

    }

    public DbSet<UserProfile> UserProfiles { get; set; }
    public DbSet<Projects> Projects { get; set; }
    // more here
}
</code>```

(IIRC, by inheriting from the DbContext type we get all the built-in membership stuff.)

Then the rest of the app had to be configured to use this context (as given in the tutorial). (All of my controllers create an instance of my derived context, but this could be set up using dependency injection as well.)

It looks like the database connection is initialized in the InitializeSimpleMembershipAttribute by default -- this attribute gets applied to all the controllers.

I ran into the issue stated in this question with this setup, so to fix it I followed the advice in the accepted answer and moved the database connection initialization code to a central point (Global.asax.cs) that ran only once when the web app started.

I'm afraid that I can't remember many more details than this, so you'll probably have to do a bit more research (and probably troubleshooting) to get this working, but it should give you a decent idea of what to look for.

I ended up reading a lot of tutorials on asp.net, so you might check that site out if you've not already.

Body:
If I'm reading your question correctly, then what you're looking for is SimpleMembership.

Here's an explanation of SimpleMembership; from the section Easy to with[sic] Entity Framework Code First:

SimpleMembership ... allows[changed] you to use any table as a user store. That means you're in control of the user profile information, and you can access it however you'd like ....

So you can define your own User class to hold extra user data and relationships, e.g.:

[Table("UserProfile")]
public class UserProfile
{
    // required fields:
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int      UserId { get; set; }
    public string   UserName { get; set; }

    // your extra fields:
    public List<Project> Projects { get; set; }
    // more here...
}

and the regular, built-in Membership stuff is used for handling logins, passwords, and everything else behind the scenes.

There's a little more to be done to integrate it, but it wasn't too bad when I used it.

Note that this works from MVC4 on.

Edit to address questions asked in the comments:

From what I can tell looking at my code (it's been a while since I worked on the project and, between it being my first MVC project and other projects scrambling my brain since then, things are a bit fuzzy):

It looks like I ended up creating my own context that inherited from the DbContext class (as outlined in this tutorial) and contained all my application data; e.g.:

public class MyContext : DbContext
{
    public MyContext()
        : base("DefaultConnection")
    {

    }

    public DbSet<UserProfile> UserProfiles { get; set; }
    public DbSet<Projects> Projects { get; set; }
    // more here
}

(IIRC, by inheriting from the DbContext type we get all the built-in membership stuff.)

Then the rest of the app had to be configured to use this context (as given in the tutorial). (All of my controllers create an instance of my derived context, but this could be set up using dependency injection as well.)

It looks like the database connection is initialized in the InitializeSimpleMembershipAttribute by default -- this attribute gets applied to all the controllers.

I ran into the issue stated in this question with this setup, so to fix it I followed the advice in the accepted answer and moved the database connection initialization code to a central point (Global.asax.cs) that ran only once when the web app started.

I'm afraid that I can't remember many more details than this, so you'll probably have to do a bit more research (and probably troubleshooting) to get this working, but it should give you a decent idea of what to look for.

I ended up reading a lot of tutorials on asp.net, so you might check that site out if you've not already.


问:
How to manage client expectations when working with a BDD type framework?
I've recently been investigating SpecFlow and can see the value of it for writing client/stateholder specs which can show a feature's progress however I'm nervous of introducing them to a client...

Imagine a feature:

```<code>Given I'm on the "MyAccount" page
   And I enter "Liath" into the "Name" textbox
When I click "Save"
Then my username should be "Liath"
</code>```

We write these features, show the client a set of red icons and set the developers to work.

If our developer completes the happy path then the icon will go to green and the client will see that feature is complete.

However our developer knows that they've still got to consider things like:

Checking the username to see if it's in use

Checking the username for profanity

Handle errors from the database

Some form of validation to ensure the username uses only valid characters

Clearly if a client sees a row of green lights they will be reluctant to continue paying for the rest of the development, it will look like we're trying to stretch out the work.

What is the value of these tests as a progress indicator if they indicate work is completed long before it is? What techniques would we need to use to give a more accurate example of progress?

答:
The scenario above could be one of many around the account update story.

```<code>In order to keep my account up to date,
As a registered user
I should be able to update my account
</code>```

So you would multiple scenarios covering each of the potential outcomes for a story. A story could also be one of many stories for a feature.

What is the value of these tests as a progress indicator if they
  indicate work is completed long before it is?

One of the values tests like this bring is to show how far through a feature the development is. Only once all the tests are green is this feature functionally ready to be shipped.

This also a good point to add that the acceptance tests [AT] will be part of a suite of unit tests. Following the red, green, refactor method of TDD, the AT will be written first and fail. Then the unit tests for the code are written. Once the unit tests are passing the AT will pass because the code for that scenario has been completed.

What techniques would we need to use to give a more accurate example
  of progress?

Out of the box Specflow has the @ignore tag which you can put on your scenarios, eg

```<code>@ignore
Scenario: Updating my username
    Given I'm on the "MyAccount" page
    And I enter "Liath" into the "Name" textbox
    When I click "Save"
    Then my username should be "Liath"
</code>```

This is useful as it gives the developer the ability to have all your stories and scenarios written but not executing, so when you show the tests to the client they will see a mix of green and yellow, the yellow being the scenarios that have not been started/completed.

I hope that covers it.

Rich

Body:
The scenario above could be one of many around the account update story.

In order to keep my account up to date,
As a registered user
I should be able to update my account

So you would multiple scenarios covering each of the potential outcomes for a story. A story could also be one of many stories for a feature.

What is the value of these tests as a progress indicator if they
  indicate work is completed long before it is?

One of the values tests like this bring is to show how far through a feature the development is. Only once all the tests are green is this feature functionally ready to be shipped.

This also a good point to add that the acceptance tests [AT] will be part of a suite of unit tests. Following the red, green, refactor method of TDD, the AT will be written first and fail. Then the unit tests for the code are written. Once the unit tests are passing the AT will pass because the code for that scenario has been completed.

What techniques would we need to use to give a more accurate example
  of progress?

Out of the box Specflow has the @ignore tag which you can put on your scenarios, eg

@ignore
Scenario: Updating my username
    Given I'm on the "MyAccount" page
    And I enter "Liath" into the "Name" textbox
    When I click "Save"
    Then my username should be "Liath"

This is useful as it gives the developer the ability to have all your stories and scenarios written but not executing, so when you show the tests to the client they will see a mix of green and yellow, the yellow being the scenarios that have not been started/completed.

I hope that covers it.

Rich


答:
First things first, I don't see how programming to interfaces addresses the expression problem.

In short, the expression problem states that you can only add new data or new functions to a datatype (without recompiling) - not both.

Programming to most interfaces lets you make new functions that use that interface, but you can't add data to the interface without recompiling it.

Now, on to the meat of the question:

Is 'design with types first' ultimately the same as 'design with interfaces first'?

Not quite.

Design interfaces first implies you approach the design from how it's going to be used, rather than how it's going to be implemented.

Design types first implies that as well, but in a language like scala some of the implementation details will impact how it can be structured. Are you going to use structural types? Are you going to use case classes? Are you going to mix in some traits? How you slice the reusable parts will change how you present the interface for the code's use.

Body:
In Object Oriented Programming, we're taught to think in terms of Polymorphism (the idea that the implementation is decoupled from  the interface - and that it makes sense to think of the interface first).

This is expressed by Eric Gamma (author of Gang of Four Design Patterns Book)

Program to an interface, not an implementation

Later we learned that this is a way of solving The Expression Problem.

Other languages such as Clojure address 'The Expression Problem' using protocols.

Now coming out of Scala (and perhaps influenced by Haskell) we're seeing a design movement that says "Design your Types First". When I heard that - I thought I'd heard it before.

My question is: Is 'design with types first' ultimately the same as 'design with interfaces first'?


问:
Logging Exception in multi-tier application
I'm building a multi-tier enterprise application using Spring. I have different layers: Controller, Business and Provider. Within the application I've built a custom error handling mini-framework that is based on a single RuntimeException which has an error code to discriminate different kind of errors.

Error codes are enums implementing this interface:

```<code>public interface ErrorCode {
    public int getNumber();
    public String getDeveloperMessage();
    public String getHelpURL();
    public boolean isSystemError();
}
</code>```

So for example I have:

```<code>public enum SystemErrorCode implements ErrorCode{
    E_UNKNOWN_ERROR(10000, "Unkwown internal error", true),
    E_MISSING_ARGUMENT(10001, "Missing argument", true),
    E_INVALID_ARGUMENT(10002, "Invalid argument", true),
    E_CONTEXT_NO_TENANT(10003, "No tenant in context", true),
    E_CONTEXT_TENANT_CHANGE(10004, "Tenant change attempt", true),
    E_CONTEXT_ALREADY_INITIALIZED(10005, "Tenant already initialized", true),
    E_NOT_IMPLEMENTED(10005, "Feature not implemented", true),
    ...
}
</code>```

The exception class itself look like this (getters, constructors, and utility methods are stripped out for brevity):

```<code>public class EngineRuntimeException extends RuntimeException {
    private static final long serialVersionUID = 1L;

    private final String incidentReportId;
    private final ErrorCode code;
    private final Map<String, Object> additionalInfo;

    ...

    public static EngineRuntimeException wrap(Throwable exception, ErrorCode errorCode) {
        if(exception instanceof EngineRuntimeException) {
            EngineRuntimeException se = (EngineRuntimeException)exception;
            if(errorCode != null && errorCode != se.getCode()) {
                return new EngineRuntimeException(exception.getMessage(), exception, errorCode);
            }
            return se;
        } else {
            return new EngineRuntimeException(exception.getMessage(), exception, errorCode);
        }
    }

    public static EngineRuntimeException wrap(Throwable exception) {
        return wrap(exception, SystemErrorCode.E_UNKNOWN_ERROR);
    }
</code>```

Using the wrap utility method I can easily encapsulate exceptions in the different layers without loosing the original stack and without having it dirty by rethrowing or incapsulating exception more than once.

```<code>try {
  if(a == null) {
    throw new EngineRuntimeException(SystemErrorCode.E_UNKNOWN_ERROR, "blah, blah, blah");
  }

  // Some other code that can throw exceptions (checked or unchecked),
  // these exceptions will be wrapped inside EngineRuntimeException if they
  // are not already of that type.

} catch (Exception ex) {
    throw EngineRuntimeException.wrap(ex);
}
</code>```

To recap, each layer has a safety boundary try-catch that wraps Exceptions into EngineRuntimeException, just rethrowing what is already an EngineRuntimeException.

The question is: where should I log the exception? I want to log the exception only once so I was thinking about do the logging stuff inside the constructor of EngineRuntimeException class itself. Is that a bad idea? Is it better to do the logging stuff only in the catch blocks of the layer boundaries?

Also I don't want to simply log using a log4j but I want to use a service (@Service) injected by spring so that I can decide to do something more than just logging on file system. How can I do that since I can't make spring inject something in a class I have created with the "new" instruction.

答:
If you don't mind having another library dependence, you could use AspectJ to log any exceptions that derive from EngineRuntimeException with the following declaration:

```<code>public before() throws EngineRuntimeException: mypointcut() {...}
</code>```

The reason why this method is preferable is to decouple the logging from the actual exception itself.

You could of course also log directly in EngineRuntimeException and though you shouldn't run into any problems, if you need to create another exception that doesn't derive from EngineRuntimeException, that too would need to reuse the same log whereas with AspectJ, you could have a cleaner solution overall with all "logging" together in one place.  You can generalize AspectJ also for other parts of your program in an unintrusive way.

Note that if you don't want AspectJ to impact performance, there are ways you can apply aspectJ to jars while being built in such a way to not require AspectJ in production at all.  In this way you wouldn't need to use Spring at all.

Body:
If you don't mind having another library dependence, you could use AspectJ to log any exceptions that derive from EngineRuntimeException with the following declaration:

public before() throws EngineRuntimeException: mypointcut() {...}

The reason why this method is preferable is to decouple the logging from the actual exception itself.

You could of course also log directly in EngineRuntimeException and though you shouldn't run into any problems, if you need to create another exception that doesn't derive from EngineRuntimeException, that too would need to reuse the same log whereas with AspectJ, you could have a cleaner solution overall with all "logging" together in one place.  You can generalize AspectJ also for other parts of your program in an unintrusive way.

Note that if you don't want AspectJ to impact performance, there are ways you can apply aspectJ to jars while being built in such a way to not require AspectJ in production at all.  In this way you wouldn't need to use Spring at all.


答:
The question is: where should I log the exception? I want to log the exception only once

Log the exception only once, namely at place where it gets handled (not rethrown). This is the place where you have the most information.

so I was thinking about do the logging stuff inside the constructor of EngineRuntimeException class itself. Is that a bad idea?

Yes. This leads to logging it multiple times.

Also I don't want to simply log using a log4j but I want to use a service (@Service) injected by spring so that I can decide to do something more than just logging on file system.

That looks like pure boilerplate to me. I'd bet log4j can be configured to log to whatever you want. If it's not enough, switch to slfj which allows you to use any of the most common 4+ logging frameworks.

Title: Logging Exception in multi-tier application

Body:
I'm building a multi-tier enterprise application using Spring. I have different layers: Controller, Business and Provider. Within the application I've built a custom error handling mini-framework that is based on a single RuntimeException which has an error code to discriminate different kind of errors.

Error codes are enums implementing this interface:

```<code>public interface ErrorCode {
    public int getNumber();
    public String getDeveloperMessage();
    public String getHelpURL();
    public boolean isSystemError();
}
</code>```

So for example I have:

```<code>public enum SystemErrorCode implements ErrorCode{
    E_UNKNOWN_ERROR(10000, "Unkwown internal error", true),
    E_MISSING_ARGUMENT(10001, "Missing argument", true),
    E_INVALID_ARGUMENT(10002, "Invalid argument", true),
    E_CONTEXT_NO_TENANT(10003, "No tenant in context", true),
    E_CONTEXT_TENANT_CHANGE(10004, "Tenant change attempt", true),
    E_CONTEXT_ALREADY_INITIALIZED(10005, "Tenant already initialized", true),
    E_NOT_IMPLEMENTED(10005, "Feature not implemented", true),
    ...
}
</code>```

The exception class itself look like this (getters, constructors, and utility methods are stripped out for brevity):

```<code>public class EngineRuntimeException extends RuntimeException {
    private static final long serialVersionUID = 1L;

    private final String incidentReportId;
    private final ErrorCode code;
    private final Map<String, Object> additionalInfo;

    ...

    public static EngineRuntimeException wrap(Throwable exception, ErrorCode errorCode) {
        if(exception instanceof EngineRuntimeException) {
            EngineRuntimeException se = (EngineRuntimeException)exception;
            if(errorCode != null && errorCode != se.getCode()) {
                return new EngineRuntimeException(exception.getMessage(), exception, errorCode);
            }
            return se;
        } else {
            return new EngineRuntimeException(exception.getMessage(), exception, errorCode);
        }
    }

    public static EngineRuntimeException wrap(Throwable exception) {
        return wrap(exception, SystemErrorCode.E_UNKNOWN_ERROR);
    }
</code>```

Using the wrap utility method I can easily encapsulate exceptions in the different layers without loosing the original stack and without having it dirty by rethrowing or incapsulating exception more than once.

```<code>try {
  if(a == null) {
    throw new EngineRuntimeException(SystemErrorCode.E_UNKNOWN_ERROR, "blah, blah, blah");
  }

  // Some other code that can throw exceptions (checked or unchecked),
  // these exceptions will be wrapped inside EngineRuntimeException if they
  // are not already of that type.

} catch (Exception ex) {
    throw EngineRuntimeException.wrap(ex);
}
</code>```

To recap, each layer has a safety boundary try-catch that wraps Exceptions into EngineRuntimeException, just rethrowing what is already an EngineRuntimeException.

The question is: where should I log the exception? I want to log the exception only once so I was thinking about do the logging stuff inside the constructor of EngineRuntimeException class itself. Is that a bad idea? Is it better to do the logging stuff only in the catch blocks of the layer boundaries?

Also I don't want to simply log using a log4j but I want to use a service (@Service) injected by spring so that I can decide to do something more than just logging on file system. How can I do that since I can't make spring inject something in a class I have created with the "new" instruction.


问:
Pitfalls in the Grails Framework
What are some of the biggest issues/pitfalls with using the Grails framework?  I'm learning the framework now, and I really like it, but I need to know what major issues I may encounter when using it and how to avoid them.</p>

答:
Currently it has iffy support for abstract classes.  For instance, you can't bind a list of implementations into a single List<T> in a command object. Granted, this is primarily annoying because I'm used to it magically binding everything else! :D

Generally it's still just kind of "green"; you eventually run into odd little limitations and bugs. It's really come a long way in a few years though.

Title: Pitfalls in the Grails Framework


问:
What are memory addresses?
I have more or less 0 knowledge in low-level topics, so forgive my possible ignorance.

I know that in languages such as C, pointers hold 'memory addresses', i.e. strings (or binary data?) written in hex such as 0x52A132F3.

Judging by the term 'memory address', I assume this number actually leads to some place in memory. But I'm having a hard time understanding what a 'place in memory' actually is and how a hexadecimal number can 'lead' to it. (A Java programmer here..)

So two questions:

Do the memory addresses point to places on the CPU itself, or anywhere in the computer?

Is memory 'ordered' in hardware in some way that it makes sense to refer to it using an 'ordinal' number, such as 0x52A132F3? How is memory ordered in hardware, and how and why does it make sense to access it using a numerical value?

答:
First, that a pointer contains a memory address is an implementation detail. That's not how it's defined in the standard. The standard works very, very hard to be vague with regards to the hardware so that C can run on the weirdest, most exotic architectures around. For example, you can't assume two pointers of different types have the same representation (except for char* and void*), or that pointers to objects have the same representation as pointers to functions.

To address your actual question:

Memory addresses point to places in memory. Memory is external to the CPU.*

A process sees memory as a continuous sequence of addressable units (usually 8-bit bytes). So, yes, you can compare memory addresses. As a side note, unless you're running on bare metal, the fact that a process has a continuous segment of memory to work with is just an illusion the OS provides. In reality the memory a process sees is mapped to different ranges of hardware memory and potentially disk space. In other words, memory address 1 for a process doesn't necessarily map to hardware address 1. You don't really need to concern yourself with this unless you want to know how OSs work.

In short, just think of memory as an array of bytes and a memory address as an index into that array.

*Yes, I know CPUs have caches.

Title: What are memory addresses?


答:
While pointers often refer to memory addresses, the concept itself is separate. I'm going to focus on the underlying "low level" concept of memory addresses, since that's what you seem to be asking about.

Do the memory addresses point to places on the CPU itself, or anywhere in the computer?

While the term memory address can conceivably refer to part of the CPU's onboard memory, in this case it does not.

These addresses refer to the common "memory" of the computer, which is usually the RAM, but in modern OSes, may be abstracted away to be physical memory or some virtual memory that actually goes elsewhere. You'll also occasionally see memory addresses refer to memory off on a video card, or other hardware.

Is memory 'ordered' in hardware in some way that it makes sense to refer to it using an 'ordinal' number, such as 0x52A132F3?

Yes.

Even if the memory isn't strictly contiguous (see physical/virtual memory above, in addition to the different chips of RAM), the hardware and then the OS will abstract that away into something that looks like an infinitely long tape. That then is ordered memory that is addressable linearly.

Each address will then have a certain amount of data it can store (a byte is common, but not universal!). Different addressing schemes are possible of course, but would be inefficient since computers are really good at doing integer math.

Title: What are memory addresses?

Body:
I have more or less 0 knowledge in low-level topics, so forgive my possible ignorance.

I know that in languages such as C, pointers hold 'memory addresses', i.e. strings (or binary data?) written in hex such as 0x52A132F3.

Judging by the term 'memory address', I assume this number actually leads to some place in memory. But I'm having a hard time understanding what a 'place in memory' actually is and how a hexadecimal number can 'lead' to it. (A Java programmer here..)

So two questions:

Do the memory addresses point to places on the CPU itself, or anywhere in the computer?

Is memory 'ordered' in hardware in some way that it makes sense to refer to it using an 'ordinal' number, such as 0x52A132F3? How is memory ordered in hardware, and how and why does it make sense to access it using a numerical value?