答:
I personally prefer loosely coupled layers my code. In your case, I would have few interfaces with my implementation. However, I would also like to provide an opportunity to consumers to play around with certain things.

So, I would start with interfaces that provide data to my graph. Assuming it uses data coming in from a database, I could use a database layer that works with IDbConnection and other such interfaces. Now, I can support anything that has a provider that uses these interfaces.

Next is processing. Once data is available we need a generic way to pass it around. So, think of some more interfaces that help you do that. And your Model (class whose instance would represent data) must implement it. This enables adding and removing business rules easily.

Now, the appearance. For controls, I prefer using public events with a default handler embedded in the control itself along with properties. For instance. on click a bar in bar chart, it's color should become say red. Now a consumer likes green, so, give a property  for him to do it. He also wants a tool tip and some more details on mouse hover. So, give him a event to handle.

I understand this is way too generic and hope it is at least somewhat helpful.

Title: Separation of presentation and logic

Body:
I'm now writing a program, which will allow processing data expressed in the form of graphs. These graphs will determine workflow of the processing and they are actually an essence of this program (in terms of being the main object being edited by the user).

It is very, very tempting to put all the processing, data containment and presentation in a single control, which will also display these graphs and handle user interaction. From one side it seems to be violation of the presentation/logic rule, but from the other I'm certain I will write surely at least 2x or 3x less code, because I won't have to take care of constant synchronization between data model and the control, which will present this model (+encapsulation, interfaces, proxy classes etc.)

I'm wondering how it is done in editors like Word. Is the data displayed by the control really physically separated from the presenter or are they combined? If you use the Word document control in your program, you ask the control to perform operations on text, what would suggest, that there is no separation (but we don't know how does it work under the hood).

So the question: is separation between data presentation and logic worth it even if it requires like 2x or 3x more code to keep everything consistent and synchronized? (yes, I mean 2x or 3x).

I've described the situation in more detail in the next question: https://stackoverflow.com/questions/22373108/how-to-separate-logic-from-presentation

OwnerDisplayName: danish


问:
Separation of presentation and logic
I'm now writing a program, which will allow processing data expressed in the form of graphs. These graphs will determine workflow of the processing and they are actually an essence of this program (in terms of being the main object being edited by the user).

It is very, very tempting to put all the processing, data containment and presentation in a single control, which will also display these graphs and handle user interaction. From one side it seems to be violation of the presentation/logic rule, but from the other I'm certain I will write surely at least 2x or 3x less code, because I won't have to take care of constant synchronization between data model and the control, which will present this model (+encapsulation, interfaces, proxy classes etc.)

I'm wondering how it is done in editors like Word. Is the data displayed by the control really physically separated from the presenter or are they combined? If you use the Word document control in your program, you ask the control to perform operations on text, what would suggest, that there is no separation (but we don't know how does it work under the hood).

So the question: is separation between data presentation and logic worth it even if it requires like 2x or 3x more code to keep everything consistent and synchronized? (yes, I mean 2x or 3x).

I've described the situation in more detail in the next question: https://stackoverflow.com/questions/22373108/how-to-separate-logic-from-presentation

答:
You seem to be operating under a very strange idea of what "seperation of concerns" means.  If your design would actually call for writing twice as much code, you are doing something very, very wrong.

A multi-layer architecture works for complex line-of-business web applications because the translations between database, business logic, and DOM are so thorny and prone to migration.  For more narrow focus, such as designing a single PHP class to translate an array of numbers and a property blob object into a pretty graph, your separation can and should be tighter and more compact.

You'd probably want some seperation between your control's internal storage of the data and the interface you take from outside, but only because you're going to want some testing and verification on that.  (And as soon as you finish your "never going to need to change this" demo, you'll be asked to make a version for mobile or print or something.)

OwnerDisplayName: Spook

ContentLicense: CC BY-SA 3.0

Title: Separation of presentation and logic


答:
Separating presentation from logic is a guideline not a rule.

While is most cases MVC is a good idea, there are a few cases where it is not. Think about "paint" or "photoshop" how much sense would it make to separate the presentation from the "logic" when the whole point of the "logic" is to manipulate what is presented.

I have come across this in trading apps. Wo betide anyone who presents a trader data using anything but ye ancient Rueters terminal colors. The best place to set these colors is in the presentation layer using business rules. You can do it in the "business logic" but this means sending a ton of extra information to the presentation layer -- about presentation; either way you are breaking the "rule".

Title: Separation of presentation and logic


问:
Unit testing of non-atomic methods
I'm new in unit testing. For last two days I'm thinking how to test non-atomic methods with unit tests.
When i need to write tests for some simple, atomic method situation in pretty easy - i'm taking all scenarios for this method that i can imagine,  and reforge each of it in unit test.

But what if i have methods that are calling other methods? Should I test each scenarios for tested method AND each methods it is calling?

Maybe some example will be easier to understand:

```<code>    public int makeSomeOperation(int a, int b)
    {
        if (a < 0)
            throw new ArgumentException("a must be greater that 0");
        if (b == 0)
            throw new ArgumentException("b cannot be 0");
        return a/b;
    }

    public bool SomeNonatomicMethod(int number)
    {
        if (number * number % 2 == 1)
            return false;
        int secondNumber = getNumberFromMordor();
        int result = makeSomeOperation(number, secondNumber);
        if (result == "123")
            return true;
        return false;
    }
</code>```

In this case, makeSomeOperation is easy to test - one test for each exception, one to check in returning result is equals with expectations.

But what about SomeNonatomicMethod? Did I need to write not only test for each case in this method (so: power of number is odd, result is equal 123, and result is diffrent), but also each possible flow of things that may happen in getNumberFromMordor and makeSomeOperation?

Things that may happen in makeSomeOperation are already tested, so are they should be tested second time?

答:
Usually you would not test the "makeSomeOperation" method.  However, this is not a universal rule.  Why?  Because your composite method may not supply an adequate combination of parameters to execute all of "makeSomeOperation"'s code.

So, what you're really looking for is code coverage.  When you write unit tests, and you've got a good code coverage tool, once you get to somewhere between 80% and 90% code coverage for a large library, you're doing pretty good.  Ideally you'd get 100%, but that's almost impossible to maintain on a code base of any size.  Maybe if the code you were writing was running in a medical device or something you'd try to maintain 100%, but for we mere mortals, we try to target and maintain 80% to 90% code coverage.

Title: Unit testing of non-atomic methods


问:
At what point does data access\manipulation logic becomes business logic?
Consider a service which updates PersonDetail tables in database. I am Using EF which maps the table to this Entity. When a record is updated, it is not actually updated but a new record is created and old one is made history by setting HistoryCode to 0

```<code>Class PersonDetail
{
  int age;
  Date birthDate;
  DateTime effectiveDate;
  int sequenceNumber; //An incremental number.
  int HistoryCode; //0 represents history record and 1 current record
}
</code>```

Now we have a service that updates the person details

```<code>if(newPersonDetail.effectiveDate < currentPersonDetails.effectiveDate)
{
  //update sequence number of currentPersonDetails by 1
  //insert a new Person Details record and assign its sequenceNumber as the sequence number
  //of currentPersonDetails and HistoryCode as 0 to represent this is an history record
}
else
{
   //Make the currentPersonDetails HistoryCode to 0 from 1
   //Insert a newPersonDetail with sequence number incremented by 1 and HistoryCode set to 0
}
</code>```

Logic? I am trying to move the data-access logic out of service layer to repository layer but not able to clearly figure out what part of this is considered as business logic and what is considered as Data Access logic.

答:
Although it's not a very satisfactory answer, pretty much as soon as you introduce a condition (if, switch, etc.) into your data layer code, you're technically mingling business logic and data access.

The reason it's not very satisfying is that this implies that data access code is pretty dumb -- it handles C.R.U.D. behaviors only.  That's why ORM emerged -- it took that brain dead C.R.U.D. stuff off our plates.

So, now, you have to clarify what you mean by "data access" layer.  A lot of times systems will be somewhat sophisticated and they'll have something called "a data access layer" that's really a combination of data access and very fundamental business logic.  For example, it would make sure that you can't commit a "car" to a database without "wheels".  Technically that's business logic, but it's so low-level, that it goes along with the data access layer code (often implemented on ORM).


问:
Is it bad software design to mix JDBC SQL calls and use an ORM in an application?
Is it bad software design to have JDBC/raw SQL and also use an ORM?  I don't mind using an ORM for boilerplate CRUD, but some queries are better in SQL (for me).  I'm not arguing for or against ORMs.  I've read about them until I'm blue in the face.

I used JDBC in my question, but it could have been DBI, or some other database interface.  ORM can be any of them.

How about maintainability?  Would mixing the strategies complicate it?  To me the maintainability of an application efficiently becomes even more blurrier, if it is a compiled language vs. a scripting language.  I have to recompile Java.  I don't have to recompile Python or PHP.

