答:
Ratchet Freak's answer is what I initially thought of too.

An alternate method might be to give each of your modules its own producer-consumer queue and then have your logger mechanism scan these queues on its own thread.

This might end up being more flexible because you can switch out how many loggers you use -- you might have one for everything, one for each module, or divide the modules into groups and have one for each group.

Edit: Elaboration

(don't mind my C -- that's what you said you're coding in, right?)

So this idea is having a producer-consumer queue/list for each of your modules. Such a queue would probably be something like this:

```<code>enum LogItemType {INFO, ERROR};

struct LogItem
{
    LogItemType type;
    char *msg;
};

struct LogQueue {...}; // implementation details -- holds an array/list of LogItem

bool queueLogItem(log_queue *, log_item *);
bool queueHasItems(log_queue *);
bool queueIsFull(log_queue *);
LogItem *dequeueLogItem(log_queue *);
</code>```

Each module will need to either initialize its own such queue or be passed one by the initialization code that sets up the threads etc. The init code should probably keep references to all the queues:

```<code>LogQueue *module_queues = {module_1_queue_ptr, module_2_queue_ptr, ... , module_N_queue_ptr};

setModuleLoggingQueue(module1, module_1_queue_ptr);
// .
// .
// .
</code>```

Inside the modules you'd have them create LogItems and queue them for each message.

```<code>LogItem *item = malloc(sizeof(LogItem));
item->type = INFO;
item->msg = malloc(MSG_SIZE)
memcpy("MSG", item->msg);
queueLogItem(module_queue, item);
</code>```

Then, you'd have one or more consumers of the queues which would take the messages and actually do the log writing in a main loop like so:

```<code>void loggingThreadBody()
{
    while (true)
    {
        for (i = 0; i < N; i++)
        {
            if (queueHasItems(module_queues[i]))
                writeLogItem(dequeueLogItem(module_queues[i]));
        }

        threadSleep(200);
    }
}
</code>```

or something like that.

This would be flexible in allowing you to have different consumers of the queues, e.g.:

```<code>// For one logger:

LogQueue *module_queues = {module_1_queue_ptr, module_2_queue_ptr, ... , module_N_queue_ptr};

initLoggerThread(module_queues);

// -OR-
// For multiple loggers:

LogQueue *group1_queues = {module_1_queue_ptr, ..., module_4_queue_ptr};
LogQueue *group2_queues = {module_5_queue_ptr, ... , module_Nmin7_queue_ptr};
LogQueue *group3_queues = {module_Nmin7_queue_ptr, ... , module_N_queue_ptr};

initLoggerThread(group1_queues);
initLoggerThread(group2_queues);
initLoggerThread(group3_queues);
</code>```

Note: I'm guessing you'd want to allocate the memory for a log message at queue time and deallocate it at consumption time (assuming your messages will contain dynamic content).

Another Edit:

Forgot to mention: if you're expecting a lot of activity on your module threads you might see if you can do the log writes asynchronously so things don't block.

Yet Another Edit:

You'll probably also want to put a timestamp as part of the LogItem; with the logger thread(s) going through the queues sequentially the log statements might get out of order from when they occurred chronologically in the modules.

And Yet Another Edit:

With this setup it would be fairly easy to pass all the modules the same queue and just having the consumer look at that one queue, which would take you back to Ratchet Freak's answer.

Geeze, will you stop editing!?:

Also you could have one queue for each group of modules and have a logger for each queue.

Ok, I'll stop now.

Title: Concurrency pattern of logger in multithreaded application

Body:
The context:
We are working on a multi-threaded (Linux-C) application that follows a pipeline model.

Each module has a private thread and encapsulated objects which do processing of data; and each stage has a standard form of exchanging data with next unit.

The application is free from memory leak and is threadsafe using locks at the point where they exchange data. Total number of threads is about 15- and each thread can have from 1 to 4 objects. Making about 25 - 30 odd objects which all have some critical logging to do.

Most discussion I have seen about different levels as in Log4J and it's other translations. The real big questions is about how the overall logging should really happen?

One approach is all local logging does fprintf to stderr. The stderr is redirected to some file. This approach is very bad when logs become too big.

If all object instantiate their individual loggers - (about 30-40 of them) there will be too many files. And unlike above, one won't have the idea of true order of events. Timestamping is one possibility - but it is still a mess to collate.

If there is a single global logger (singleton) pattern - it indirectly blocks so many threads while one is busy putting up logs. This is unacceptable when processing of the threads are heavy.

So what should be the ideal way to structure the logging objects? What are some of the best practices in actual large scale applications?

I would also love to learn from some of the real designs of large scale applications to get inspirations from!

======

EDIT:

Based on both the answers here is the question I am now left with :

What is the best practice about assigning loggers (logging queues) to the object: should they call some global_api() or should the logger be assigned to them in the constructor. When the objects are in some deep hierarchy this later approach becomes tedious. If they are calling up some global_api() it's kind of coupling with the Application, so trying to use this object in other application throws this dependency. Is there a cleaner way for this?

Body:
Ratchet Freak's answer is what I initially thought of too.

An alternate method might be to give each of your modules its own producer-consumer queue and then have your logger mechanism scan these queues on its own thread.

This might end up being more flexible because you can switch out how many loggers you use -- you might have one for everything, one for each module, or divide the modules into groups and have one for each group.

Edit: Elaboration

(don't mind my C -- that's what you said you're coding in, right?)

So this idea is having a producer-consumer queue/list for each of your modules. Such a queue would probably be something like this:

enum LogItemType {INFO, ERROR};

struct LogItem
{
    LogItemType type;
    char *msg;
};

struct LogQueue {...}; // implementation details -- holds an array/list of LogItem

bool queueLogItem(log_queue *, log_item *);
bool queueHasItems(log_queue *);
bool queueIsFull(log_queue *);
LogItem *dequeueLogItem(log_queue *);

Each module will need to either initialize its own such queue or be passed one by the initialization code that sets up the threads etc. The init code should probably keep references to all the queues:

LogQueue *module_queues = {module_1_queue_ptr, module_2_queue_ptr, ... , module_N_queue_ptr};

setModuleLoggingQueue(module1, module_1_queue_ptr);
// .
// .
// .

Inside the modules you'd have them create LogItems and queue them for each message.

LogItem *item = malloc(sizeof(LogItem));
item->type = INFO;
item->msg = malloc(MSG_SIZE)
memcpy("MSG", item->msg);
queueLogItem(module_queue, item);

Then, you'd have one or more consumers of the queues which would take the messages and actually do the log writing in a main loop like so:

void loggingThreadBody()
{
    while (true)
    {
        for (i = 0; i < N; i++)
        {
            if (queueHasItems(module_queues[i]))
                writeLogItem(dequeueLogItem(module_queues[i]));
        }

        threadSleep(200);
    }
}

or something like that.

This would be flexible in allowing you to have different consumers of the queues, e.g.:

// For one logger:

LogQueue *module_queues = {module_1_queue_ptr, module_2_queue_ptr, ... , module_N_queue_ptr};

initLoggerThread(module_queues);

// -OR-
// For multiple loggers:

LogQueue *group1_queues = {module_1_queue_ptr, ..., module_4_queue_ptr};
LogQueue *group2_queues = {module_5_queue_ptr, ... , module_Nmin7_queue_ptr};
LogQueue *group3_queues = {module_Nmin7_queue_ptr, ... , module_N_queue_ptr};

initLoggerThread(group1_queues);
initLoggerThread(group2_queues);
initLoggerThread(group3_queues);

Note: I'm guessing you'd want to allocate the memory for a log message at queue time and deallocate it at consumption time (assuming your messages will contain dynamic content).

Another Edit:

Forgot to mention: if you're expecting a lot of activity on your module threads you might see if you can do the log writes asynchronously so things don't block.

Yet Another Edit:

You'll probably also want to put a timestamp as part of the LogItem; with the logger thread(s) going through the queues sequentially the log statements might get out of order from when they occurred chronologically in the modules.

And Yet Another Edit:

With this setup it would be fairly easy to pass all the modules the same queue and just having the consumer look at that one queue, which would take you back to Ratchet Freak's answer.

Geeze, will you stop editing!?:

Also you could have one queue for each group of modules and have a logger for each queue.

Ok, I'll stop now.


问:
How important is positive feedback in code reviews?
Is it important to point out the good parts of the code during a code review and the reasons why it is good? Positive feedback might be just as useful for the developer being reviewed and for the others that participate in the review.

We are doing reviews using an online tool, so developers can open reviews for their committed code and others can review their code within a given time period (e.g. 1 week). Others can comment on the code or other reviewer's comments.

Should there be a balance between positive and negative feedback?

答:
When I do code reviews I tend to just have a running monologue, so as I'm making sense of what I'm reading there will be a lot of "Ok, I see what that does.. Good it connects to this and calls that, alright.. and that piece depends on both of those alright.".

I think in this way it's not "oo la la this is so great!", it could be perfectly trivial boring code, but hearing somebody else actually parse and show comprehension of what you wrote is a form of positive feedback in and of itself, the feedback being "This code makes sense", when I run into parts that I don't understand I ask for explanation and when I do understand it exclaim "Ah, I got it".

I think that simple transfer of comprehension is praise to another engineer because we all want our code to be understood by others, it gives a form of implicit validation.

That said, if you see parts of the code that are good or positive characteristics (even boring trivial code can be good if it's the minimal form of itself) I definitely tend to state those characteristics, again I don't attribute them as "Wow great!" so much as "I see this is a minimal implementation" or "Ok, this complex algorithm has lots of comments", focus on the attributes of the code not so much it's inherent goodness or badness.

Any time you attribute "goodness" or "badness" to code in a code review to avoid making the engineer feel looked down upon or held on a pedestal don't say something is good or bad, but rather talk through the cause and effect of their code.

"Ok this part makes sense, ah there's a magic number here, the meaning of that value might not be well understood by the next engineer to touch this"

"I see you've got a DI container here ok so you'll have loose coupling with that repository"

"Ah there's a static dictionary here, if multiple threads are touching that dictionary we could run into some race conditions"

Notice, I'm not saying anything's good or bad, but whether the engineer should change it or not is going to be understood by the engineer whose code is being reviewed. Obviously you have to end the code review with a yay or nay, but accumulating these statements over the course of it will soften the nay's as explanation has already been made in the form of cause and effect statements when you tell them "I'd like those magic numbers fixed before checking this in".

ContentLicense: CC BY-SA 3.0


问:
Hiring Developers - any tips on being more efficient?
I represent a software company that is in process of building a large software development team. We are picky in who we hire and have really good retention rate (most of the devs have been here for an average of 5-6 years).

We've been spending a lot of developers' and HR time and have a low applications to hire ratio. Here's the process we use:

HR Interview on phone - Involves asking basic behavioral and tech questions

Online test - Involves a 30 minute technical test

Technical Phone interview - A 60 minute interview by a developer

Onsite Interview - A 60-90 minute interview by several senior developers

Although this process has been working, we've been spending way too much time on interviews. Any thoughts on how this can be done differently? Our goal is to automate any tasks if possible still retaining the quality of talent.

UPDATE:
Thanks for the responses. Need to clarify a few things. Our aim is to reduce the number of applicants that go from one stage to the other. Here's our current numbers.

We receive 1000 resumes

800 resumes pass the HR interview

500 pass the online test

100 pass the initial phone screen

10 pass the onsite and get hired

As you can see, we need to do a better job of weeding out the candidates earlier on in the process. Can we do a better job in the way the online test evaluates people?

Here are more details on the process based on some responses:

HR Interview on phone - They ask very basic technical questions (What is a CLR?) to weed out as many people as possible

Online test - Have around 10 basic questions with 3 coding questions

Tech phone screen - Covers a variety of technologies. We don't care if the applicant doesn't know everything as long as they can demonstrate they will be able to pick up new technologies and come up to speed quickly

Onsite - Coding questions in front of the developers. More architectural level questions.

答:
My suggestion would be to change the HR interview to be more about asking the candidate about their ideal work environment.  A key point here for HR is to just collect answers and add points about how the answer is given to determine if the candidate knows what kind of environment they want and how well does it match your organization.

There may be something to be said for HR knowing some points in the overall process used within your organization but this is likely where you could do some more weeding if you can set things up so that you have specific values, communication styles, and other points as things for HR to find.  The point here isn't for HR to determine the fit alone but rather collect the information that can then be passed onto a developer or two that can see if someone is worth fast-tracking through the process.


答:
The workflow that I liked most with code reviews was this:

Dev submits patch on mailing list/online tool.

Everyone who needs to care looks at the patch, suggests improvements.

Dev goes back to #1

If there's no improvement needed, people say "Good work, please commit." <- POSITIVE FEEDBACK. All code that is accepted is good. The less people have to tell you to change things, the better you're doing.

Dev commits, moves on to the next item.

Usually what would happen is that new devs would get a lot more 'correctional' feedback as they got familiar with the codebase.

The benefits of this approach are:

Everyone knows what everyone is doing. There's no knowledge monopolizing or mystery commits.

Everyone learns from other's feedback. This is important. If feedback only happens between 2 people in a face-to-face conversation while pairing, then someone on the other side of the room does not benefit the same way as they would if it happened on the mailing list.

Other devs can usually spot some bugs before they're in the version control.

Title: How important is positive feedback in code reviews?

Body:
Is it important to point out the good parts of the code during a code review and the reasons why it is good? Positive feedback might be just as useful for the developer being reviewed and for the others that participate in the review.

We are doing reviews using an online tool, so developers can open reviews for their committed code and others can review their code within a given time period (e.g. 1 week). Others can comment on the code or other reviewer's comments.

Should there be a balance between positive and negative feedback?


答:
I think positive feedback is very important and it's mainly from a personal, realpolitik dynamic.  We all sit and write code for hours, days, weeks, months, and most of us take pride in what we do.  Code reviews are a chance to showcase that.

If you go to a code review and the best outcome you can hope for is "no comment" (i.e. there is no balance of positive feedback), the meeting could easily be titled in outlook "Find Out How Badly People Think You Suck".  Consequently, developers will start to be annoyed by or even dread code reviews, and that's clearly a detriment to the team.  Developers will "forget" to get their code reviewed or will develop learned helplessness and simply ask their constant critics what to do about every little thing to avoid getting blasted in these meetings.

It's all well and good to say that, theoretically, it's most logical to fix the bad and to ask everyone to leave emotions at the door, but it's precisely attitudes like that one that are responsible for the rep developers get as being interpersonally tone deaf.  Theories aside, we're humans and humans like to get a pat on the back from time to time, even a nominal one.  That stuff matters.


问:
Is it the job of a developer to suggest IT requirements?
I am the only developer working on a web application which is nearing to its end. Now we are looking into making it Live in maybe a couple of months time.

This is a web application for a non IT company. Though they have their own internal IT team, they have asked me on what will be the hardware requirements for the live servers eg. RAM, 32 bit or 64 bit.

Shouldn't the internal IT team be doing this or since I am the only person working on the project is it my resposiblity to let them know of the any specific hardware requirements which may impact the performance of the project?

The reason I am asking this question is that, I have not done this before. All the times I used to be given a server and asked to deploy apps on it. I never used to worry about the server configuration, etc.

答: You can't expect them to know the HW requirements of software they've never seen. The architects and senior devs should have an approximate idea of how much storage and processing power is required for x-amount of clients. As for how many clients the software needs to support that is up to the the client and really should be in the signed off spec.</p>


问:
Reusable VS clean code - where's the balance?
Let's say I have a data model for a blog posts and have two use-cases of that model - getting all blogposts and getting only blogposts which were written by specific author.

There are basically two ways how I can realize that.

1st model

```<code>class Articles {

    public function getPosts() {
        return $this->connection->find()
            ->sort(array('creation_time' => -1));
    }

    public function getPostsByAuthor( $authorUid ) {
        return $this->connection->find(array('author_uid' => $authorUid))
            ->sort(array('creation_time' => -1));
    }
}
</code>```

1st usage (presenter/controller)

```<code>if ( $GET['author_uid'] ) {
    $posts = $articles->getPostsByAuthor($GET['author_uid']);
} else {
    $posts = $articles->getPosts();
}
</code>```

2nd one

```<code>class Articles {

    public function getPosts( $authorUid = NULL ) {
        $query = array();

        if( $authorUid !== NULL ) {
            $query = array('author_uid' => $authorUid);
        }

        return $this->connection->find($query)
            ->sort(array('creation_time' => -1));
    }

}
</code>```

2nd usage (presenter/controller)

```<code>$posts = $articles->getPosts( $_GET['author_uid'] );
</code>```

To sum up (dis)advantages:

1) cleaner code

2) more reusable code

Which one do you think is better and why? Is there any kind of compromise between those two?

答:
I like option #1 myself because it gives a clearer narrative to your code, and other programmers don't have to dig into your Articles class to see the what happens when $authorUid is null.

But if your going to be using that if() in many places then it makes sense to do something to wrap that logic up into a single function. Maybe make it a convention that any generically named method like getPosts() will have logic to resolve optional arguments. Maybe do something like this:

```<code>class Articles {

    public function getPosts($authorUid = NULL, $yada = NULL, $yada_yada = NULL) {
        // Keep all descision making logic in this method.
        if( $authorUid !== NULL ) {
            // call getPostsByAuthor
        } else if(...) {
            // call getAllPosts
        } // else if (...) and so on...
    }

    public function getAllPosts() {
        return $this->connection->find()
            ->sort(array('creation_time' => -1));
    }

    public function getPostsByAuthor( $authorUid ) {
        return $this->connection->find(array('author_uid' => $authorUid))
            ->sort(array('creation_time' => -1));
    }
}
</code>```

Body:
I like option #1 myself because it gives a clearer narrative to your code, and other programmers don't have to dig into your Articles class to see the what happens when $authorUid is null.

But if your going to be using that if() in many places then it makes sense to do something to wrap that logic up into a single function. Maybe make it a convention that any generically named method like getPosts() will have logic to resolve optional arguments. Maybe do something like this:

class Articles {

    public function getPosts($authorUid = NULL, $yada = NULL, $yada_yada = NULL) {
        // Keep all descision making logic in this method.
        if( $authorUid !== NULL ) {
            // call getPostsByAuthor
        } else if(...) {
            // call getAllPosts
        } // else if (...) and so on...
    }

    public function getAllPosts() {
        return $this->connection->find()
            ->sort(array('creation_time' => -1));
    }

    public function getPostsByAuthor( $authorUid ) {
        return $this->connection->find(array('author_uid' => $authorUid))
            ->sort(array('creation_time' => -1));
    }
}


答:
Not nearly as important as honest feedback.
I work for a large financial corporation, and our customers don't care if the programmer is trying hard or is a good guy, or usually write good code! They require software that works.</p>


问:
How should I create lo-fi (non-interactive) UI prototypes?
I'm currently working on throwing together some basic prototypes, partly to gather requirements and partly to design the final UI.

At the moment I'm trying building up the screen using Post-it notes, with yellow notes for information and pink for actions (buttons or menus). The idea being that you can easily move, remove and add information. But I'm sure there are more efficient methods out there.

What is the recommended way for developers to efficiently create non-interactive UI prototypes?

And why?

I tried some pen, paper and Post-it note versions and they went down like a lead balloon (likely my drawing skills). In the end I used Balsamiq, which thus far is liked by most users and they get it is prototype. Alas, some people are still having problems with the idea that first one should get an idea of what the application should do via some lo-fi prototypes and really want to "see something on the screen" before committing to anything.

答:
It sounds like you're using good methods, but you're running into resistance having people accept the utility of rapid prototyping. Everybody loves to code, but if a half hour in, they're still fighting with JScrollBars and you've generated 12 mockups, they might understand that the strength of these tools is in rapid iteration.

That being said, the various low-fi approaches have different strengths:

Whiteboard prototyping is useful because anyone can participate and it helps ground ideas, but it's only a discussion tool. You want something one step further if you're going to iterate on a design, if only because it gets hard to erase old marker. ;)