If my SQL is in a model layer, does that change anything?

EDIT:  If I use find a point where I need to use SQL in my ORM, in my model, what kind of data is sent back?  I won't be able to use something like person.save('somevalue'), for example, will I?

答:
It depends.  Most of the ORM frameworks provide an "escape hatch" that lets you execute raw SQL, but what should be in the back of your mind is:  "Why am I doing this when my ORM layer is capable of recreating an entire object hierarchy on demand?"

I would say you're really stepping onto thin ice if you're doing updates outside of your ORM layer.  Queries would probably be okay if you're doing something reporting related (aggregation or joins), where the cost of building up object hierarchies is high and provides little value.  However, even then you have to think about what you're trading off.  The reason we use ORMs is so that we can think in objects, and by writing raw SQL against the underlying tables, even when it's just queries, we're writing code that will break when we change our object attributes or relationships.  Whether that's a big deal or not depends entirely on the application.

So, unfortunately the answer to your question is:  "It depends."


问:
How would you know if you've written readable and easily maintainable code?
How would one know if the code one has created is easily readable, understandable, and maintainable? Of course from the author's point of view, the code is readable and maintainable, because the author wrote it and edited it, to begin with. However, there must be an objective and quantifiable standard by which our profession can measure code.

These goals are met when one may do the following with the code without the expert advice of the original author:

It is possible to read the code and understand at a basic level the flow of logic.

It is possible to understand at a deeper level what the code is doing to include inputs, outputs, and algorithms.

Other developers can make meaningful changes to the original code such as bug fixes or refactoring.

One can write new code such as a class or module that leverages the original code.

How do we quantify or measure code quality so that we know it readable, understandable, and maintainable?

答:
I read through all the answers, and I noticed nobody mentioned code complexity.

There's a tight correlation between code complexity and readability/maintainability.  There are numerous code complexity scoring algorithms, but I'll just talk about how McCabe complexity scoring works.

Basically, McCabe scoring reads through your code and computes the number of unique "paths" there are through it.  If you use McCabe as your numerator and lines of code as your denominator, you get a pretty good approximation of "readability" as well.

If you've got 10 lines of code, and there are 300 paths through that code, that is some pretty unmaintainable code (difficult to change safely and easily), and it's probably not very readable.  Conversely, if you've got 300 lines of code, but there is only 1 path through it (it has no conditions), it's both readable and easily maintainable.

Where McCabe falls down however is in that latter example.  If I've got 300 lines of code with no conditions, there's a really good chance I've done "copy/paste reuse", and obviously that's not a good thing either.  So there are system-wide metrics that you apply in addition to McCabe -- like duplicate or near-duplicate code detection.


问:
Create Database To Fit Data vs Make Data Fit The Database
I'm newish to programming and would like to know what people have found to be the most supportable option or rather what people find more understandable.  I'm the only one writing code at my firm, but gradually will hand this off to someone else in the future.

The Project

The project I'm working on is parsing a huge text file that comes in once a day.  The data file uses previous day's data file as a reference so each data file builds upon the previous data file. Using documentation I'm able to parse the text file and populate a MySQL database.  Once the text file has been read, the data in the database is manipulated and then dumped into many Excel workbooks.  The text file has a huge amount of data and I only need a fraction of it for the Excel workbooks.

My Question

My question is what is the proper structure for this program?  I see that I have two options.

Is it better to construct a database that fits the data.  So create a database with more tables and fields then is actually needed and possibly a little cryptic in it's layout.

Is it better to manipulate the data while it's being parsed and put it into more readable, human database?

What I've Done So Far

When I wrote the first version of this program, I went for option 2.  Since I didn't know much about relational databases at the time and wanted it very easy for me to understand, visualize, etc.  However, now that I'm looking to expand the data that I parse from the text file and since each data file builds on the previous day's data file I'm leaning towards option 1 so later down the line I won't have to spend a day reconstructing the database.

Does this make sense?  Please comment if it doesn't. Any help would be greatly appreciated.

thanks,
Justin

Update

Let me start by saying the answers so far have been very helpful.  Thank you. After reading some comments though I find that I have been a bit vague in my question.  Let me try to be a bit clearer.  I'm dealing with financial data of clients.  Each account has its own Excel workbook that is used by the financial advisors to make projections and investment decisions.  Additional worksheets in the the workbook are used to display the account's holdings and projected future to the client.  The data that I get in each data file is separated by a record.  Each record has a type...there are something like 13 different record types.  Each record type is marked by a particular letter or number.  Then after that letter or number there are a specific number of characters that represent that particular record.  The record layout differs per record type and there is redundant information between record types which makes it additionally aggravating.  Another point of contention is that if the financial advisor buys a new security, one that s/he hasn't bought before then the next business day, only in that day's data file, will the security's information show up (such as the name, security type, etc) and not again unless there's a change in that security.  So if I ever need to rebuild the database I would have to start from day one.

So I'm taking this data file, parsing it and writing it to a MySQL database separated into about four tables.  I use EF6 to work with the database, and so that was a reason I started with a simple, readable fields and tables.  Hope this expanded explanation helps.

答:
It sounds very much like you're doing an ETL project.  You've got all three attributes -- you've got your extract (pulling data out of the incoming file), transform (merging the data with yesterday's file), and you've got a load into mySQL.  In fact, you've even got reporting (your Excel stuff).

There are whole companies (like Informatica) that have built products around making what you're doing easy.  If this is a one-off kind of thing that you see no signs of ever growing into more and more data sources, then by all means you should follow the suggestions of others in this thread -- simpler is better.  However, if this is just the first of many such projects, you really owe it to yourself to go learn about data warehousing, ETL, reporting -- Business Intelligence in general.  Most warehouses start out as little one-off type projects like yours, and then they grow and grow until your little custom programs get completely unwieldy and unmanageable from an operations/run perspective.  At that point companies start talking about "getting serious" about their warehouse, which means they start standardizing the processes for doing what you're doing (having a drop box, having a staging layer, having a standard/foundation layer, having defined places for doing loads vs. leaving things in files, etc.)


答:
When dealing with the data access layer code, you can think of the following:

Think of a persister, something that does operations on the data in the underlying persistence layer, such as save, delete, and update operations.

POJOs that represent the records in the tables are part of the DAL.

Every thing that depends on and queries that layer directly in order to use the persisted objects in operations can be classified as BL.

This should be a one way relation of course, BLL layer should depend on DAL and not the opposite.

Now the second code snippet you provided

```<code>if(newPersonDetail.effectiveDate < currentPersonDetails.effectiveDate)
{
  //update sequence number of currentPersonDetails by 1
  //insert a new Person Details record and assign its sequenceNumber as the sequence number
  //of currentPersonDetails and HistoryCode as 0 to represent this is an history record
}
else
{
   //Make the currentPersonDetails HistoryCode to 0 from 1
   //Insert a newPersonDetail with sequence number incremented by 1 and HistoryCode set to 0
}
</code>```

can be classified as BL if its true in some cases, but can be pushed to the DAL in case this is true whenever you are updating a record.

In some other cases, BL deals directly with the persistence layer for performance reasons, but this depends on what the application does.

Title: At what point does data access\manipulation logic becomes business logic?

Body:
Consider a service which updates PersonDetail tables in database. I am Using EF which maps the table to this Entity. When a record is updated, it is not actually updated but a new record is created and old one is made history by setting HistoryCode to 0

```<code>Class PersonDetail
{
  int age;
  Date birthDate;
  DateTime effectiveDate;
  int sequenceNumber; //An incremental number.
  int HistoryCode; //0 represents history record and 1 current record
}
</code>```

Now we have a service that updates the person details

```<code>if(newPersonDetail.effectiveDate < currentPersonDetails.effectiveDate)
{
  //update sequence number of currentPersonDetails by 1
  //insert a new Person Details record and assign its sequenceNumber as the sequence number
  //of currentPersonDetails and HistoryCode as 0 to represent this is an history record
}
else
{
   //Make the currentPersonDetails HistoryCode to 0 from 1
   //Insert a newPersonDetail with sequence number incremented by 1 and HistoryCode set to 0
}
</code>```

Logic? I am trying to move the data-access logic out of service layer to repository layer but not able to clearly figure out what part of this is considered as business logic and what is considered as Data Access logic.

Body:
When dealing with the data access layer code, you can think of the following:

Think of a persister, something that does operations on the data in the underlying persistence layer, such as save, delete, and update operations.

POJOs that represent the records in the tables are part of the DAL.

Every thing that depends on and queries that layer directly in order to use the persisted objects in operations can be classified as BL.

This should be a one way relation of course, BLL layer should depend on DAL and not the opposite.

Now the second code snippet you provided