Paper prototyping is useful because people understand it is impermanent and open to change, and you can iterate quickly, and you still can get very good results. Do a usability test with some users on a paper prototype and you can get great feedback on a design very rapidly, at quite low cost. People get really engaged when they can see and feel the interface on paper.

Balsamiq is a shortcut for rapidly making paper prototypes that are reusable. I print out screenshots and use them as paper prototypes. I also use it during meetings to help mock up ideas as we have them--similar to whiteboard prototyping. I've also used Visio and OmniGraffle for this.

Cut and paste prototypes are good for iterating on an existing design--take a screen shot, chop it up in an image editor, and mix and match with new controls (from Balsamiq or elsewhere). Once again your goal is rapid iteration of the prototype, not something that looks good.

I never do prototyping with users. I do it with the design team, based on the user data we have. Users are not designers; if you treat them as designers, you end up with watery tomato sauce, bitter coffee (thanks Malcolm Gladwell), and The Homer car. Gather user input to your design and use it to refine the design with the design team.

ContentLicense: CC BY-SA 2.5


问:
Recovering an anemic domain model into a multitier architecture
I have spent the past several days learning about domain driven design and attempting to apply it to a current project. I decomposed the problem domain into the canonical logical components: domain, infrastrucutre, and presentation. Having completed a first pass, I stepped back and realized that the architecture I created resulted in virtually all of the business logic being contained in the application services, which is the tell-tale sign of the anemic domain model code smell. After further consideration, domain driven design may not be the correct fit for my application, and even if it is, given the small number of business logic rules in my application, the layering overhead may not be worth it. Since I have only written UML and psuedo code I haven't invested too much time and effort, but I would like to leverage what I can if it's possible.

My architecture consists of 4 physical components:

The "domain" project: abstractions and CRUD methods for the data type I use

The "infrastucture" project: the logical representation and methods to be performed on the data

The "presentation" project: the user interface (WinForms or WPF)

The "services" project: coordiates the domain and infrastucture objects according to business rules

If I were to use the components as designed in a multitiered architecture, my presentation layer would only hold references to 3 or 4 services to handle all application functionality. Is this a valid approach?

答:
I think that you are on the right track but it might be easier to understand possible different nomenclature to get a better understanding about the role that each of the layers will play in the architecture.

The Martin Fowler article that you linked where he coined the term, Anemic Data Model was his criticism at the time for transactional based architecture and the trend to push Behavior out of data objects entirely.  Being a staunch believer in OOP programming and design it is a valid criticism, as he worries about the sophistication of software devolving back to the days of structs and procedural programming.  Personally I feel he is rather alarmist and this will not be a problem if strict architectural adherence to layerd and tiered software design is followed in a stateless and transactional way.

Domain layer consists of data objects that have the knowledge and ability to persist themselves to database or file

Infrastructure layer  This could also be known as your Data Access Layer.  Stateless DAO classes will have the ability to fetch data objects.

I propose you have a seperate Business Logic Layer.  Any and all application logic, special domain logic, interactions with multiple DAO or external web services, integrating third party services to satisfy business rules and all in a stateless transactional way, are all examples of what code belongs here.  Sure some of this will simply be a wrapper to a single DAO call, but the seperation of concerns and low coupling is the benefit.

Presentation Layers  There can be multiple presentation layers but they will typically be the arbiters of stateful communication with your users.  A good example of this would be the Controller in MVC architecture.  It can maintain stateful interaction, and where business logic operations are required, it should be as simple as calling the necessary transactions to retrieve the data needed or perform the necessary operation.  There are different types of presentation as well, a thick client or web server will can call the business logic layer directly, or perhaps the business logic can be wrapped by web services that return the data in a useful form.

Service Layers The service layer should be devoid of any application or business logic and should focus primarily on a few concerns.  It should wrap Business Layer calls, translate your Domain in a common language that your clients can understand, and handle the communication medium between server and requesting client.

Knowing this and understanding the benefits of why this type of transactional architecture might be useful to you however depend entirely on the type of software that you are building.  Also questions about the appropriate number of services are meaningless in this case without a complete understanding of your business and technical requirements.  It is a valid approach if you wish to build large highly maintainable, scalable solutions with low coupling.  If you are building a smaller application with few business concerns or objectives then perhaps it is overkill.

OwnerDisplayName: Noren


答:
I can't agree with this at all. What's the difference between Good Development Techniques and so-called Ninja Coders who can write awesome but inexplicable-to-mere-mortals code? Software Development is now (IMO) a Lowest Common Denominator discipline where flair and cunning are shunned in favour of maintainability and ease-of-understanding. It's just too risky.

I can't think of a time I've ever seen code in a review that would make me go 'Oh that's cool'. I can only assume that if I did encounter code like this it'd fall into the Cool-Yet-Unacceptable camp.

You'd also have issues with people who get no positive feedback perhaps trying too hard and making an eventual mess "Trust me, it works!".

Code reviews are there to spread code quality responsibility among the team, i.e. an individual developer cannot be blamed if a serious problem arises later. Use it to find issues, use it to get explanations from the original developer of weird stuff in case you ever end up having to maintain it. Personally, I'm more interested in receiving negative feedback. Customers don't care about the coolness of your code, only that it does what they want.

Leave the backslapping to the pub.


问:
Versioning APIs
Suppose that you have a large project supported by an API base.  The project also ships a public API that end(ish) users can use.

Sometimes you need to make changes to the API base that supports your project.  For example, you need to add a feature that needs an API change, a new method, or requires altering of one of the objects, or the format of one of those objects, passed to or from the API.

Assuming that you are also using these objects in your public API, the public objects will also change any time you do this, which is undesirable as your clients may rely on the API objects remaining identical for their parsing code to work.  (cough C++ WSDL clients...)

So one potential solution is to version the API.  But when we say "version" the API, it sounds like this also must mean to version the API objects as well as well as providing duplicate method calls for each changed method signature.  So I would then have a plain old clr object for each version of my api, which again seems undesirable.  And even if I do this, I surely won't be building each object from scratch as that would end up with vast amounts of duplicated code.  Rather, the API is likely to extend the private objects we are using for our base API, but then we run into the same problem because added properties would also be available in the public API when they are not supposed to be.

So what is some sanity that is usually applied to this situation?  I know many public services such as Git for Windows maintains a versioned API, but I'm having trouble imagining an architecture that supports this without vast amounts of duplicate code covering the various versioned methods and input/output objects.

I'm aware that processes such as semantic versioning attempt to put some sanity on when public API breaks should occur. The problem is more that it seems like many or most changes require breaking the public API if the objects aren't more separated, but I don't see a good way to do that without duplicating code.

答:
When maintaining an API that is used by third parties it is inevitable that you will need to make changes. The level of complexity will depend on the type of change that is occuring. These are the main scenarios that come up:

New Functionality Added to Existing API

Old Functionality Deprecated From API

Existing Functionality In API Changing In Some Way

New Functionality Adding To Existing API

This is the easiest scenario to support. Adding new methods to an API should not require any changes to existing clients. This is safe to deploy for the clients that need the new functionality as it has no updates for any existing client.

Old Functionality Deprecated From API

In this scenario you must communicate to existing consumers of your API that the functionality will not be supported long term. Until you either drop support for the old functionality (or until all clients have upgraded to the new functionality) you must keep the old and new API functionality at the same time. If it's a library that's provided most languages have a way to mark old methods as obsolete/deprecated. If it's a third party service of some kind it's usually best to have different endpoints for the old/new functionality.