if(newPersonDetail.effectiveDate < currentPersonDetails.effectiveDate)
{
  //update sequence number of currentPersonDetails by 1
  //insert a new Person Details record and assign its sequenceNumber as the sequence number
  //of currentPersonDetails and HistoryCode as 0 to represent this is an history record
}
else
{
   //Make the currentPersonDetails HistoryCode to 0 from 1
   //Insert a newPersonDetail with sequence number incremented by 1 and HistoryCode set to 0
}

can be classified as BL if its true in some cases, but can be pushed to the DAL in case this is true whenever you are updating a record.

In some other cases, BL deals directly with the persistence layer for performance reasons, but this depends on what the application does.


问:
What would you add in this Software Development Project Checklist?
I'm a big fan of checklists. There is Travel Checklist, Moving Checklist and even a Scrum Checklist.

Context: you have been hired by a large corporation and given the mission to setup the whole software development environment, processes, team, etc. You have "carte blanche". You will be responsible for the creation of the working increments of the software. Project size: 2000 man/days.

What items would you add to the following (intentionally small and incomplete) checklist:

Install a Continuous Integration Server

Write a DoD

Write a one page coding guidelines

Create a product backlog

Install a Bug Tracking System

Schedule Regular Face Time

答:
A large cost to organisations is not assigning budget to security throughout the development lifecycle, this means security usually ends up being in after the fact ineffective, expensive set of activities or controls put in place too late to do much good.

Get security built in from initial project plan, with key milestones, the same as you would with all other aspects of development, and use an iterative process to keep security guidance up to date. Final signoff from security should ten be a no-surprises check that all security controls were implemented as per design.

Otherwise you'll end up running security after implementation - where it could cost 8 - 10 times as much (figures from Gartner, IBM and others), will upset people as functionality is likely to be impacted and it may be too late to prevent exploitation and damage.

ContentLicense: CC BY-SA 2.5


问:
Developing a feature which sole purpose to be taken out?
What is the name of the pattern in which individual contributors (programmers/designers) developed an artifact for the sole purpose is to serve as a diversion so that management can remove that feature in the final product?

This is a folklore I heard from an ex-colleague who used to work at a large game development company. At that company, it is well known that middle management is pressurized to "give inputs" and "make changes" to the product otherwise they risk being seen as not contributing to the project. This situation have delayed many projects because of these superfluous "management inputs".

In one project at the above company, the artists and developers created a supernumerary animated character that appears in every cutscene and sticks out like a sore thumb. They designed it in such a way that it can be easily removed before the game is shipped (this was when games were still sold in physical media and not a downloadable product). Obviously the management then voted to remove the animation. On the positive side, management didn't introduced any unnecessary changes that would have delayed the project because they have shown that they provided constructive inputs to the product.

This process pattern has a name among game programmers that work in corporates, but I forgot what was the actual name. I believe it's duck-something.  Anybody can help pointing out the name and perhaps some rather credible reference to how the pattern develops?.

答:
My boss called it the "fountain strategy". He designed a new computer wing for a University with a massive fountain out the front. The wing was approved but without the fountain, exactly as planned.

That was 50 years ago, so this is nothing new.