Existing Functionality In API Changing In Some Way

This scenario will depend on the type of change. If input parameters no longer need to be used then you can just update the service/library to ignore the now extra data. In a library it would be to have the overloaded method internally call the new method that requires fewer parameters. In a hosted service you have the endpoint ignore extra data and the it can service both types of clients and run the same logic.

If the existing functionality needs to add new required elements then you must have two endpoints/methods for your service/library. Until clients update you need to support both versions.

Other Thoughts

Rather, the API is likely to extend the private objects we are using for our base API, but then we run into the same problem because added properties would also be available in the public API when they are not supposed to be.

Do not expose internal private objects through your library/service. Create your own types and map the the internal implementation. This will allow you make internal changes and minimize the amount of updating the external clients need to do.

The problem is more that it seems like many or most changes require breaking the public API if the objects aren't more separated, but I don't see a good way to do that without duplicating code.

The API whether it is a service or library needs to be stable at the integration point with the clients. The more time you take to identify what the inputs and outputs should be and keep them as separate entities will save you a lot of headaches down the road. Make the API contract its own separate entity and map to the classes that provide the real work. The time saved when internal implementations change should more that offset the extra time it took to define the extra interface.

Don't view this step as "duplicating code". While similar, they are separate entities that are worth the while to create. While external API changes almost always require a corresponding change to internal implementation, internal implementation changes shouldn't always change the external API.

Example

Suppose you are providing a payment processing solution. You are using PaymentProviderA to do credit card transactions. Later on you get a better rate through PaymentProviderB's payment processor. If your API exposed Credit Card / Bill Address fields of your type instead of PaymentProviderA's representation then the API change is 0 as the interface remained the same (hopefully anyway, if PaymentProviderB requires data that was not required by PaymentProviderA then you need to chose to either support both or keep the worse rate with PaymentProviderA).

Title: Versioning APIs


答: It has mattered to me.  I don't want fluff comments or positivity for the sake of positivity.  If all the code I wrote is crappy, you tell me why and let's correct it and learn.  But if I do something right, it's nice to hear it once and awhile.  I don't need positive reinforcement for everything I did that was "correct", but even if it's a "let's improve X, Y, and Z, but the rest looks good" it matters.</p>


答:
This isn't so much a programming question as it is a general management and human-interactions question. Positive feedback in code reviews is exactly as important as positive feedback in any sort of review.

Whether or not this is required (and the extent to which it is required) is a function of the disposition and emotional makeup of the person you're talking to. Some people respond to correction much more effectively when it is coupled with praise. Others see praise as insincere when delivered with correction.

The general formula is sometimes called a "Feedback Sandwich": Good stuff first, bad stuff second, good stuff last. The idea is to keep the overall tone positive while at the same time making sure that the negative feedback is received. This can help prevent stress when anticipating a review, and help prevent self-absorbed brooding afterward. Both are very important with respect to productivity and quality. This isn't just touchy-feely emotional hogwash; It's human behavior 101.

Again, you have to know the person you're working with and understand what they respond to. Dealing with people is what management is about, and good managers know how to make people respond.


问:
How to implement isValid correctly?
I'm trying to provide a mechanism for validating my object like this:

```<code>class SomeObject {
    private $_inputString;
    private $_errors=array();

    public function __construct($inputString) {
        $this->_inputString = $inputString;
    }

    public function getErrors() {
        return $this->_errors;
    }

    public function isValid() {
        $isValid = preg_match("/Some regular expression here/", $this->_inputString);
        if($isValid==0){
            $this->_errors[]=  'Error was found in the input';
        }
        return $isValid==1;
    }

}
</code>```

Then when I'm testing my code I'm doing it like this:

```<code>    $obj = new SomeObject('an INVALID input string');

    $isValid = $obj->isValid();
    $errors=$obj->getErrors();

    $this->assertFalse($isValid);
    $this->assertNotEmpty($errors);
</code>```

Now the test passes correctly, but I noticed a design problem here. What if the user called $obj->getErrors() before calling $obj->isValid()?

The test will fail because the user has to validate the object first before checking the error resulting from validation. I think this way the user depends on a sequence of action to work properly which I think is a bad thing because it exposes the internal behaviour of the class.

How do I solve this problem?

Should I tell the user explicitly to validate first? Where do I mention that?

Should I change the way I validate?  Is there a better solution for this?

UPDATE:

I'm still developing the class so changes are easy and renaming functions and refactoring them is possible.

答: Make isValid equal something by default that is only changed when it is is checked... When calling getErrors check for that default value and return an exception if it is there... That way, getErrors won't work unless isValid has been run first.

Title: How to implement isValid correctly?


答:
There are two ways to get around this issue:

To set $_errors to an error indicating that the validation has not happened yet, or

To go "completely stateless": make isValid return the array of errors, and check the size of the array to determine if the validation succeeded or not

The first path is easier for an existing system: making this change is a lot cheaper, but interaction with your object remains statefull, in the sense that it relies on the order of calls.

The second path may be harder to integrate, but if it is applied consistently, it makes your API easier to learn. Different languages provide different ways of addressing the need to return multiple items from a call. In languages that allow passing by reference or by pointer, validating function often returns a boolean, and a placeholder for errors is passed by reference on the side.

Title: How to implement isValid correctly?

Body:
I'm trying to provide a mechanism for validating my object like this:

```<code>class SomeObject {
    private $_inputString;
    private $_errors=array();

    public function __construct($inputString) {
        $this->_inputString = $inputString;
    }

    public function getErrors() {
        return $this->_errors;
    }

    public function isValid() {
        $isValid = preg_match("/Some regular expression here/", $this->_inputString);
        if($isValid==0){
            $this->_errors[]=  'Error was found in the input';
        }
        return $isValid==1;
    }

}
</code>```

Then when I'm testing my code I'm doing it like this:

```<code>    $obj = new SomeObject('an INVALID input string');

    $isValid = $obj->isValid();
    $errors=$obj->getErrors();

    $this->assertFalse($isValid);
    $this->assertNotEmpty($errors);
</code>```

Now the test passes correctly, but I noticed a design problem here. What if the user called $obj->getErrors() before calling $obj->isValid()?

The test will fail because the user has to validate the object first before checking the error resulting from validation. I think this way the user depends on a sequence of action to work properly which I think is a bad thing because it exposes the internal behaviour of the class.

How do I solve this problem?

Should I tell the user explicitly to validate first? Where do I mention that?

Should I change the way I validate?  Is there a better solution for this?

UPDATE:

I'm still developing the class so changes are easy and renaming functions and refactoring them is possible.


问:
How can I compute the Big-O notation for a given piece of code?
So I just took a data structure midterm today and I was asked to determine the run time, in Big O notation, of the following nested loop:

```<code>for (int i = 0; i < n-1; i++) {
    for(int j = 0; j < i; j++2) {
        //1 Statement
    }
}
</code>```

I'm having trouble understanding the formula behind determining the run time. I thought that since the inner loop has 1 statement, and using the series equation of:
(n * (n - 1)) / 2, I figured it to be: 1n * (n-1) / 2. Thus equaling (n^2 - 1) / 2.
And so I generalized the runtime to be O(n^2 / 2). I'm not sure this is right though haha, was I supposed to divide my answer again by 2 since j is being upped in intervals of 2?
Or is my answer completely off?

答:
To be precise, //1 statement would matter a lot in calculating the Big-O notation for a given piece of code. But considering that it takes a constant time ( I am supposing it is a count+=1 statement) then your solution would go like:

```<code>(Sigma i (over 1 to n) (Sigma j (over 1 to i))
</code>```

And this would lead to O(n^2).

I suggest that you solve the problems at this link once. These will give you a good idea.

Body:
To be precise, //1 statement would matter a lot in calculating the Big-O notation for a given piece of code. But considering that it takes a constant time ( I am supposing it is a count+=1 statement) then your solution would go like:

(Sigma i (over 1 to n) (Sigma j (over 1 to i))

And this would lead to O(n^2).

I suggest that you solve the problems at this link once. These will give you a good idea.


问:
What are the advantages of using the Java debugger over println?
I always use System.out.println(...) to debug my code, and it works pretty well. In which cases do you use the eclipse java debugger? I never had to use it and the little bug symbol is still ab bit mysterious to me. Are there cases where the debugger helps me that I can´t solve with a println?

答:
The problem with println debugging is that the printlns themselves are a problem.

When you're debugging using println, you have to modify your code to do it.  This means you need to already know, or at least have a pretty good idea, where the bug is before you start debugging.  And if you already know that, you probably don't need to debug anyway, as (IME at least) the mian reason for debugging is to find where a bug is, not figure out how to fix it.  Once you have the bug pinned down, the solution is usually obvious just from the context.  (Not always, of course, but usually.)

If your println diagnostics turn out not to give you useful information, or not to give you useful enough information, you need to close the program, change the code, recompile, and then get back to the point you were at to reproduce the bug.

Once you're done with finding and fixing the bug, you then have to change your code again to remove the println statements.  (Let's hope you don't miss any and then ship the product with debug output left in!)

An interactive debugger doesn't have these problems.  You don't need to know what you're looking for ahead of time; you can examine the values of any arbitrary variable at any time it's in scope.  You don't need to modify your code, because the debugger is external to your program.  And therefore you don't need to stop debugging, change your code and recompile to look at something new.

That principle alone--inspecting the code from the outside instead of having to instrument it (and then un-instrument it) manually--is the reason interactive debuggers have become the favorite method of debugging among modern programmers.


答:
You should have googled it first. Debugger in Eclipse already contains a lot of functions that you don't need to define. Other than that, System.out.println() statements might do for you in a simple small project but in a project where many members are working or you have large number of files, you just can't keep adding these statements. It is good to have breakpoints.

I suggest you to take a look at this

Title: What are the advantages of using the Java debugger over println?

Body: I always use System.out.println(...) to debug my code, and it works pretty well. In which cases do you use the eclipse java debugger? I never had to use it and the little bug symbol is still ab bit mysterious to me. Are there cases where the debugger helps me that I can´t solve with a println?


问:
Serializing Data Structures in C
I've recently read three separate books on algorithms and data structures, tcp/ip socket programming, and programming with memory. The book about memory briefly discussed the topic of serializing data structures for the purposes of storing it to disk, or sending it across a network. I can't help but wonder why the the other two books didn't discuss serialization at all.

After an unsuccessful web/book search I'm left wondering where I can find a good book/paper/tutorial on serializing data structures in C? Where or how did you learn it?

答: Serialization is usually pretty simple and recursive. You just figure out what fields you need to send before the actual data so you can reconstruct the structure at the other end. Issues would mainly be endianness. Learn it by trial and error, I doubt you need a book for this sort of thing.</p>

Title: Serializing Data Structures in C


问:
Are there any specific workflows or design patterns that are commonly used to create large functional programming applications?
I have been exploring Clojure for a while now, although I haven't used it on any nontrivial projects. Basically, I have just been getting comfortable with the syntax and some of the idioms. Coming from an OOP background, with Clojure being the first functional language that I have looked very much into, I'm naturally not as comfortable with the functional way of doing things.

That said, are there any specific workflows or design patterns that are common with creating large functional applications? I'd really like to start using functional programming "for real", but I'm afraid that with my current lack of expertise, it would result in an epic fail.

The "Gang of Four" is such a standard for OO programmers, but is there anything similar that is more directed at the functional paradigm? Most of the resources that I have found have great programming nuggets, but they don't step back to give a broader, more architectural look.

答:
In my personal opinion, design patterns are semantic. I remember rewriting some of my old apps using MVC just to make sure I understood the pattern as well as I thought I did. But, in the end, I didn't gain anything from MVC over my original code.

However, if I were to apply my original code to a larger development environment and tell someone that there is an issue with this certain method... it would be hard for that developer to track down the problem. HOWEVER, if I said tht the ContractController was mucked up for some reason, (s)he would know exactly where to start.

Design patterns are great... but like I said, I think they are semantic!

EDIT: You evangelist types crack me up. How did anything ever get developed without MVC (or some other design pattern)!


答:
Have a look at the work Google has done with Protocol Buffers.

You write a .proto file like this:

```<code>message Person {
  required int32 id = 1;
  required string name = 2;
  optional string email = 3;
}
</code>```

Then you compile it with protoc, the protocol buffer compiler, to produce code in C++, Java, or Python.

Then, if you are using C++, you use that code like this:

```<code>Person person;
person.set_id(123);
person.set_name("Bob");
person.set_email("bob@example.com");

fstream out("person.pb", ios::out | ios::binary | ios::trunc);
person.SerializeToOstream(&out);
out.close();
</code>```

You can examine the SerializeToOstream method to understand how Google generates the serialization code.  Yes, it's C++ code, but it should still be pretty close to C code.

Title: Serializing Data Structures in C

Body:
I've recently read three separate books on algorithms and data structures, tcp/ip socket programming, and programming with memory. The book about memory briefly discussed the topic of serializing data structures for the purposes of storing it to disk, or sending it across a network. I can't help but wonder why the the other two books didn't discuss serialization at all.

After an unsuccessful web/book search I'm left wondering where I can find a good book/paper/tutorial on serializing data structures in C? Where or how did you learn it?

Body:
Have a look at the work Google has done with Protocol Buffers.

You write a .proto file like this:

message Person {
  required int32 id = 1;
  required string name = 2;
  optional string email = 3;
}

Then you compile it with protoc, the protocol buffer compiler, to produce code in C++, Java, or Python.

Then, if you are using C++, you use that code like this:

Person person;
person.set_id(123);
person.set_name("Bob");
person.set_email("bob@example.com");

fstream out("person.pb", ios::out | ios::binary | ios::trunc);
person.SerializeToOstream(&out);
out.close();

You can examine the SerializeToOstream method to understand how Google generates the serialization code.  Yes, it's C++ code, but it should still be pretty close to C code.


答:
Every case...

Granted, I'm not 100% sure about Java IDEs (and would welcome any corrections that aren't consistent with Java IDEs) because I use Visual Studio over C#/F# but adding variables to the watch list and using conditional breaks completely practically replaces println...

you don't need to rebuild to get additional information (you don't even need to restart your process/debug session)

you can remote debug a process on another machine

you can move the current execution while executing (drag the arrow that indicates the current line)

step into code/out of/over code based on whether or not you think it matters

And hell, I'm not even that great with the debugger, but it's still light years ahead of console debugging or digging through logs.


问:
Making a design for a Problem
I have written many codes using OOPS and I am still to understand when is a code good enough to be accepted by experts. The thought procedure of every man is different and so is the design. My question is should I do something in particular to design my programs in such a way that they are good enough to be accepted by people.

Other thing I have also read Head First Object Oriented Design but at last I feel that the way they design the problems is much different I would have designed them.

答:
Code is good enough when it satisfies the customer's requirements for functionality, performance and maintainability.

That's it.  If you can accomplish those things with your approaches, more power to you.

Best practices are learned by gaining experience in coding design, and studying the design practices of others.  Over time, you will discover those practices that yield consistent results, and other practices which are ultimately unproductive.

Title: Making a design for a Problem


答:
should I do something in particular to design my programs in such a way that they are good enough to be accepted by people

I don't agree with this approach. You shouldn't be designing your application for the sake of having some design patterns implemented there. Design patterns are used to solve some specific type of problems/challenges that your software project is facing.

Keeping your code as simple as possible should be the best design approach. In another words, KISS and YAGNI are the best guidelines in the design process.

As well as, Solid-development-principles should be the way to improve your application code base, as needed.

Title: Making a design for a Problem

Body:
I have written many codes using OOPS and I am still to understand when is a code good enough to be accepted by experts. The thought procedure of every man is different and so is the design. My question is should I do something in particular to design my programs in such a way that they are good enough to be accepted by people.

Other thing I have also read Head First Object Oriented Design but at last I feel that the way they design the problems is much different I would have designed them.


答:
There isn't really any information a debugger can give you that you could not gain through print statement as well, but...

it's just so much faster and more flexible. Words can't describe the power you get when you can freely look at what the code is doing while it's running.

All in all, I'd estimate that using a debugger typically speeds up the debugging process 10 times. Probably 100 times in cases where there's a lengthy build or deployment between the time where one print statement gives you information about where to next, i.e. where to add another print statement, and the time you can see the output of that new print statement. In a debugger, all it takes is a single click, maybe half a second. If you have to do a full rebuild and deploy to an app server, it could take half an hour.


答: Why would you let a consumer of your class create an object in an invalid state?  I would design the class such that if it is ever asked to enter an invalid state (either by bad constructor parameters, or asking for an invalid mutation) it throws an exception and does not change the state.</p>

Title: How to implement isValid correctly?


答:
I think it is important to be completely objective.  Attempting to boost morale by making positive comments is a waste of time to my mind.

This can mean that code reviews are unduly critical - but then isn't that the point.  We should also be critical of ourselves.  I find that the assumption that the code that I have written is probably complete rubbish and definitely could be improved drives me to improve my code and my skill level.

If you get no comments then you can consider you have done a good job.


问:
Whiteboard "testing" during an interview: legitimate way to back up your (whiteboard) code?
As I get it, having an error (even a typo like or missing ";") in your whiteboard code will often cost you some interview points. Avoiding that will inevitably make one proof-reading code again and again (losing time and possibly neural energy / concentration) or even using a simpler (and thus less effective) algorithm -- and both these ways are "costly" again!

So, why not just fast write code as elegant and effective as you would having a (unit) testing framework at your disposal and then just normally test it it (just on the whiteboard)?

Has anyone tried / seen this approach? Is the whole idea worthy?

[this also applies to the pen-and-paper case of course]

答: I've done that. At an interview I was asked to implement run-length encoding on the whiteboard, and while I short-cut some of the code (explaining what I was abbreviating) to fit the whiteboard I still came up with a collection of tests for this unit, and ran through one of them to validate my solution and show how the testing would help. I was offered that position so I assume the testing was helpful, or at worst not annoying.</p>


答:
C has no native support for serializing structures, so you're on your own.

The first order approximation is (as stated in other replies) to define it
for primitive types, and apply it recursively to larger structures.