问:
Why isn't java used as a build language?
If Java is a general purpose language, and building a program is something that can be described using the Java language, why isn't this the best way to write build files and instead we use tools like Ant, Maven, and Gradle? Wouldn't that be more straightforward, and also remove the need to learn yet another programming language?
(BTW - this question can also be applied to other languages, like C#)</p>

答:
If you look at the features of a typical build system you find:

Lots of data: names, switches, settings, configuration items, strings, etc

Lots of interaction with the environment: commands, environment variables

A relatively straightforward "build engine" handling dependencies, threading, logging etc.

If you set out to write a series of build files using some language (Java/C#/Python/etc), by about the third or fourth iteration you would settle on (a) keeping most of the data and external commands as data in something like XML (b) writing the "build engine" in your favorite language.

You would also find it useful to treat some of the data in your XML as an interpreted language, to trigger various features in the build engine. You might also interpret some macros or perform string substitutions, in the data.

In other words, you would finish up with Make, or Ant, or Rake, or MsBuild. An imperative language for the things it does well, and data structures to describe what you want to do, now usually in XML.


问:
How to design a scriptable communication emulator?
Requirement:
We need a tool that simulates a hardware device that communicates via RS232 or TCP/IP to allow us to test our main application which will communicate with the device.

Current flow:

User loads script

Parse script into commands

User runs script

Execute commands

Script / commands (simplified for discussion):

Connect RS232 => RS232ConnectCommand

Connect TCP/IP => TcpIpConnectCommand

Send data => SendCommand

Receive data => ReceiveCommand

Disconnect => DisconnectCommand

All commands implement the ICommand interface. The command runner simply executes a sequence of ICommand implementations sequentially thus ICommand must have an Execute exposure, pseudo code:

void Execute(ICommunicator context)

The Execute method takes a context argument which allows the command implementations to execute what they need to do. For instance SendCommand will call context.Send, etc.

The problem
RS232ConnectCommand and TcpIpConnectCommand needs to instantiate the context to be used by subsequent commands. How do you handle this elegantly?

Solution 1:
Change ICommand Execute method to:

ICommunicator Execute(ICommunicator context)

While it will work it seems like a code smell. All commands now need to return the context which for all commands except the connection ones will be the same context that is passed in.

Solution 2:
Create an ICommunicatorWrapper (ICommunicationBroker?) which follows the decorator pattern and decorates ICommunicator. It introduces a new exposure:

void SetCommunicator(ICommunicator communicator)

And ICommand is changed to use the wrapper:

void Execute(ICommunicationWrapper context)

Seems like a cleaner solution.

Question

Is this a good design? Am I on the right track?

答:
While I am mostly ambivalent about your two design patterns above, I would tend to lean more toward solution 1. Some of my reasoning is thus:

You have a clear separation of concerns using Execute (or any command) with a parameter of the communications context. It shows that each Execute has a dependency on a context that it does not own or manage. What you do in the Execute interacts with the communications context in the same fashion (i.e., send characters, receive characters, pause transmission, etc.). This will show you what interface your communications context requires. You can reuse the object with a different communications context just by calling it again with a different object.

You can swap out your communications context for anything else that supports the interface. There's no harm if your object supports more functionality, but your object must support the minimums. It also means that if you want to add any other communications protocol (USB, UDP, eSATA, SCSI) at a later date, it should be very simple.

You can create a factory method for each of the communication contexts. This will help for contexts that can be multi-threaded as you can start a separate thread for each instance as a part of spinning up the instance. For those contexts that don't support multi-threading (e.g., Serial), you can also spin up just one instance per serial port. The factory could keep track of the "pool" of available serial ports so you don't reuse one already in use.

Lastly, this follows a very widely known and supported methodology for implementation. It is (IMO) a much more maintainable implementation.


答:
A number of factors count against using Java/C++/C# in these cases.

Firstly, you'd have to compile your build script before you could run it to build your app. How would you specify any packages, flags, compiler versions, tool paths needed to build your build script? Certainly, you could come up with a way around it, but its much more straightforward to have either have a language that doesn't need that build step (e.g. python) or a language that your build tool natively understands.

Secondly, build files are data heavy whereas Java/C++/C# are much more geared towards writing code and algorithms. Java and friends don't have a very succinct representation of all the data you'd want to store.

Thirdly, Java and friends need a lot of boilerplate to be valid. The build file would have to be inside a method inside a class with all of its imports. When using a scripting language or custom language you can avoid all that boilerplate and just have the build details themselves.

Title: Why isn't java used as a build language?

Body:
If Java is a general purpose language, and building a program is something that can be described using the Java language, why isn't this the best way to write build files and instead we use tools like Ant, Maven, and Gradle? Wouldn't that be more straightforward, and also remove the need to learn yet another programming language?
(BTW - this question can also be applied to other languages, like C#)</p>


问:
How can I explain this is an anti-pattern?
I recently started at a new job.

The existing system works OK but is poorly designed and hard to maintain, and they are planning to rebuild it in MVC and I fear it will be much worse. (Not because of MVC)
I want to discourage the lead dev from using an anti-pattern

Existing issues include

Hard coded strings in the business logic which are also tied to the interface (e.g. if (x.CustomerType.Contains("Shipping Customer") foobar(); is everywhere

Some classes map to dbTables directly (which is good). But because the software
has to be "easy to add features to ", there is a Sharepoint-esque "DynamicData" table for storing new fields that the customer may require. a la [FieldName] [FieldValue] [FieldType]

Refusal to use Linq but chains lamda queries for about 2 page widths.

Exceptions not explicitly thrown or rethrown, but not handled properly (the client can see the call stack because it is just dumped into a popup dialog)

Refusal to use var but loves the idea of dynamic everything.

What is the best way to go about convincing my lead dev to avoid these horrible methods and not put everything in the generic key-value-pair table in version 2.0?
(Considering I am new here and the team lead has been here for many years)

答:
What you are really talking about are not really anti-patterns, but smells. Your question covers multiple completely separate issues and if you google enough, you should find out enough material to argument against your boss. There is also problem of persuading your boss that those are actual problems and need to be addressed. Now, for your problems:

Having "xxxType" in a class is clear code smell and implies that there should be some kind of object hierarchy. Refactoring is highly encouraged to create good OOP design. If the class can have multiple "types", then Strategy Pattern is good fit.

This is commonly called Entity-attribute-value model and it has it's merits and problems. It is up to you and your boss to figure out if it fits your application.

Note that lambda chains are too part of LINQ. And while using LINQ query syntax might create more readable code, some prefer not to use it.

There is tons of discussion on how exception handling should be done. Google is your friend.

var and dynamic are two completely different things. And when one uses dynamic, the person should be aware of the downsides it brings. Again, Google should help.


答:
The only way of convincing your team lead to change something is to talk to him. For lots of the problems I guess you don't have to collect "big arguments" why the code should not look like this - most devs know that such code is not clean, but often people just don't care, or go with an attitude of "don't have the time now, will fix this later" (which obviously never happens).

Bad code will only become better when the team has a common understanding of code quality, and the team members are willing to improve this. To get to a common understanding, the team needs to do daily code reviews and discuss the individual issues. And when there are no code reviews now, perhaps the start of the new "V2.0" can be a good occasion to introduce such reviews. Why not suggest this to your team lead?

Title: How can I explain this is an anti-pattern?

Body:
I recently started at a new job.

The existing system works OK but is poorly designed and hard to maintain, and they are planning to rebuild it in MVC and I fear it will be much worse. (Not because of MVC)
I want to discourage the lead dev from using an anti-pattern

Existing issues include

Hard coded strings in the business logic which are also tied to the interface (e.g. if (x.CustomerType.Contains("Shipping Customer") foobar(); is everywhere

Some classes map to dbTables directly (which is good). But because the software
has to be "easy to add features to ", there is a Sharepoint-esque "DynamicData" table for storing new fields that the customer may require. a la [FieldName] [FieldValue] [FieldType]

Refusal to use Linq but chains lamda queries for about 2 page widths.

Exceptions not explicitly thrown or rethrown, but not handled properly (the client can see the call stack because it is just dumped into a popup dialog)

Refusal to use var but loves the idea of dynamic everything.

What is the best way to go about convincing my lead dev to avoid these horrible methods and not put everything in the generic key-value-pair table in version 2.0?
(Considering I am new here and the team lead has been here for many years)


问:
Left and Right most Derivation
So i understand the semantics of derivations as far as Backus Naur Form goes. One thing I cannot find in  any text book or the various lecturers' notes that are on-line is this.

When would a right most derivation be beneficial over a left most derivation? Would a compiler or generator always use either left, or right, or would it use both?

So here's an example:

Suppose we had the following language sentence: A = B + C * A

Example Language Grammar Permitting the Above:

```<code><assign>  <id> = <expr>
<id>  A | B | C
<expr>  <expr> + <term>
        |<term>
<term> <term> * <factor>
       |<factor>
<factor>  ( expr )
        | <id>
</code>```

Left Most Derivation:

```<code><assign>   => <id> = <expr>
              => A = <expr> + <term>
              => A = <term> + <term>
              => A = <factor> + <term>
              => A = <id> + <term>
              => A = B + <term>
              => A = B + <term> * <factor>
              => A = B + <factor> * <factor>
              => A = B + <id> * <factor>
              => A = B + C * < term>
              => A = B + C * <factor>
              => A = B + C * <id>
              => A = B + C * A
</code>```

Right Most Derivation:

```<code><assign>  => <id> = <expr>
      => <id> = <expr> + <term>
      => <id> = <expr> + <term>   * <factor>
      => <id> = <expr> + <term> * <id>
      => <id> = <expr> + <term> * A
      => <id> = <expr> + <factor> * A
      => <id> = <expr> + <id> * A
      => <id> = <expr> + C * A
      => <id> = <term> + C * A
      => <id> = <id> + C * A
      => <id> = B + C * A
      =>  A = B + C * C
</code>```

答:
On their own, both left-most and right-most derivations are nothing but arbitrary rules that disambiguate which steps to take when parsing or generating with a CFG: many different orders of nonterminal expansion are imaginable which ultimately lead to the same tree, and LMD and RMD are simply two simple strategies to decide which route to take.

For computers, it is vital to have such arbitrary rules because they cannot make decisions on their own.  All they can do is either follow some strict rule, or try all possibilities and back-track as necessary. Back-tracking amounts to a search of the possibility space, which is wasteful if a simple rule would also do the trick. Virtually all programming languages are defined so that you only need a deterministic parser, and often only a deterministic parser with very limited look-ahead scope.

Title: Left and Right most Derivation


问:
How to find number of points with same minimal distances on matrix
I'm trying to find the number of points in a matrix with the same minimal distances.

Start with a MxN matrix, where M and N < 50000.  There is given a set of fixed points, with their respective coordinates.

The problem is to find the number of points in the matrix such as the minimal distance from any point in the set can be reached for at least two point in the set. The distance is measured by moving one units a time horizonatally or vertically. You can't move diagonally.

An example would make things clearer.

Let's say we have 3 fixed point: (1,3), (3,1) and (3,6). Here the point (3,3) would be one point since the minimal distance is 2 units and it can be reached for the first two fixed points in the set. However, the point (4,4) doesn't satisfy the requirements. Although the distance from (1,3) and (3,1) is same, 4 units, the minimal distance is 3 units.

My idea was to use any point with integer coordinates that lies on the bisector of a segment joining each par as a point that would fill our requirements. But I find a lot of false positives were a point satisfies the requirements, but doesn't lie on the perpendicular bisector.

Brute-force methods won't work, because we need too check a billion of distance for each point and that one heck of a job to do.

How should I go about solving this problem?

答:
You can easily see that if a point you are searching for it's at the same distance from two or more points, then they must all be at a distance such that d(a,b) is even.

Given this, you can iterate over all the pairs {a,b} of fixed points, and add to your set of result points all the matrix points that are half the distance d(a,b) from both the points you have chosen.
To do it, consider the two "circles" (in your geometry it's a tilted square) of radius d(a,b)/2 and center a and b. All the matrix points that belong to both circles are possibly good points for you.

Now you have to check if every point you found at the previous step are actually good or false positives.
To do so, just iterate over your result set and check the distances from all the fixed points.

The algorithm should be quite fast given the fact you have at most 10 fixed points


答:
The way it was explained in my compiler class, well over thirty years ago, is that a leftmost derivation scheme works best with a top-down (e.g. recursive descent) parser, while a rightmost derivation scheme works best with a bottom-up (e.g., SLR(1), LALR(1), YACC et al) parser.  Using a leftmost derivation scheme with a bottom-up parser requires the parser to "stack up" the entire sentence, then process it from the right end, rather than recognizing short phrases at a time.  Ditto the opposite pairing.

My Green Dragon Book and Red Dragon Book are currently in storage, or I'd go looking to see if they have examples or better explanations.

After thinking some more about it, the above discussion is about left recursive and right recursive grammars, but I think the effect is the same.  At the time, I found the result very satisfying, in that it said there wasn't one "best" way to do a grammar, but rather it depended on how your parser worked as well.

Title: Left and Right most Derivation

Body:
So i understand the semantics of derivations as far as Backus Naur Form goes. One thing I cannot find in  any text book or the various lecturers' notes that are on-line is this.

When would a right most derivation be beneficial over a left most derivation? Would a compiler or generator always use either left, or right, or would it use both?

So here's an example:

Suppose we had the following language sentence: A = B + C * A

Example Language Grammar Permitting the Above:

```<code><assign>  <id> = <expr>
<id>  A | B | C
<expr>  <expr> + <term>
        |<term>
<term> <term> * <factor>
       |<factor>
<factor>  ( expr )
        | <id>
</code>```

Left Most Derivation:

```<code><assign>   => <id> = <expr>
              => A = <expr> + <term>
              => A = <term> + <term>
              => A = <factor> + <term>
              => A = <id> + <term>
              => A = B + <term>
              => A = B + <term> * <factor>
              => A = B + <factor> * <factor>
              => A = B + <id> * <factor>
              => A = B + C * < term>
              => A = B + C * <factor>
              => A = B + C * <id>
              => A = B + C * A
</code>```

Right Most Derivation:

```<code><assign>  => <id> = <expr>
      => <id> = <expr> + <term>
      => <id> = <expr> + <term>   * <factor>
      => <id> = <expr> + <term> * <id>
      => <id> = <expr> + <term> * A
      => <id> = <expr> + <factor> * A
      => <id> = <expr> + <id> * A
      => <id> = <expr> + C * A
      => <id> = <term> + C * A
      => <id> = <id> + C * A
      => <id> = B + C * A
      =>  A = B + C * C
</code>```


问:
In-house SMS (using a SIM card) receiving and sending?
My needs are very simple but I don't know the terms I should be Googling for: I need to occasionally send a SMS to a machine and that machine is going to answer with another SMS to the number that did send the first SMS.

I've got no say as to how that machine works: it's closed and all it does is receive SMS and send SMS in reply to SMS received (and uniquely to the number that did send the first SMS) and there's no other way to interface with it.

Upon receiving the "response SMS", I need to update a server / DB (a regular Java webapp server + SQL DB) with the infos contained in the SMS.

There isn't going to be a lot of volume (only a few SMSes daily) and robustness isn't that important (a few SMSes can be missed).

What would be a possible architecture for something like that?

I was thinking about using two or three cheap smartphones and giving them SIM cards and then program them to regularly (once every height hours or so) send an SMS.  Then these smartphones would receive an SMS back from the machine they contacted and I'd intercept that SMS and update my server/DB accordingly. (I'll have physical control on these smartphones so giving any permission needed to any app won't be an issue).

Is this something easily doable?  Technically, if such a solution could work, how can I access the SMS functionalities of the phone?  And how can I have the phone udpate my server/DB?  (the phone can be hooked to the Internet, so I take it I can simply do an HTTP POST to my server).  Or is there already some (preferrably free or open-source) offering a similar functionality? (maybe something not totally unlike what "zapier" of "ifttt" does, where I could create a rule saying: "If I receive a SMS containing the word xxx, then send the SMS using an HTTP POST to the URL yyy").

Note that I did configure the server(s) hosting the Webapp myself and develop the entire webapp myself: so the "programming" part ain't that much of a problem.  My issue is that I don't know how to "create a bridge" between the SMS coming in response to another SMS and my webserver.

I hope I explained the issue simply enough: basically I need some guidance on the architecture to use here (which fits programmers.stackexchange as far as I can tell from just having read its FAQ).

答:
For your issue I suggest to you to create a routing rule in your SMS gateway. The routing rules help you to create a SMS route (inbound, outbound). With this route you can control the SMS messages you wish to receive / send.

You can find detailed information about routing rules on this website:
http://www.ozekisms.com/index.php?owpn=138&info=configuration/inbound-routing


问:
Dividing database between MySQL and NoSQL
Mine is a typical website developed with LAMP stack. Due to scalability issues we are planning to incorporate NoSQL for the application.

For the implementation, we are planning to implement hybrid solution: using NoSQL only for few of the high volume tables in database and keep on using MySQL for all the other. So, we are splitting the database in two with two different servers which use different database technology.

We are weighing pros and cons for this approach. For example, with this approach we will have to depend on the web application to maintain data integrity. There will be 2 points of failure due to 2 DB servers, instead of one.

Is it a usual practice? What are the pros and cons?

答:
Your best bet to improve scalability is to focus on removing calls to the database, rather than to remove data from the database.  (although that may happen   with things like session data)

If your web application is like most I've seen, a single page load triggers multiple HTTP requests.  Each of these requests is likely to trigger a session data retrieval, user details retrieval and then also hit the database for the data for the information to respond to the request.

There are some obvious things that can be done here.

Session data is a obvious thing to move into a NoSQL database.  Its a straight Key/Value operation, and something like Riak is good place to store this data.

Database data such as user details that are read often, but changed rarely are also a good match for storing in Riak.  How you handle the race condition when updates occur will vary depending on your ability to tolerate using stale data.

Likewise, your website probably also has lots of other data that it processes into HTML sections that do not vary from user to user.  Implementing a caching mechanism to store the outcome of this processing can also substantially reduce the hits on your database.

In the end though, you will find that some tables will contain some critical information that cannot be cached.  When you reach this point, your choices are to either redesign your core data structure (which is probably not possible, you you would already have done it) or to throw money (and hardware) to enable the database to keep running.  In one environment i've worked with, this meant a MS SQL server with 128 cores with the fastest disk and max memory money could buy.


问:
How can you estimate time for tasks which primarily consist of figuring out a problem?
While it is relatively possible for an experienced developer to estimate how long it will take to implement code when the pattern and problem the code is solving is well understood, how can you make a good estimate when, while the end goal is well understood, the implementation is 95% theoretical/problem solving and has very small amounts of implementation?

My work often consists of tasks to accomplish well defined goals, however I have to find the way to reach that goal and until I understand the solution, it i6sn't clear what additional barriers there may be.  More specifically, I'm often working on code generation or automated code manipulation tools.  Once the solution is fully solved and the tool perfected, it will directly do 95% of the actual changes very quickly.  I don't, however, have any way to estimate how many additional issues may have to be resolved to make the generation or analysis tool deal with unforeseeable edge cases.

For planning purposes, my company wants a better idea of how long it will take, but since I don't know how many additional problems may come up while working through solving each step of the solution.  I'm not sure how I can approach giving a better estimate.

答:
I would suggest you to try something midway between the answers of tylerl and MichaelT with the following:

split the work to be done in 3 or 4 phases. The phases should be:

Problem analysis

Solution prototyping

Real world solution

Output assessment (test)

provide an estimate only for phase 1 (analysis) based on your experience, or on phases 1+2 (analysis + prototype) to your management. Then, provide them with an estimate for phases 3+4 when the problem phases 1 and 2 are done (or at least advanced enough so that you can have confidence in your estimate).

The rationale behind is that you know by experience that you need X days to analyze a given codebase (probably depending on its size) and have a suit of basic tools or scripts running (and maybe failing). Then, the number of errors should provide you some information about the actual difficulty of the task at hand.

This may not be exactly what the management wants, but I believe it's always better to come up with estimates that you will actually meet.


答:
As this question is primarily about research types of work, asking software developers is a brave approach, a common metric is that software developer that takes twice as long as their estimate is probably a good developer.  However, that said, research (and architecture design) tasks are very much a part of programming, and are often skipped / minimised.  They are also often hard to estimate.

The first question I would ask myself, is this a problem that can be solved?  This is not an intellect or a brain power issue, but one of practical reality.  Unless you are in the world of Google moon shots, where failure is an expected outcome, the hard reality is that I will be expected to deliver this, whatever this turns out to be. A rough rule of thumb seems to be do we already know what 90% of the solution needs to be?

The second question I would ask, what else would be useful to know in thinking about the solution?  This is really a way of double checking on do we really know enough to come up with a solution that will be acceptable.  It may generate a series of fact finding tasks that help to better define what the solution needs to be, each of which are usually quite easy to define and estimate.

The third question is, who is best suited on the team for this kind of problem?  Whoever gets this task will flavour the outcome with their own style.  Giving this kind of a problem to a programmer that has 10 million questions at the start of a task, and then goes away and delivers something first time (albeit slowly) may well be a better choice than giving it the programmer that bangs out implementation quickly, but when there's a problem, its only discovered at the end of the process.

Then, the actual task would be to think about possible solutions, implementations and approaches, and to have a fixed time scale in which they need to report back.

When they report back, you then have a choice about getting a wider set of possible solutions, giving the go ahead on implementing a solution, or reflecting, as the solution is still not defined clearly enough

Title: How can you estimate time for tasks which primarily consist of figuring out a problem?

Body:
While it is relatively possible for an experienced developer to estimate how long it will take to implement code when the pattern and problem the code is solving is well understood, how can you make a good estimate when, while the end goal is well understood, the implementation is 95% theoretical/problem solving and has very small amounts of implementation?

My work often consists of tasks to accomplish well defined goals, however I have to find the way to reach that goal and until I understand the solution, it i6sn't clear what additional barriers there may be.  More specifically, I'm often working on code generation or automated code manipulation tools.  Once the solution is fully solved and the tool perfected, it will directly do 95% of the actual changes very quickly.  I don't, however, have any way to estimate how many additional issues may have to be resolved to make the generation or analysis tool deal with unforeseeable edge cases.

For planning purposes, my company wants a better idea of how long it will take, but since I don't know how many additional problems may come up while working through solving each step of the solution.  I'm not sure how I can approach giving a better estimate.


答:
The biggest smells here are probably MVC and rebuild. It sounds like a stab at achieving buzzword compliance, possibly with the misplaced belief that MVC will sort all the current issues. And on top of that there is of course the whole rewrite from scratch fallacy.

Schema-free has its place, and it sounds like a reasonable choice for your application, but you might want to consider a database that is designed for that use.

As for changing the plans, have you talked to the other developers? A single man's concerns tend not to weigh that hard, especially when that man is the new guy. But if some of the other developers voice similar concerns it becomes a lot harder not to listen. If you can't get any allies there is probably not a lot you can do it.


答:
Specific Tool for a Specific Purpose

Verbosity

General purpose languages are often too verbose. If I had to manage a build in Java, I'd be very depressed very quickly by the size of the beast. However, it could easily be manageable using a DSL written in Java. And to some extent that's how you can see Gradle (for Groovy) and Buildr (for Ruby).

Difficulty

General purpose languages are hard. Well I don't think programming is hard and can't be picked up by anyone, but the point is: your build engineer isn't necessarily your programmer!

Purpose

That's more or less at the intersection of difficulty and verbosity. A language is designed for a purpose, and here we're talking about something very specific. So why would you need or want to use a general purpose language? For builds, you need:

branches and conditionals to handle separate configurations, environments, etc...

a set of instructions to extract data from your environment,

a set of instructions to produce deliverables.

You don't really need much more.

Sure it's annoying when your build system seems like it's not flexible enough for that one special use case you're after, but it's probably far better than the alternative for most people.

That's probably why there's a polarity between people who prefer declarative build systems over most programmable onces: I guess a developer might have a natural tendency to look for ways to break out of the box.

Wait, Do We Really Need a Tool?

Another related question would be: do we really need a build tool? Isn't the fact that they exist in all languages the sign that they fill a gap that shouldn't even be there in the first place?

Some languages don't necessarily require a build tool. For instance, most scripting languages don't need one and resolve at load time. Or take Go, whose compiler will handle everything for you, which is a nice counterpoint: what if a compiler like gcc suddenly didn't need a bunch of flags, a linker, and a makefile to die everything together? Or if javac didn't need a build.xml or pom.xml to tell him what to do? Shouldn't dependency management directly be part of the language's own tooling, as the dependencies are a part of the final program?

It surely seems like a much simpler approach for the user (the builder). Though one could argue they're just doing in under the hood and taking away your choice and opportunities to influence that build process (but then you'd hope such a tool allows compiler extensions and similar things). Plus we used to see the tools and the language as two separate things, so he might seem unpure to suddenly have them so tightly coupled.

I don't think the language you use to build your programs is sthe issue. It's the language you use to program and its core platform and tooling that should matter, and we're still making headway on that.

Personally, I've used make/gmake/autotools/pmk and been happy with them for C, and I started with Java when all we had was make, then ant, and now I'm generally preferring Maven over all these alternatives. Though I can see value in gradle, buildr and others, but I like the prevalence of maven so far, until a more significant shift occurs. Plus I like that it's rigid, but still leaves you the ability to work around that if necessary. That it's not easy is a good thing.

It's a build tool. Just learn to embrace it and don't fight it. It's a losing battle. Or at least a very very long one.


问:
Is the microsoft C compiler (cl.exe) a compiler driver or a compiler?
gccand clang are both known to be compiler drivers. As such, the gcc executable does not compile anything itself. Rather, it calls the compiler (cc1), assembler (as) and linker (ld) with the right flags as needed.

Is this setup true also for the Microsoft C compiler, cl.exe? Is there actually some other executable that does the compilation? I assume that at least the assembling and linking are done by separate executables, since I know that ml.exe (known as MASM) and link.exe exist as separate executables, so cl.exe probably calls them.

答:
So the size of cl.exe is 158 KB. What do you think?

The rest of the MSVC C++ compiler is in various DLLs. The C/C++ compiler driver is officially cl.exe, but many of the driver functions are provided by MsBuild. Other executables include link.exe, lib.exe, ml.exe and bscmake.exe. You can't translate the archictecture or terminology blindly from one product set to another that is completely different.

However, if all you want to do is compile from the command line, cl.exe is the place to go. Details can be found here: https://stackoverflow.com/questions/7865432/command-line-compile-using-cl-exe.


答:
A top down or recursive descent parser (LL(k)) constructs a left derivation. Your first diagram roughly outlines the steps of such a parse.

A bottom up (SLR(k) or LALR(k)) parser constructs a right derivation. Your second diagram roughly outlines the steps of such a parse.

What is missing from this discussion is the differences between suitable grammars. If the grammar looked like this:

```<code><expr> := <term>
        | <term> + <expr>
</code>```

The left derivation is now shown to backtrack, and is less suitable for an LL(k) parser. The right derivation does not, and still suits bottom up parsing. There are other grammars in which the right derivation turns up problems.

The main reason for these methods, as I have used them in the past, is to show problems in the grammar which can be addressed by refactoring, to make them more suitable for a particular kind of parser.

Title: Left and Right most Derivation

Body:
A top down or recursive descent parser (LL(k)) constructs a left derivation. Your first diagram roughly outlines the steps of such a parse.

A bottom up (SLR(k) or LALR(k)) parser constructs a right derivation. Your second diagram roughly outlines the steps of such a parse.

What is missing from this discussion is the differences between suitable grammars. If the grammar looked like this:

<expr> := <term>
        | <term> + <expr>

The left derivation is now shown to backtrack, and is less suitable for an LL(k) parser. The right derivation does not, and still suits bottom up parsing. There are other grammars in which the right derivation turns up problems.

The main reason for these methods, as I have used them in the past, is to show problems in the grammar which can be addressed by refactoring, to make them more suitable for a particular kind of parser.


问:
How to structure many projects that can be versioned and deployed separately
My team is in a situation where we need to create about 30 different processors. By processors I mean code that takes data from a DB and sends it to a remote service somewhere else in the world. They each do fundamentally the same thing, only with different data.

Only some of these processors will be deployed to server A, some to server B, and so on. And they won't all version at the same time. If we make a change to processor X, we would version and ship X while leaving the rest of the processors alone.

We've come up with three possible options, each of which have their own issues:

Have 30 different solutions

Have one solution with 30 different projects

Have one solution, with one project, with each processor functionality separated by namespace

The main con of #1 is having to create and maintain 30 different solutions. And if we have to make a change to many processors at once, we'll need to open n solutions to do so.

The main con with #2 is versioning. If we branch the code, we branch the whole thing, even if only one project changed.

The main con with #3 is the same as #2. And then we also need to find a way to exclude certain processors from certain servers for deployment. #3 is the easiest to manage during development, but the most difficult during deployment.

Is one of these the best option, or is there something else I haven't considered?

答:
I would advise to think carefully about the different phases of release.  For example, usually as a release is created it is built, tested, packaged and then deployed.

Just because a solution builds together, doesn't mean it needs to be packaged together.  And likewise, just because you have a single package, doesn't mean you need to deploy everything in the package.

Thinking in this way will give you flexibility where and when you need it.

I think either Option #1 or Option #2 could work.  I don't think I would go with Option #3 because you don't have the option to package and deploy separately (So, configuration management would hold all of the complexity).

Option #1:

With this option, you give yourself ultimate flexibility.  Advantages are that different development teams can happily work independently (because the code is physically separate).  As you have suggested, the DRY principle suffers.  However, you can mitigate this by encapsulating common components in NuGet packages and then pulling them into your separate solutions.  This also gives the advantage of being about to version your common components.

This option also gives the option of having completely different packaging and deployment processes.  Again, you can have a standard script or process for each, but they are separate physical files.  This is advantageous if some of the processors will develop at different speeds, or have different complexity, or are development by different teams of people.

But the downside, as you have observed, is that you have separate solutions and have to manage and maintain them.

Option #2:

With this option, usually you would build the solution together.  You can keep common components in the solution (as different projects) and reference them.  However, versioning the common components between the processor projects is more difficult.

This option is slightly more difficult with different development teams because there is likely more merging as one team (or dev) makes changes to common components.  Adding more processor projects (at the same time) also give rise to merge issues.

Even if you build together, you can have different packaging routines for each of the processors.  So, you can separate out the dependencies of each and package the deployment separately.

In summary, it depends on many factors on your project.  Things like rate of change, team size, code dependencies, and complexity of each processor make a difference.  I'm not sure If I've given a clear answer one way or the other, but hopefully this will help in your decision.


答:
One product, one source code, different data.

There are not really enough details to know whether my answer is the right one, but your problem is a common one for solution providers with multiple customers who have slightly different requirements. The answers always some to converge on: Just One Product.

When you build, you build everything and when you run tests, you run all tests. That way one team doesn't silently break something for someone else and everything works all the time.

When you release, you release everything. You run short cycles and you only ship what needs to be shipped, but everything is there all the time for testing.

You may ship code for platform A that is not used on platform B, and you need configuration data to switch things on and off. Depending on your needs, you may converge on a solution that is largely data driven with little or no custom code. Or you might use a Domain Specific Language, or a scripting language to encapsulate differences.

This is a very good answer to a lot of questions. Perhaps it is the answer to yours.

Title: How to structure many projects that can be versioned and deployed separately

Body:
My team is in a situation where we need to create about 30 different processors. By processors I mean code that takes data from a DB and sends it to a remote service somewhere else in the world. They each do fundamentally the same thing, only with different data.

Only some of these processors will be deployed to server A, some to server B, and so on. And they won't all version at the same time. If we make a change to processor X, we would version and ship X while leaving the rest of the processors alone.

We've come up with three possible options, each of which have their own issues:

Have 30 different solutions

Have one solution with 30 different projects

Have one solution, with one project, with each processor functionality separated by namespace

The main con of #1 is having to create and maintain 30 different solutions. And if we have to make a change to many processors at once, we'll need to open n solutions to do so.

The main con with #2 is versioning. If we branch the code, we branch the whole thing, even if only one project changed.

The main con with #3 is the same as #2. And then we also need to find a way to exclude certain processors from certain servers for deployment. #3 is the easiest to manage during development, but the most difficult during deployment.

Is one of these the best option, or is there something else I haven't considered?


问:
Automated GUI tests take too long because of doing same steps before the "main test action" can proceed
We are developing a desktop application and I am responsible for GUI testing. Currently we have around 500 tests which take 8 hours to complete.

The tests are testing almost every button/menu/field etc. in the application. Some functions are taking 3 - 15 minutes because the application is actually measuring something, but most of the tests takes around 2 minutes.

The biggest problem I see is that a lot of tests are doing  same steps before the "main test action" can proceed, for instance start with import of documents to use for testing, which takes several seconds.

How to reduce this time?

Parallel testing or some fancy execution environment is probably not a solution or what I ask for. I am more interested in the design of the test or some tips for decreasing the repetition of work.

答:
Which automated testing tool are you using? This will significantly affect answers.

As for shortcuts yes you can trim down some of your tests.

e.g. If your first steps are always launching, logging in, navigating to module X... querying for "thing-a-ma-bobs" then running 12 scripts that do something with one. you can split your tests up so that you have the option of running each one end to end, or if the initial launch/login/navigation part is done, just run the remainder of each of your tests from that point.

Each of your tests should actually be recorded/written in a fairly modular format so that you can combine many of them together as needed and/or swap out one that needs tweaking.

e.g. for a desktop app I would expect a bunch like:

Launch App

Login to App (if applicable)

Navigate to Screen X (one for every major part of your app)

Select/View an ${Item} (whatever your items are)

Rename it

Modify it

Assign it

Unassign it

...

Hopefully (if needed) you can skip the first few steps once you're "in" and just run the individual tasks.


答:
There are several approaches to attacking the time bloat of automated testing.  Some of the approaches such as running multiple virtual environments and running the tests in parallel, and reducing the scope of what is actually tested may not not suitable for your workload, but are worth mentioning as possible strategies for other readers in the future.

One of the areas that automated tests can spend a lot of time, is getting the test run into the 'test state'.

One suboptimal solution is group together the tests so that you only need to get into that test state once, and then you run all of the tests in this state as part of a group test.  This is flawed, because you can have situations where tests only pass because of artefacts left over by an earlier sequences in the test run.

A more complete solution is to find a mechanism to quickly recreate your starting environment.  Have a lot at the systemic effect of the import of these documents.  Are they processed into different configuration files? are they updating multiple database tables?  and then finding a more efficient mechanism for the injection of this state information into the test environment.  However, this approach can be problematic if the format of the state information changes during product development, its a substantial job to recreate.

The solution I would probably use is to use a mechanism that allowed rollback.  It might be virtual environment that could rollback to a snapshot, or perhaps using LVM below the file system and then at the start of each test, we just roll back to the start point, losing the artifacts from the previous run.

Title: Automated GUI tests take too long because of doing same steps before the "main test action" can proceed

Body:
We are developing a desktop application and I am responsible for GUI testing. Currently we have around 500 tests which take 8 hours to complete.

The tests are testing almost every button/menu/field etc. in the application. Some functions are taking 3 - 15 minutes because the application is actually measuring something, but most of the tests takes around 2 minutes.

The biggest problem I see is that a lot of tests are doing  same steps before the "main test action" can proceed, for instance start with import of documents to use for testing, which takes several seconds.

How to reduce this time?

Parallel testing or some fancy execution environment is probably not a solution or what I ask for. I am more interested in the design of the test or some tips for decreasing the repetition of work.


问:
Mutable cursor in readonly object?
I've been searching for the different uses of the keyword mutable. I've found that it is generally used for caching, lazy computing, mutex, ...

But I'm wondering if it is coherent to use it for a cursor on a readonly object.

For example, I have a sound class, and I want each sound to be able to keep track of a play cursor, for play/pause use, but I don't think this is part of the state of the sound.
A const sound cannot be modified, but it can be read.

Is it ok to make the cursor attribute mutable?

答:
I believe your design would be better if the cursor attribute was changed to a function that created and returned a new cursor object that referenced this sound object.

That way you can have multiple modifiable cursors referencing the same sound object, all the cursors at different positions in the sound file -> which is just how cursors should work.

Title: Mutable cursor in readonly object?


答:
Is it ok to make the cursor attribute mutable ?

No. const is a promise that calling methods on the object won't change its state in an observable way. I use the term observable loosely here; it doesn't need to be observable from within the program. If a Sound object remembers its position in the playback, it has observable mutable state, because calling play() twice can result in different parts of the song being played. The rest of your program may not be able to observe that through the Sound class's public interface, but you can.

mutable is usually used for caching/lazy evaluation/memoization because you're going to be returning the same results every time. So even though something inside the object is changing, there's no way to observe that short of actually looking at the bytes in memory.

That said, do follow Ptolemy's advice - keeping the cursor out of the object is a cleaner design.

Title: Mutable cursor in readonly object?

Body:
I've been searching for the different uses of the keyword mutable. I've found that it is generally used for caching, lazy computing, mutex, ...

But I'm wondering if it is coherent to use it for a cursor on a readonly object.

For example, I have a sound class, and I want each sound to be able to keep track of a play cursor, for play/pause use, but I don't think this is part of the state of the sound.
A const sound cannot be modified, but it can be read.

Is it ok to make the cursor attribute mutable?


问:
How to model more than one 'last name'?
In Spanish-speaking countries we use more than one last name, like:

First name ↘                           ↙ Last name

                  Pedro Arturo Rodríguez Loyola

          Middle name ↗                               ↖ (?)

I'm trying to model data for patient name. In our country it's important, so I can't neglect the second last name, but I would like to build an application that can makes sense to other developers, especially from English-speaking countries.

I created a similar question on English.SE, it has some insights about why it's important to persist both values.

答:
This may help. The post is humorous, but insightful.

[First name] [Last name] is not a universal rule for names. It is just common where you live. If you impose rules in this, sooner or later you will have people who cannot be added to your system.

Basically, make sure you impose as few restrictions as possible, and allow flexibility between the actual stored name, and any extra ones.

I would go with something like this:

Display name (for consistent names when showing forms/data): (should be [first] [last]).

Other names/full name (for searching, more precise matching, etc).
Here, allow user to write anything, up to given length; length should be more than you think should reasonably be enough - e.g. if you think 40 characters should be enough, put 500 :) ).

Addressing (Mr, Mrs, Ms, Jr, Sr, -san, custom value (like Tov.) etc).

internal ID (this ID should uniquely identify every person within your application, preventing name collisions).

For example, my name isn't correctly writable in most European languages, due to Romanian-specific diacritics (so, whenever I write my name outside my home country, technically I write it wrongly - so people can read and pronounce it).

Some interesting name examples and links:

http://en.wikipedia.org/wiki/Nicholas_Barbon

http://en.wikipedia.org/wiki/Prince_%28musician%29

http://en.wikipedia.org/wiki/P_diddy

http://en.wikipedia.org/wiki/Burmese_name

Title: How to model more than one 'last name'?


答:
Q: How does a DBA count?

A: 0, 1, many

An individual has 1 or more given names and 1 or more family names, and possibly a title.  These names have an order to them.  It is up to the localization and culture to determine how to refer to an individual.

```
ContactId
NamePart {"John", "Smith", ... }
NameType {title, given, family, ???}
Order {1, 2, 3, ... }
```

For Pedro Arturo Rodríguez Loyola (contact #1), you would have four rows:

```
1 / Pedro     / given  / 1
1 / Arturo    / given  / 2
1 / Rodríguez / family / 3
1 / Loyola    / family / 4
```

This way it is not limited to any given structure yet still makes sense for a given contact on there.  What do you do when you have someone with 3 or 4 given or family names? or a maiden name?

Note that I've changed the order from a  from previous revision of this answer - the order is an order over the entire name rather than just an order within the name type because in some cultures, the family name comes first, you may have split title parts "Sir John Smith II".

Additional Reading

Falsehoods programmers believe about names

Two Last Names

Title: How to model more than one 'last name'?

Body:
In Spanish-speaking countries we use more than one last name, like:

First name ↘                           ↙ Last name

                  Pedro Arturo Rodríguez Loyola

          Middle name ↗                               ↖ (?)

I'm trying to model data for patient name. In our country it's important, so I can't neglect the second last name, but I would like to build an application that can makes sense to other developers, especially from English-speaking countries.

I created a similar question on English.SE, it has some insights about why it's important to persist both values.

Body:
Q: How does a DBA count?

A: 0, 1, many

An individual has 1 or more given names and 1 or more family names, and possibly a title.  These names have an order to them.  It is up to the localization and culture to determine how to refer to an individual.

ContactId
NamePart {"John", "Smith", ... }
NameType {title, given, family, ???}
Order {1, 2, 3, ... }

For Pedro Arturo Rodríguez Loyola (contact #1), you would have four rows:

1 / Pedro     / given  / 1
1 / Arturo    / given  / 2
1 / Rodríguez / family / 3
1 / Loyola    / family / 4

This way it is not limited to any given structure yet still makes sense for a given contact on there.  What do you do when you have someone with 3 or 4 given or family names? or a maiden name?

Note that I've changed the order from a  from previous revision of this answer - the order is an order over the entire name rather than just an order within the name type because in some cultures, the family name comes first, you may have split title parts "Sir John Smith II".

Additional Reading

Falsehoods programmers believe about names

Two Last Names


问:
Should an API be aware of client versions?
Should an API be aware of a client version?

The idea of that really goes against the grain for me, but it seems like it is being pushed for in my current project.

Am I wrong? Can I get some reasons for or against this that I can present or understand why it might be good.

The API is a RESTful API.

答:
The problem that you can run into without the idea of supporting client version is that it becomes very difficult to change your data representations required and sent by the API.  Representation changes can force client applications to change and leave them in a broken state until they do.

Not including a client version scheme makes sense if the API is reasonably static and its methods are not subject to modification, just addition.

Ideally you shouldn't have to choose between not being able to modify the service contract and breaking clients.

The underlying code base does not necessarily have to care about the client version if you have version specific deploy points managed via an url path scheme (ie http://my.org/myservice/v1, http://my.org/myservice/v2, etc).  You would have to manage multiple copies of the code with this scheme however.


问:
what is a basic block called if it contains a branch instruction?
what is a basic block called if it contains a branch instruction? How about a block that does not contain a branch instruction?

Thanks,

答: If a basic block contains any non-linear code-path (like a branch or jump) then it is not a basic block, by definition.


答:
The Wikipedia definition for Basic Block says that a basic block has one entry point, and one exit point.

It also says that the target of a jump instruction is a "leader," meaning that it marks the start of another basic block. So I guess they consider a jump (conditional or unconditional) an exit point.

It follows that jumps are places where you break off new basic blocks.  I suppose you call a basic block containing a branch instruction "not a basic block."

Body:
what is a basic block called if it contains a branch instruction? How about a block that does not contain a branch instruction?

Thanks,


问:
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?

答: That happens to me as well, all the time. I'd say if you lose interest in something, just let it go and start something else. If you really, really like an idea or a project you wouldn't lose interest. If you lose interest, you didn't really like it in the first place. So in the spare time, do what you feel like doing. Keep trying new projects and ideas till you find one where you don't need any artificial motivation. Even if takes years for that idea to come by, keep trying out whatever comes in your mind.</p>

OwnerDisplayName: Yash


问:
OOP what is meant by object-to-object communication
I've been reading in basic concepts of OOP,as i'm trying to make this shift from transactional scripts to more oop manner in php, and i often come across this definition :

An object stores its state in fields (variables) and exposes its
  behavior through methods (functions). Methods operate on an object's
  internal state and serve as the primary mechanism for object-to-object
  communication

and a simple example like :

Objects like real life objects have state and behavior. Dogs have
  state (name, color, breed, hungry) and behavior (barking, fetching,
  wagging tail).

from definition i see that methods should be either manipulating internal state (setters) or communicating with other objects;

but when i try to apply this to a Blog example. composed of mainly 2 domains User and posts.

what could be the User object Behavior ?

I cannt find any !

login, its an auth lib. thing so i should not include it in user.

posting articles is a Post object thing; again user conduct it; but its more of a post object concern to create a post right ?

User may be the main Aggregate object in a blog; yet the user is more like the Creator of Other Objects but he does not have a Behavior i can think of; he is being used -and his state- by other objects in most cases that all !

in a nutshell : what are allowed type of methods inside an object ?

答:
what could be the User object Behavior ?

I cannt find any !

login, its an auth lib. thing so i should not include it in user.
posting articles is a Post object thing; again user conduct it; but its more of a post object concern to create a post right ?
User may be the main Aggregate object in a blog; yet the user is more like the Creator of Other Objects but he does not have a Behavior i can think of; he is being used -and his state- by other objects in most cases that all !

Think about the system you are modeling, and worry less about where in the computer stuff happens.

Think about how you would do this in the real world. A blog post exists as a thing. But where did it come from? In most cases an author wrote it. That is behavior. Thus in modeling a system with OO design it makes sense to model that behavior by representing an author object that publishes blog posts.

"User" is way too general a term to have any meaning at this level. Think about the users of the system in terms of the behavior they carry out. An author of blog posts is very different behavior to a reader of blog posts, just like a newspaper reporter is very different to a newspaper reader. These can, and probably should, be different objects.

If all the different types of users share a common behavior, such as logging into the system, then you may have them all inherit from a parent "User" object that has one method, "log myself in".

This is behavior of the user, nothing else logs in to the system other than users after all. They may log in through an authentication system, but that system does not log in itself. So a User may carry out the "log myself in" message by sending another message to the authentication system saying "Here are my username and password, please authenticate me on the system".


问:
Why is it bad to write something in language X as if you're writing a program in language Y in terms of using a shared coding paradigm
A while ago, I asked a question on SO about something written in C++, but instead of getting an answer to the problem at hand, the comments went all crazy on my coding style, even when I indicated that it was a WIP piece of code and that I meant to clean it up later when I had the base case running. (I got so many down votes that I decided to pull the question, as my rep on SO already is near-abysmal)

It made me wonder why people adopt such a hard line "you're a noob, go fuck yourself" attitude. I was being accused of writing C++ as if it was Java. Something that I cannot understand, and that still baffles me.

I've been programming in quite a few OOP languages for a number of years now, albeit in intervals. I choose the language to use in terms of its available libraries and optimal execution environments for the job at hand. I adopt design patterns in OOP code and I'm quite confident  that my use of patterns is sound and that OO wise, I can hold my own. I understand the OOP toolbox, but choose to use the tools only when I think it's really required, not to just use a neat trick to show my coding wits. (Which I know are not top notch, but I think aren't at n00b level either).

I design my code before I write a single line. To define tests, I list the goals of a certain class, and the test criteria that it has to adhere to. Because it is easier to me to create sequence diagrams and then write code, I chose to write my tests after the interface has become obvious.

I must admit that in the piece of code I posted in the question, I was still using pointers, instead of using smart pointers. I use RAII whenever I can. I know proper RAII means safeguarding against nullpointers, but I work incrementally. It was a work in progress and I meant to clean it up later. This way of working was condemned strongly.

In my view, I should have a working example first so I can see if the base case is a viable way of thought. I also happen to think that cleaning up the code is something that is typical of the refactoring phase of agile, after the base case has been proven. I must admit that although I'm slowly getting the Cxx standard, I prefer to use what I understand, instead of taking the risk of using concepts that I have yet to master in production code. I do try new stuff once in a while, but usually in play projects that I have on the side, just for that purpose.

[edit] I'd like to clarify that gnat's suggestion [1] did not show up in the search I did before I started to ask my question. However although his suggestion does cover one aspect of the question, the question he linked to does not answer the heart of my question, just part of it. My question is more about the response I got to my coding style and the professional aspects of handling different coding styles and (apparent) levels of skill. With my previous question on SO and it's response as a case in point.
[/edit]

The question then is: why scoff someone that doesn't use your coding style?

The matters/subdivisions at hand for me are:

Why would it be a bad programming practice to use more error prone code in prototype situations, if refactoring makes it more robust afterward?

How would can program written in C++ be like it was written in Java? What makes it a bad program, (considering that I indicated the intent of the current style and the planned work to improve?)

How would I be a bad professional if I chose to use a construct that is used in a certain programming paradigm (e.g. OOP/DP)?

[1] Develop fast and buggy, then correct errors or be slow, careful for each line of code?

答:
Why would it be a bad programming practice to use more error prone code in prototype situations, if refactoring makes it more robust afterward?

When writing quick and dirty with the mind of fixing later there is the danger of forgetting something that you need to fix.

How would can program written in C++ be like it was written in Java? What makes it a bad program, (considering that I indicated the intent of the current style and the planned work to improve?)

In java you don't have to think about who owns a certain object, you just pass the reference along and forget about it like it is nothing. However in C++ there needs to be a clear definition of who owns the object and who is responsible for cleaning it up.

How would I be a bad professional if I chose to use a construct that is used in a certain programming paradigm (e.g. OOP/DP)

You wouldn't; C++ is a multi-paradigm language, it happens to support OOP rather well but it can do a lot of other things as well. However most of it boils down to using the correct tool for the job instead of pulling out the hammer each time you need to drive a pointy spike into some wood.

The reason you got a bad response is that most people on SO tend to judge skills by how idiomatic you can code in the language you are asking about. The people familiar with C++ tend to knee-jerk when they see bad code that looks like something that has bitten them in the past.