However, there are lots of devilish details that have to be addressed beyond the
simple concept.  To name a few:

endian order of integers, and sizes of various common types of integers depending
on machine architectures.  This isn't much of a problem if all the consumers of serializatin are the same binary, but consider reading data produced by a 32 bit PPC Mac on a 64 Bit Windows machine, or if a "long" is 32 or 64 bits.

Different representations for common data types.  Color bitmaps have 3 components on a PC, but 4 components, in a different order, on Macs

Representation and precision of floating point numbers.

If strings with the same letters are idential or only similar.

dealing with cyclic or self-referential data structures.

Title: Serializing Data Structures in C


问:
Distance from point to n-dimensional line
The solution for the 3-d case can be found here; I would like to get the generalized version.  There's no simple generalization of the Mathworld algorithm since the cross product is defined only for 3 and 7 dimensions, so I understand.</p>

答:
The algorithm is to minimise the distance between the point and the line.

The line is a set of points. Write an equation to express the distance between the given point and each point in the line - it will be something like d = sqrt((a1 - b1)^2 + (a2-b2)^2 + ... + (an-bn)^2).

Now minimise that equation.

Rather than implement this algorithm yourself, I'd suggest you find a library for linear equations in your chosen language. I've heard of JAMA (for Java), but I have never needed to do this so haven't researched it.


答:
Express the line as a function of a single parameter t.  Call it X(t).

The distance from a point P to a point on the line X(t0) is just u(t) = || X(t0) - P ||, and you don't actually need to do the square root.

Now find the value of t that minimizes u(t).  The standard method from first-semester calculus is to form the derivative du/dt, set it to zero, and solve for t.

If the line is actually a straight line, you will get one solution.  If the line is a curve, you may get many solutions, and you'll have to look at all of them to find the actual minimum.

Title: Distance from point to n-dimensional line

Body: The solution for the 3-d case can be found here; I would like to get the generalized version.  There's no simple generalization of the Mathworld algorithm since the cross product is defined only for 3 and 7 dimensions, so I understand.</p>


答:
If you use vector algebra (which is easy with a vector algebra library), there is no real difference between the 3-d case and the N-d case.  Unfortunately, the page you link to has written out the vector math element by element, which tends to obscure this.

So, paraphrasing from the article:  given a line through two points A and B, the minimum distance d to a point P can be computed as:

```<code>   n_vector pa = P - A
   n_vector ba = B - A
   double t = dot(pa, ba)/dot(ba, ba)
   double d = length(pa - t * ba)
</code>```

Note that adding two n_vector's is just like adding a 3-vector, except you add N corresponding elements instead of 3 of them, and scaling an n_vector by scalar t is just like scaling a 3-vector except you scale N elements instead of 3.

Evaluating the length() of an n_vector is only slightly more complicated:  you sum up the squares of all N elements (instead of just the 3), and take the sqrt() of the result.  Finally, as you may have guessed, the dot() product is the sum of the products of the N corresponding elements (again, instead of just the 3).

Body:
If you use vector algebra (which is easy with a vector algebra library), there is no real difference between the 3-d case and the N-d case.  Unfortunately, the page you link to has written out the vector math element by element, which tends to obscure this.

So, paraphrasing from the article:  given a line through two points A and B, the minimum distance d to a point P can be computed as:

   n_vector pa = P - A
   n_vector ba = B - A
   double t = dot(pa, ba)/dot(ba, ba)
   double d = length(pa - t * ba)

Note that adding two n_vector's is just like adding a 3-vector, except you add N corresponding elements instead of 3 of them, and scaling an n_vector by scalar t is just like scaling a 3-vector except you scale N elements instead of 3.

Evaluating the length() of an n_vector is only slightly more complicated:  you sum up the squares of all N elements (instead of just the 3), and take the sqrt() of the result.  Finally, as you may have guessed, the dot() product is the sum of the products of the N corresponding elements (again, instead of just the 3).


问:
How do you stay motivated for hobby projects?
I started seriously programming as a hobbiest, student and then intern about 4 years ago and I've always done small projects on the side as a learning exercise.  Schools over now though, and I spend my days at work as a software developer. I would still love to do projects on the side to learn about areas in computer science that I'm not exposed to at work, but I've noticed that after 8 hours of starring at an IDE it's far to tempting to veg out.  Any time I do get up the gumption to work on something for a few hours lately it's gotten left by the wayside.

Anyone have any advice for sticking with side projects when you spend most of your day coding?

答:
I agree with a lot of the other comments, like picking topics you love, and working with technologies not used at work.

Other than those, the biggest things that keeps me going are leaving a broken test when I leave, so I have an easy starting point the next time, and looking at some aspect of the project everyday, even if only for a few seconds, just so the project stays fresh in my mind.


答:
Make $isValid an instance varible.

Do the error check in the constructor including populating the array.

Then isValid() function only returns $isValid.

Doing the error checking in the constructor time-decouples getErrors() from isValid()

Title: How to implement isValid correctly?


问:
Is there a difference between arguments and parameters?
It might be like this: Parameter means from the callers POV and arguments mean inside the procedure, or other way round.

Or is there no difference?

Update

In Swedish we say "anropsparametrar" i.e. "parameters that are calling the procedure" and we would not say "anropsargument" ("arguments that are calling the procedure").

答:
According to MSDN article  there are some definition differences what is called parameter and what is called argument:

Parameter:

A parameter represents a value that the procedure expects you to pass when you call it. The procedure's declaration defines its parameters.

Argument:

An argument represents the value you pass to a procedure parameter when you call the procedure. The calling code supplies the arguments when it calls the procedure.

There is also a great quote how to understand it in real-life example:

To communicate this information to the procedure, the procedure defines a parameter, and the calling code passes an argument to that parameter. You can think of the parameter as a parking place and the argument as an automobile. Just as different automobiles can park in the parking place at different times, the calling code can pass a different argument to the same parameter each time it calls the procedure.


问:
Having a generic data type for a database table column, is it "good" practice?
I'm working on a PHP project where some object (class member) may contain different data type. For example :

```<code>class Property {
    private $_id;     // (PK)
    private $_ref_id; // the object reference id (FK)
    private $_name;   // the name of the property
    private $_type;   // 'string', 'int', 'float(n,m)', 'datetime', etc.
    private $_data;   // ...

    // ..snip.. public getters/setters
}
</code>```

Now, I need to perform some persistence on these objects. Some properties may be a text data type, but nothing bigger than what a varchar may hold. Also, later on, I need to be able to perform searches and sorting.

Is it a good practice to use a single database table for this (ie. is there a non negligible performance impact)? If it's "acceptable", then what could be the data type for the data column?

答:
Is it a good practice to use a single database table for this

Whether you need 1 table or more is a factor of the dependency of the columns belonging to the table to the PK, as per normalization rule. The sample you show suggests, though not very clear, that this is the case.

You may decompose a single logical table into two physical tables if you have very large number of columns or if your data can be logically partitioned to gain performance, however, this is no longer necessary with new database systems since you can use database features such as Partitions to take care of this.

To gain performance, you need to define indexes and decide which columns should participate in each index as well as the sequence of columns in each. This is the case if you have large data with frequent access. Indexes are slower for large columns than for other types of columns such as int. Also, an index may be ignored if you perform certain LIKE queries.

what could be the data type for the data column?

It is not clear what the data column in your example will hold. However, either a VARCHAR or a TEXT data type may do. For a comparison in performance between the two types, you may want to check Text vs Varchar and dba.stackexchange-Text vs Varchar.


问:
How important is positive feedback in code reviews?
Is it important to point out the good parts of the code during a code review and the reasons why it is good? Positive feedback might be just as useful for the developer being reviewed and for the others that participate in the review.

We are doing reviews using an online tool, so developers can open reviews for their committed code and others can review their code within a given time period (e.g. 1 week). Others can comment on the code or other reviewer's comments.

Should there be a balance between positive and negative feedback?

答: Mantra is simple: If one wants quality Code (which is less in reality) then proper review methods must be practiced. Having said that, positive feedback helps a developer/programmer to think and come up with ideas/solutions/fixes. Don't be too harsh, but be firm on the point. Q&A Managers should be aware of good methodologies and practices so that he/she could guide the team (or a member) in the right direction. This results in quality. Period. </p>


问:
Plagued by multithreaded bugs
On my new team that I manage, the majority of our code is platform, TCP socket, and http networking code. All C++.  Most of it originated from other developers that have left the team. The current developers on the team are very smart, but mostly junior in terms of experience.

Our biggest problem: multi-threaded concurrency bugs. Most of our class libraries are written to be asynchronous by use of some thread pool classes. Methods on the class libraries often enqueue long running taks onto the thread pool from one thread and then the callback methods of that class get invoked on a different thread. As a result, we have a lot of edge case bugs involving incorrect threading assumptions.  This results in subtle bugs that go beyond just having critical sections and locks to guard against concurrency issues.

What makes these problems even harder is that the attempts to fix are often incorrect. Some mistakes I've observed the team attempting (or within the legacy code itself) includes something like the following:

Common mistake #1 - Fixing concurrency issue by just put a lock around the shared data, but forgetting about what happens when methods don't get called in an expected order.  Here's a very simple example:

```<code>void Foo::OnHttpRequestComplete(statuscode status)
{
    m_pBar->DoSomethingImportant(status);
}

void Foo::Shutdown()
{
    m_pBar->Cleanup();
    delete m_pBar;
    m_pBar=nullptr;
}
</code>```

So now we have a bug in which Shutdown could get called while OnHttpNetworkRequestComplete is occuring on. A tester finds the bug, captures the crash dump, and assigns the bug to a developer. He in turn fixes the bug like this.

```<code>void Foo::OnHttpRequestComplete(statuscode status)
{
    AutoLock lock(m_cs);
    m_pBar->DoSomethingImportant(status);
}

void Foo::Shutdown()
{
    AutoLock lock(m_cs);
    m_pBar->Cleanup();
    delete m_pBar;
    m_pBar=nullptr;
}
</code>```

The above fix looks good until you realize there's an even more subtle edge case. What happens if Shutdown gets called before OnHttpRequestComplete gets called back? The real world examples my team has are even more complex, and the edge cases are even harder to spot during the code review process.

Common Mistake #2 - fixing deadlock issues by blindly exiting the lock, wait for the other thread to finish, then re-enter the lock - but without handling the case that the object just got updated by the other thread!

Common Mistake #3 - Even though the objects are reference counted, the shutdown sequence "releases" it's pointer. But forgets to wait for the thread that is still running to release it's instance. As such, components are shutdown cleanly, then spurious or late callbacks are invoked on an object in an state not expecting any more calls.

There are other edge cases, but the bottom line is this:

Multithreaded programming is just plain hard, even for smart people.

As I catch these mistakes, I spend time discussing the errors with each developer on developing a more appropriate fix. But I suspect they are often confused on how to solve each issue because of the enormous amount of legacy code that the "right" fix will involve touching.

We're going to be shipping soon, and I'm sure the patches we're applying will hold for the upcoming release. Afterwards, we're going to have some time to improve the code base and refactor where needed. We won't have time to just re-write everything. And the majority of the code isn't all that bad. But I'm looking to refactor code such that threading issues can be avoided altogether.

One approach I am considering is this. For each significant platform feature, have a dedicated single thread where all events and network callbacks get marshalled onto. Similar to COM apartment threading in Windows with use of a message loop. Long blocking operations could still get dispatched to a work pool thread, but the completion callback is invoked on on the component's thread. Components could possibly even share the same thread. Then all the class libraries running inside the thread can be written under the assumption of a single threaded world.

Before I go down that path, I am also very interested if there are other standard techniques or design patterns for dealing with multithreaded issues.  And I have to emphasize - something beyond a book that describes the basics of mutexes and semaphores. What do you think?

I am also interested in any other approaches to take towards a refactoring process.  Including any of the following:

Literature or papers on design patterns around threads. Something beyond an introduction to mutexes and semaphores. We don't need massive parallelism either, just ways to design an object model so as to handle asynchronous events from other threads correctly.

Ways to diagram the threading of various components, so that it will be easy to study and evolve solutions for. (That is, a UML equivalent for discussing threads across objects and classes)

Educating your development team on the issues with multithreaded code.

What would you do?

答:
Your problems are pretty bad, but typical of poor use of C++.
Code review will fix some of these problems. 30 minutes, one set of eyeballs yeilds 90% of the results.(citation for this is googleable)

#1 Problem You need to ensure there is a strict lock hierachy to prevent your locking deadlocking.

If you replace Autolock with a wrapper and a macro you can do this.

Keep a static global map of locks created in the back of your wrapper.
You use a macro to insert the finename and line number information into the Autolock wrapper constructor.

You'll also need a static dominator graph.

Now inside lock you have to update the  dominator graph, and if you get an ordering change you assert an error and abort.

After extensive testing you may be rid of most of the latent deadlocks.

The code is left as an exercise for the student.

Problem #2 will then go away ( mostly)

Your archientctual solution is going to work. I've used it before in mission and life crtical systems. My take on it is this

Pass immutable objects or make copies of them before passing.

Don't share data via public variables or getters.

External events come in via a multithreaded dispatch in to a queue  serviced by one thread.  Now you can sort-of reason about Event handling.

Data changes that cross threads come into a thread-safe qeuue, get handled by one thread.  Make subscriptions.  Now you can sort-of reason about data flows.

If your data needs to go cross-town, publish it to the data queue.  That will copy it and pass it to the subscribers asnynchronously. Also breaks all the data dependancies in the program.

This is pretty much an actor model on the cheap. Giorgio's links will help.

Finally, your problem with shut-down objects.

When you are reference counting, you've solved 50%. The other 50% is to refernce count callbacks. Pass callback holders a refernce.
Shutdown call then has to wait for zero count on the refcount.  Doesn't solve complicated object graphs; that's getting into real garbage collection. ( Which is the motivation ins Java for not make any promises about when or if finalize() will get called; to get you out of programming that way.)

Title: Plagued by multithreaded bugs


问:
How important are unit tests in software development?
We are doing software testing by testing a lot of I/O cases, so developers and system analysts can open reviews and test for their committed code  within a given time period (e.g. 1 week).  But when it come across with extracting information from a database, how to consider the cases and the corresponding methodology to start with? </p>

<p>Although that is more likely to be a case studies because the unit-testing depends on the project we have involved which is too specific and particular most of the time.  What is the general overview of the steps and precautions for unit-testing? </p>

答:
What is the general overview of the steps and precautions for unit-testing?

Unit testing is validation and verification methodology where the developers test the individual units of source code. General steps consist from triple A methodology. Meaning having steps of "Arrange-Assert-Act”.

Main precaution with unit test is, your unit test should test a single unit of work per unit test. Once you have more than single purpose in the method, or unit of code that you are testing then purpose of unit-testing is broken at that point.

References to look:
There is a very good MSDN article on - How to: Create a Data-Driven Unit Test and good practices to follow in - Write Maintainable Unit Tests That Will Save You Time And Tears.


答:
It's not useful to talk about unit testing in a vacuum.

The usefulness of a unit test is dependent upon how well it tests an acceptance criterion a granular unit of functionality which fulfills an acceptance criterion. Also, that unit test must be readable and it must be easy to maintain because acceptance criteria are always subject to change.

Title: How important are unit tests in software development?

Body:
We are doing software testing by testing a lot of I/O cases, so developers and system analysts can open reviews and test for their committed code  within a given time period (e.g. 1 week).  But when it come across with extracting information from a database, how to consider the cases and the corresponding methodology to start with? </p>

<p>Although that is more likely to be a case studies because the unit-testing depends on the project we have involved which is too specific and particular most of the time.  What is the general overview of the steps and precautions for unit-testing? </p>


问:
Is Perforce as good at merging as DVCSs?
I've heard that Perforce is very good at merging, I'm guessing this has to do with that it tracks changes in the form of changelists where you can add differences across several files in a single blow. I think this implies Perforce gathers more metadata and therefore has more information to do smarter merging (at least smarter than Subversion, being Perforce centralized).

Since this is similar to how Mercurial and Git handle changes (I know DVCSs track content rather than files), I was wondering if somebody knew what were the subtle differences that makes Perforce better or worse than a DVCS like Mercurial or Git.

答:
Very good, especially given perforce's competition (cvs, svn, etc) over most of its life, but no, not as good as Git. I can't speak to hg.

While some of this is off-base (the p4 shelf is now an old feature, no, you can't make a dvcs behave just like a vcs, and it seems silly to knock p4 for xcode dropping support) the choice bits about p4 merging headaches are accurate.

I don't think there's anything inherent to a distributed version control system that makes its merging naturally superior to a centralized system like perforce. Rather, the data models adopted by many dvcs's lend themselves to superior branch and merge tracking.

Perforce branch mappings? A cumbersome alternative to git's pointers and directed graph approach.

One key advantage of git over perforce -- tracking code through refactoring -- has nothing to do with git's status as a distributed version control, but rather the fact that it looks at both files and blobs of content as first class citizens in its data model. This is completely different from perforce, which keeps extensive metadata in its database on a file-by-file level: file state, history, attributes, changelists, while versioned file content is stored separately in a filesystem hierarchy.

Lastly, and this may be a bit unfair but I feel that if a version control system needs to have a file locking feature, it probably doesn't bode well for its branching and merging sophistication.

For a biased comparison of git vs. perforce (archived link). I also find it telling that perforce has itself jumped on the gitwagon.


问:
What are options for 3rd Party Centralized Software Settings Management?
I am an architect in an enterprise looking to build a SaaS solution.  Our products are distributed over many different deployable containers,  Web Services, Web UI's, etc.

I am looking for some open-source or 3rd party software solution to manage the settings of our application.  These would be similar to the settings you might find in Word or Eclipse or Visual Studio.  The settings would control various behaviors and features of the product. (Probably not settings like which database to connect to but more like, should I show line numbers on the page or not by default..).  Ideally, we would be able to store values for different dimensions (by tenant, by user, by application environment... )

Because we have so many different deployables, I am looking for a centralized solution that can provide a web service that each of the deployables can get their individual settings from.

Does anyone know of a centralized service providing this sort of features or give me some help in searching for an alternative to rolling our own?

答:
Sounds like a business rules engine where you only use simple rules.
Those engines provide a library which fetches the rules from a centralised repository and caches them in an efficient

Way.
Just google for “business rules engine“ and you'll find lots of them...


问:
What types of processes have to be reflected in flowchart?
In visual programming, there are many events which support our application tasks. So, along with user defined processes, should these also be in flowchart or not?

What about the constructors of classes, are they supposed to be shown in flowchart or not?

答:
The Wikipedia article covers this pretty well.  Look under building blocks; if it's not there, a flowchart doesn't cover it.

Flowcharts are mostly meant to illustrate a process that embodies an algorithm (a recipe for solving a problem).  The flowchart illustrates the procedural steps that the computer would take to solve the problem.


答:
In short, a flowchart is a type of diagram that represents an algorithm or process, showing the steps as boxes of various kinds, and their order by connecting these with arrows.

You may also think of a flow chart as a process flow of application logic while connecting components in some type of flow of events.

What about the constructors of classes, they supposed to be shown in flowchart or not ?

Well class design is not done by means of flow chart. Generally a UML diagram is created to present class diagram structure or high-level application design.

Title: What types of processes have to be reflected in flowchart?

Body:
In visual programming, there are many events which support our application tasks. So, along with user defined processes, should these also be in flowchart or not?

What about the constructors of classes, are they supposed to be shown in flowchart or not?


问:
Why am I seeing so many instantiable classes without state?
I'm seeing a lot of instantiable classes in the C++ and Java world that don't have any state.

I really can't figure out why people do that, they could just use a namespace with free functions in C++, or a class with a private constructor and only static methods in Java.

The only benefit I can think of is that you don't have to change most of your code if you later decide that you want a different implementation in certain situations. But isn't that a case of premature design? It could be turned into a class later, when/if it becomes appropriate.

Am I getting this wrong? Is it not OOP if I don't put everything into objects (i.e. instantiated classes)? Then why are there so many utility namespaces and classes in the standard libraries of C++ and Java?

Update:
I've certainly seen a lot examples of this in my previous jobs, but I'm  struggling to find open source examples, so maybe it's not that common after all. Still, I'm wondering why people do it, and how common it is.

答:
I'm seeing a lot of instantiable classes in the C++ and Java world
  that don't have any state.

Some possibile reasons to create classes without ivars of their own:

State is or could be contained in a superclass.

Class implements some interface and needs to be instantiable so that instances can be passed to other objects.

Class is intended to be subclassed.

Handy way to group related functions. (Yes, there may be better or different ways to do the same.)

Am I getting this wrong? Is it not OOP if I don't put everything into
  objects (i.e. instantiated classes)?

OOP is a paradigm, not a law of nature. There are some languages where everything is an object, so you really don't have a choice. Other languages (e.g. C) don't provide any support for OOP at all, but you can still program in an object oriented style. I'd say you can have OOP if you don't do everything in classes... you might say that you just have less OOP in that case.


答:
Utility classes in Java and C++ are used to maintain a library of methods that take one or more input parameters, and return a result.  They have no need to hold state, if they simply return a value.  In that respect, these methods can be regarded as a crude form of functional programming, with all the advantages thereof (no problems with concurrency, for one thing).

In any case, these classes just serve to group together related methods in a single container, methods that don't require instantiation of an object to work properly.  An example of such a class is a Math class containing SINE and COSINE (and other math) functions.

Title: Why am I seeing so many instantiable classes without state?

Body:
I'm seeing a lot of instantiable classes in the C++ and Java world that don't have any state.

I really can't figure out why people do that, they could just use a namespace with free functions in C++, or a class with a private constructor and only static methods in Java.

The only benefit I can think of is that you don't have to change most of your code if you later decide that you want a different implementation in certain situations. But isn't that a case of premature design? It could be turned into a class later, when/if it becomes appropriate.

Am I getting this wrong? Is it not OOP if I don't put everything into objects (i.e. instantiated classes)? Then why are there so many utility namespaces and classes in the standard libraries of C++ and Java?

Update:
I've certainly seen a lot examples of this in my previous jobs, but I'm  struggling to find open source examples, so maybe it's not that common after all. Still, I'm wondering why people do it, and how common it is.


答:
Caleb and Robert Harvey have already pointed out what utility classes are, and some legitimate reasons for having "data-less" classes.  Those descriptions are spot-on, but deal with the positive aspects.

I would like to just mention that the abuse of utility classes can definitely be an OO anti-pattern (see c2wiki's description).  This quote sums it up nicely:

A large number of such classes (particularly those with only a single method) indicates that the designers are thinking upside-down, ie. thinking of that which can be done to an object rather than that which can done by the object.

If you are claiming to be practicing object oriented design, but your codebase consists almost entirely of utility classes, I'd say that you are definitely doing something wrong.  Having said this, a functional approach has just as many merits, and can be excellent to work with, too.  Just don't claim to be following the one, while half-heartedly implementing the other.


答: A lot of those classes do interact with data, even if they don't explicitly contain it, and need to do so in ways that require multiple simultaneous instances to be active. E.g. a stateless session bean that interacts with a database stored procedure, passing incoming data to some PL/SQL package and passing the results back to the calling application. That thing needs to exist in multiple instances, but doesn't itself contain any data fields.</p>


答:
Patterns of this kind are usually symptoms of a broken, unfit underlying model.

OOP is broken by design, unfit for the most of its applications, therefore it bursts with all that so called "patterns". Functional model is (just a bit) more flexible, and the need in "patterns" is not that obvious there.

Once you start applying a (natural for the functional programmers) language-oriented approach, using or creating DSLs for each specific problem domain, you'll find that no patterns are showing up at all, because you're always employing an adequate model for describing a problem.

Of course some recurring high level "patterns" or "recipes" are unavoidable even in the very abstract, clean and pure math, but they're of a different kind and of a different level of abstraction than the GoF patterns. You'll find monads useful, for example.

Title: Are there any specific workflows or design patterns that are commonly used to create large functional programming applications?

Body:
I have been exploring Clojure for a while now, although I haven't used it on any nontrivial projects. Basically, I have just been getting comfortable with the syntax and some of the idioms. Coming from an OOP background, with Clojure being the first functional language that I have looked very much into, I'm naturally not as comfortable with the functional way of doing things.

That said, are there any specific workflows or design patterns that are common with creating large functional applications? I'd really like to start using functional programming "for real", but I'm afraid that with my current lack of expertise, it would result in an epic fail.

The "Gang of Four" is such a standard for OO programmers, but is there anything similar that is more directed at the functional paradigm? Most of the resources that I have found have great programming nuggets, but they don't step back to give a broader, more architectural look.


问:
Criteria for selecting timeout value?
Situation: a piece of software reads frames of data from a file in a seperate thread and puts it on a queue, emptied by another thread. That second thread periodically checks on the queue and fails rather gracefully, by showing an error message stating the read timed out, if no data is available within a certain amount of time. Initially this timeout was set to 200mSec. There was no real reasoning behind that constant though, but it worked fine. We measured on a couple of machines and for large data frames, larger than what would be used by customers, a read took like 20mSec whith no other load on the machine.

However one customer now gets timeout errors now and then (on the second try all is fine, probably the file is in cache or the virus scanner leaves it alone). The programmers are like 'well, yeah, but that customer's machine is full of cruft, virus scanners, tons of unneeded background processes etc'. Of course the customer is like 'hey this should just work, shouldn't it'? While the programers have a point, since the software is heavy enough to validate the need for a dedicated machine, that does not make the customer happy.

Increasing the timeout to 2 seconds, for example, solves the problem. But I'd like to make a proper decision now instead of just randomly pick some magic constant that is probably ok in 99% of cases. What criteria should be used for that? We could just pick a large number, but that feels wrong. (and then we end up with a program that has the horrible bahaviour of hanging when trying to read from a disconnected drive for instance, whereas we'd rather make it show an error right away). Or we could make the timeout value a user setting, but then we need to ducument it clearly and even then not all customers are tech savy enough to really understand what it does. Or we could try and wait until another customer reports timeouts and increase the value again. And again. Until we find something ok for 99.99% of the cases.. Any good practice for this type of situation?

答:
It's very similar to performance non-functional requirements: how can you tell that a specific process should take 100 ms., or 500 ms., or maybe 10 seconds? The answer is the same: it's up to the stakeholders to decide, after being advised by interaction designers.

It's also the work of an interaction designer to make the application feel responsive in your case. If the timeout is increased to 2 seconds, it doesn't mean that in a case of a disconnected drive, the application should completely hang during this time. It's difficult to be more detailed, but here's an example of user experience I can imagine:

I'm doing something which starts the action you describe. This action has a timeout of two seconds.

Given that on ordinary cases, the action takes approx. 20 ms., nothing is displayed during the first 100 ms.

Let's say I experience local network connectivity issues. After those 100 ms., the application shows an animation indicating that it's currently doing something. Optionally, a cancel button may be shown too.

If the file cannot be accessed after two seconds, the application apologizes to the user, explains why it cannot work as expected and what the user may do to help the application work as expected again.

While showing this, the application checks in loop if the file is still unavailable. If the connection is back, the application informs the user that the issue is solved.

Title: Criteria for selecting timeout value?