问:
Improving performance with caching of files in C#
EDIT: You were right, the problem is not checking if a file with the same name exists but the connection. I made a mistake measuring the time with StopWatch and included a part which used the network connection.

Original Question:
I am writing a software which analysis data from a database. In the database are many textfiles - could be 100GB - ranging from 100KB to some MB (<10MB). My software should run an analysis task every night. So I want to implement a file cache which stores the files and only downloads them again if they have changed. My problem is, the more files I have inside my storage folder, the slower the whole process of checking if the file exists becomes. I am using C#.

My ideas until now:

Lazy loading - just download the file if it could not be accessed

Group the files in folder so that I have less files in one directory

Creating a Dictionary with filename as key and File object as value for all files in one directory before starting analysis of one folder (assuming finding files in a dictionary should be much faster than on the disk)

I invested already some time and before I reimplement it I want to know:

Is there something else (idea) which I could try?

In C# is there a best practice regarding file handling which leads to the best performance?

Just as side information, if there is a new file I have to analyse all lines of it. Is there a faster way than StreamReader if I know upfront that it is a tabulator seperated file and I have to read for sure all lines of it and parse the information?

We are talking about an Oracle Database. The files are stored as BLOB in the database. The database is organized as tree and elements have children which are files that contain the state of the elements. Those files I have to download to be able to analyze them. I can get the last modify date so I am able to download only the files I need (which have changed or are new). There are about 300.000 files in the database but I don't need all of them. I have write access to the folders where I want to store them create them with my program on the local disk. This folder structure where I want to store them I call "file cache".

答:
Saving stuff to files isn't normally a good way of creating a cache. If in your case its the downloading of large files over the network from a remote machine which is slow. I would create another identical local database to store the files once you have downloaded them.

If this is not possible, create a small database in which you just store the id, filename and directory of the downloaded files. You can use this to check for the existence of the file and then read it off disk as you do now.

Rather than reading all the lines in a file to see if it's the same as the new one, create a checksum when you download it and add this to the database table.


问:
How isolated are static variables?
If I have a

```<code>public class SomeClass {
    public static final HashMap hashmap = new HashMap();
}
</code>```

and then I have five different classes with main(String[] args) methods, that I will run.

when they access SomeClass.hashmap, would they be accessing the same HashMap or will they each have their own hashmap created in the JVM?

答:
In general, for most of the available implementations of JVM, each of your 5 classes will get their own hashmap instances to call and use, because each new main thread will run in a new JVM. A new JVM is created if you use "java" command to run your program.

If you call these 5 main classes in 5 distinct threads from a tester class, they will be run as threads in the same JVM. In that case, they'll be using the same hashmap.

(https://stackoverflow.com/questions/18394560/when-multiple-java-programs-run-on-the-same-machine)

There are two more interesting scenarios where you can look at this question from - one if what if you are running your 5 main classes as separate programs on a server, and second is what if you are using a multitenancy supporting Java SDK.

In case of the application server, usually the same JVM and a new classloader will be used for each new program, so again, each main class gets its own hashmap. For the multitenant environment, the same hashmap gets shared (http://www.ibm.com/developerworks/library/j-multitenant-java/).

ContentLicense: CC BY-SA 3.0

Title: How isolated are static variables?


问:
Fine grained search over a large dataset
I have roughly 4 million records per day and have to keep 7 years worth online, so we are looking at 10.2 billion records that I need to be able to search through. The users are expecting that the search will be quick enough for a UI, results in 3-5s

Due to politics out of my control, I can't use an off the shelf database solution because it means I will have to give the database to another team to manage (don't ask) which means I loose the ability to optimize hardware and software as they have a one-size-fits-all service for databases and charge (internally) by the GB. I'm sure I am going to get comments suggesting I make the point, I already have and management understand what they are asking me to do is ridiculous.

I have been looking at using Lucene as the crux of my solution. Storing the actual data partitioned by type and by day in flat files. Then using a Lucene document to index some of the fields that are searched by, with the only "Stored" field being the id of the record (so that I can read it from the flat file)

I am not exactly clued up on Lucene or hard drives, but as per my understanding, there will be initial IO/seeking time for searching the index, then when I have all the Lucene document IDs, I read the documents which will incur further IO/seeking time, then I read the actual record from the flat flat...I can't imagine, given the size of the dataset, that this will be very quick, which I am slightly worried about?

Lucene has a max document size of 2.1billion per index, so I will require multiple indicies here.

Does this approach, on the face of it, look like it could work?

The data I am storing is event-action data. Most of the queries will be grouping by event id and getting the last event-action details for a particular event. Some of the queries will be analysing large sets events and their individual event-actions.

答:
You haven't said how big the data is, how big the individual fields are, or what budget you've got.

Regardless of what indexing system you choose, consider throwing hardware at the problem.  You shouldn't need to search the disks for anything.  Index all the data, using a scheme that's very fast to traverse (perhaps a sorted list or tree).  Store the index on disk, but then cache the whole index in RAM.  You may need tens, or even hundreds of gigabytes of RAM to do that.

If the individual fields are big, or variable size, consider indexing hashes of them.

The price for the server to do that could be scary.


答:
The answer is "each instance of the class within a different class loader is distinct."

If you have two different JVMs (you invoked java MainOne and java MainTwo), these are obviously different class loaders.  However, there are corner cases here with things such as nailgun which keeps the JVM running and... things get complex.

You also have situations such as when the application is launched from within an IDE. Some IDEs will spawn a new JVM for running the application, others could be launching a new class loader within their own JVM (I don't think any do this anymore as it made the IDE less than ideally stable). This can get very tricky too.

When running in within an application server environment, this can either be configurable, or you will need to check the specifications. However, realize that every possible combination is possible. You can have isolation of two modules within the same ear (each .war has its own class loader), you can the entire .ear share a class loader, you can have some funky compatibility mode where everything shares the .jar class loader, but is itself in an isolated class loader for the .war, or you can have the entire server use one class loader. Note that some application servers have changed how they work with different versions (JBoss prior to version 5 was not isolated, while after version 5 was, and 4.0.2 had a hierarchal class loader while 4.0.3 reverted to a unified class loader).  It's a mess.  And thus the awareness of "static is one per class loader" is key.

It is even possible to have different class loaders within a single application as demonstrated in run a executable jar file within java program using class loaders.

So, going back to the answer "each instance of the class within a different class loader is distinct" - and that can take many different forms and edge conditions.

Title: How isolated are static variables?

Body:
If I have a

```<code>public class SomeClass {
    public static final HashMap hashmap = new HashMap();
}
</code>```

and then I have five different classes with main(String[] args) methods, that I will run.

when they access SomeClass.hashmap, would they be accessing the same HashMap or will they each have their own hashmap created in the JVM?


答:
If I was in your shoes, I would begin with a very reasonable, by-the-book implementation, using nothing but a regular RDBMS, embedded within the application, so that they do not feel as if they have to support something.  SQLite, H2, or any alternative embedded database should do: No special flat files, no exotic indexes, no nothing: just a straightforward application of standard practices to solve the problem at hand, for the most part disregarding the immensity of the data.  (I would, of course, choose a large enough integer as a key, and that's all, pretty much.)

While working at it, a couple of more ideas would probably occur to me, as to how to make it work faster without resorting to anything exotic.

Then, I would test this to see how it performs, and I would demonstrate the results, together with the working solution, to the "powers of be" in your organization.

There is a chance that your straightforward implementation will perform within the required constraints, so you will be fine right there, no need to do anything else, zero resources wasted.

If the performance of the straightforward implementation is outside, but not too far out of, the required constraints, the "powers of be" might say "well, this is close enough, we don't want to do anything else about it, so that's what it will be."  Again, zero resources wasted.

If the performance of the straightforward implementation is outside, but within the same order of magnitude, of the required constraints, I would tell them to just buy better, bigger, faster hardware.  Most chances are they will do that and case closed.

If they don't want to buy better, bigger, faster hardware, then I would recommend that they rethink their requirement to refrain from using a large, scalable RDBMS.  If they are reasonable, and you have shown that you are reasonable too, chances are they will rethink it.

If the powers of be don't want to follow any of the reasonable avenues, and instead they want you to play the role of a magician, then and only then I'd start worrying about exotic solutions.  Many chances are, things will not reach that point.  But even if they do, the amount of work that you will have done in vain up until that point will be relatively small, and well worth the gamble that it might just suffice.

Title: Fine grained search over a large dataset

Body:
I have roughly 4 million records per day and have to keep 7 years worth online, so we are looking at 10.2 billion records that I need to be able to search through. The users are expecting that the search will be quick enough for a UI, results in 3-5s

Due to politics out of my control, I can't use an off the shelf database solution because it means I will have to give the database to another team to manage (don't ask) which means I loose the ability to optimize hardware and software as they have a one-size-fits-all service for databases and charge (internally) by the GB. I'm sure I am going to get comments suggesting I make the point, I already have and management understand what they are asking me to do is ridiculous.

I have been looking at using Lucene as the crux of my solution. Storing the actual data partitioned by type and by day in flat files. Then using a Lucene document to index some of the fields that are searched by, with the only "Stored" field being the id of the record (so that I can read it from the flat file)

I am not exactly clued up on Lucene or hard drives, but as per my understanding, there will be initial IO/seeking time for searching the index, then when I have all the Lucene document IDs, I read the documents which will incur further IO/seeking time, then I read the actual record from the flat flat...I can't imagine, given the size of the dataset, that this will be very quick, which I am slightly worried about?

Lucene has a max document size of 2.1billion per index, so I will require multiple indicies here.

Does this approach, on the face of it, look like it could work?

The data I am storing is event-action data. Most of the queries will be grouping by event id and getting the last event-action details for a particular event. Some of the queries will be analysing large sets events and their individual event-actions.


问:
Inclusive or exclusive upper bound in pseudo-random number generators
I am creating a little randomization library. One of the methods is similar to .NET's System.Random.Next(int start, int end). The thing is, I am not sure how to decide on whether the upperp bound should be inclusive or exclusive. While .NET's Random.Next has an exclusive upper bound, there are other frameworks and libraries that work with inclusive bound.

First there's a question of why do some library makers choose upper bound to be exclusive. IMO, from the library user's perspective, it doesn't read nice.

For example, throwing a d6 die looks like this:

```<code>var dieResult = rand.Next(1, 7);
</code>```

The second argument hints either that 7 is a valid result, or that I want a random number from the range of 7 consecutive numbers starting with the number 1. None of which is true. I can't see how choosing an exclusive upper bound makes any sense.

Additionally, by being exclusive, upper bound prevents a PRNG from choosing the Int32.MaxValue which is another strange thing.

So, to summarize:

Is there an explanation as to why did .NET devs choose upper bound to be exclusive

答:
The simple, slightly humorous, and not entirely likely answer: They copied Java.

```<code>public int nextInt(int n)
</code>```

Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence. ...

More likely, it dates back to C and its random() which generates 0 to INT_MAX.  To get it back into the range you want, you would typically do a (rand() % range) + 1.  So, if you wanted a d6 you would do dieResult = (rand() % 6) + 1. Note the 'exclusive' range there.  Do also note that this is a very poor random number generator because of its non-uniform distribution.

The choice of inclusive or exclusive is one of debate, and one can find reasons for it on both sides.  And someone is going to be unhappy and someone is going to get a off by one error with whichever choice is made.

The exclusive high value makes it much easier to work with zero based indexes such as the code given in the docs:

  int mIndex = rnd.Next(0, malePetNames.Length);
  int fIndex = rnd.Next(0, femalePetNames.Length);
```

<p>I remember that nicely in perl back in the day:</p>

```$rnd = $array[rand @array];
```

Note the 0 based indexing and the exclusive top range.

On the other hand, python uses an inclusive range.  C++'s std::uniform_int_distribution also uses an inclusive range for its random numbers.

If it was inclusive, you'd be adding a - 1 to each of those max values for selecting an element from the list. On the other hand an exclusive range, makes dice rollers look funny and trying to get from 0 to the Int32.MaxValue a bit more tricky. It also averts some index out of bounds errors in the code snippets from above. Pick which off by one error you'll make occasionally.

Trying to do both makes the library unwieldy and confusing. Pick one. Document it. Stick with it.

.Net (and Java) probably stuck with the one that was most familiar to its users from other languages and possibly one that made interfacing to existing libraries at the time a tad bit easier.

Body:
The simple, slightly humorous, and not entirely likely answer: They copied Java.

public int nextInt(int n)

Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence. ...

More likely, it dates back to C and its random() which generates 0 to INT_MAX.  To get it back into the range you want, you would typically do a (rand() % range) + 1.  So, if you wanted a d6 you would do dieResult = (rand() % 6) + 1. Note the 'exclusive' range there.  Do also note that this is a very poor random number generator because of its non-uniform distribution.

The choice of inclusive or exclusive is one of debate, and one can find reasons for it on both sides.  And someone is going to be unhappy and someone is going to get a off by one error with whichever choice is made.

The exclusive high value makes it much easier to work with zero based indexes such as the code given in the docs:

  int mIndex = rnd.Next(0, malePetNames.Length);
  int fIndex = rnd.Next(0, femalePetNames.Length);

I remember that nicely in perl back in the day:

$rnd = $array[rand @array];

Note the 0 based indexing and the exclusive top range.

On the other hand, python uses an inclusive range.  C++'s std::uniform_int_distribution also uses an inclusive range for its random numbers.

If it was inclusive, you'd be adding a - 1 to each of those max values for selecting an element from the list. On the other hand an exclusive range, makes dice rollers look funny and trying to get from 0 to the Int32.MaxValue a bit more tricky. It also averts some index out of bounds errors in the code snippets from above. Pick which off by one error you'll make occasionally.

Trying to do both makes the library unwieldy and confusing. Pick one. Document it. Stick with it.

.Net (and Java) probably stuck with the one that was most familiar to its users from other languages and possibly one that made interfacing to existing libraries at the time a tad bit easier.


问:
Is there anything wrong with putting comments in an HTML closing tag?
I often write comments like this

```<code><div id="wrapper">
...
</div> <!-- id="wrapper" -->
</code>```

But what if I wrote them like this?

```<code><div id="wrapper">
...
</div id="wrapper">
</code>```

At first it seems dangerous to put an id attribute that has the same value on another tag, but then, doesn't the browser throw out that information? As far as I can see Chrome and Firefox both disregard any "attributes" in the closing tag.

Also seems unfortunate that it's not a real comment. It wont be parsed semantically as a comment. But I don't think anyone is using that space for anything else, right? Closing tags only close, right?

Is there any reason not to do this other than it's not a standard?

答:
This scenario is not covered in the spec: http://www.w3.org/html/wg/drafts/html/master/syntax.html#end-tags

Moreover, the next section in the spec (Attributes) claims that attributes must be in start tags.

Since your proposal doesn't respect the spec, I strongly recommend against it.


问:
Prevent circular dependencies without introducing intermediary library

TL;DR Below

I'm working on a game server (in Java, but that part is less important), and have decided to split up the server logic from the engine logic; in part because they're in two different logical domains. The server will interact directly with the client, and handle the networking aspect of things, while the engine will deal with in-game events. I would like these to be as separate as possible, so that the engine doesn't deal with packets and networking, and the server doesn't deal with player movement, or game events. However, these two will have to remain coupled because, although they are in different domains, they are part of the same process.

For example, when the user launches their client and connects to the server, the server must send a message to the engine to create a new game session. The engine then initializes the world, adds the player to it, and registers the proper in-game event listeners. At this point, the engine tells the server that the world is ready. The server then sends a message client to load resources (images, landscape data, text, etc), registers packet handlers, and everything is ready to be played.

We also have to take it the other way. If the player disconnects their client, or logs out, then the server is the first to know of this state change. The server session may stay active, in case it's just a slight network hiccup, but eventually, it must tell the game engine to do any cleanup.

There can be many implementations of the engine and server. For example, they can be running in the same JVM instance, they could be on different instances, using socket channels to communicate between eachother, or we could have the engine on another server, using something like a REST service to communicate messages between them.

TL;DR

What sort of design patterns or strategies can I use to send messages between two coupled components, that should remain as separate as possible, without introducing an intermediary library?

答:
You could define interfaces, ans use their implementing classes as listeners. Look at the Observer Design Pattern.

Interfaces also force you to think in term of "services", so it would be relatively easy to implement several versions of your server and engine, and always use the interfaces for abstracting their implementation details.

For instance, imagine you want to implement a chess game. Create the following interfaces (given names are for clarity purpose):

ServerInterface: it represents a server for a client. Must contain methods such as addClient(ClientInterface), which allows you the connect a new client; move(Piece,Position), for displacing a piece on the board; addWatcher(WatcherInterface); etc. A LocalServer is probably the simplest implementation of this interface, because any call to the server will directly correspond to a call to a server method. A TCPServer will transparently forward your call through TCP, so that from the client point of view there will be no behavioral differences with a local server.

ClientInterface: it represents a client for a server. Must contain methods such as setServer(ServerInterface) for defining the server that must be called when the client plays; clientLeft(ClientInterface) for informing the client an other client left the game; etc. Again, a LocalClient implementing this interface will simply take into account received messages, while TCPClient will forward messages trough TCP and MailClient will send an e-mail to the client and will wait for an answer (if required) from the same medium.

EngineInterface: it represents all the means of communication with the engine. Typically, you will have methods for asking if a move is acceptable or not, etc. You can imagine a LocalEngine that implements your own rules, or a DistantEngine that uses a REST API on the web for subcontracting this part of your system.

WatcherInterface: the classes implementing this interface are only interested in observing the game. Your server will communicate any move to such classes. A Logger will react to the messages sent by writing the moves into a file. A ScreenWatcher will graphically represent the current state of the game. Etc.

With this solution, your components are highly decoupled, because each part only knows it can be connected to zero, one or many other parts, without specifying how these parts actually behave. Each part only rely on a small set of interfaces that are easy to maintain.

In your code, you will need a bit of glue for instanciating the concrete classes. For instance, in your client, the gamer will select a TCP game, then enter an IP address, and click on "join". The code associated to the GUI has therefore to create a TCPServer and to present it as a ServerInterface to the client associated to the GUI. The client should never know it is connected to to a TCPServer, so there will be nothing specific to this particular implementation used by the client. In the server side, your TCP application will be listen for incoming connexions. When a connexion starts, the application creates a TCPClient and presents it to the existing server as a ClientInterface, which will deal with it ignoring it is a TCP client.


问:
What operating systems will "\n" create a new line in? (Java)
(Java Version 8 Update 60)

Hello. I am currently writing a program for my first Java assignment in college.

My question is, in what operating systems will

System.out.println("Hello,\n World");

result in

```<code>Hello,
World
</code>```

to be written in the console?

I am writing a program that extensively uses \n to format my strings and this is working 100% fine on my laptop (Windows 10). I am just afraid that this will result in some wacky things such as

Hello,\n World

on other operating systems. My instructors use Mac and Linux and possibly older versions of Windows.

Edit: I've also seen something about "\r\n" somewhere, can anyone explain this as well?

答:
As others have already indicated, your question requesting a list is likely to get closed. You may be able to save it from being closed by rewording it to not ask for a list.  So, I will give you an answer which does not include a list.

Generally,

```<code> System.out.println("Hello,\n World");
</code>```

will always result in

```<code>Hello,
World
</code>```

in the console of any operating system out there.  In Linux and other systems belonging to the general Unix family, it is a given fact that this will work, because "\n" is the linefeed in these systems.

However, in Windows, where the linefeed character is "\r\n", emitting "\n" will still work.  So obviously, Java goes through the trouble of providing some kind of conversion.  However, I have never come across any official promise on behalf of Java that this conversion will always be provided.

If you are really concerned about this issue and do not want to leave anything to chance, then you should not emit "\n", you should emit the string obtained from the following call:

```<code>System.getProperty("line.separator");
</code>```

Body:
As others have already indicated, your question requesting a list is likely to get closed. You may be able to save it from being closed by rewording it to not ask for a list.  So, I will give you an answer which does not include a list.

Generally,

 System.out.println("Hello,\n World");

will always result in

Hello,
World

in the console of any operating system out there.  In Linux and other systems belonging to the general Unix family, it is a given fact that this will work, because "\n" is the linefeed in these systems.

However, in Windows, where the linefeed character is "\r\n", emitting "\n" will still work.  So obviously, Java goes through the trouble of providing some kind of conversion.  However, I have never come across any official promise on behalf of Java that this conversion will always be provided.

If you are really concerned about this issue and do not want to leave anything to chance, then you should not emit "\n", you should emit the string obtained from the following call:

System.getProperty("line.separator");


问:
Is there a reason not to use HTML5 local storage for content
On many static websites the total size of the actual text content of the most popular 20 pages would come in at under 100kb.

I would imagine that it would be possible to leverage HTML5 local storage to download content via AJAX from the server in a single file, save it to local storage and use it with a data driven JS framework such as angularJS.  In fact you could be downloading even more content silently in the background.  This could potentially make for a very fast and responsive user experience.

Despite this I have not seen any websites working in this way.  Is there a reason that I am missing that would make this a bad idea?

答:
I think the short answer to your question is that the reason you’re not seeing much happening like what you describe is that HTML5 local storage is in no way up to the task, and we have until just the last two years or so lacked anything being specified that would provide a better solution.

About HTML5 local storage specifically: It has a race condition and some other problems that prevent it from being suitable for use in production for any application where you want to ensure there’s not data corruption and where you want to be able to store more than just strings.

Those problems in local storage could possibly be fixed but the reality is that none of the browser-engine vendors have any interest at this point in sinking further resources into their local storage implementations. They would all rather that Web developers use alternatives to local storage.

Anyway, for the use case you describe, thankfully there are actually more robust solutions in the works. The key piece going forward will be Service Worker—and, in the context of this question, the Service Worker Cache and CacheStorage interfaces specifically.

Also IndexedDB is the production-grade solution that the Web runtime now has for the general case of robust storage of data on the client, with efficient implementations, and with more feature-filled control over the type of data stored and how it’s stored.


问:
What are your favorite version control systems?
This is more a discussion question than an actual attempt to determine the "best", since that clearly varies by the needs of the organization. I'm more curious about the arguments in favor of different systems across categories (centralized vs distributed, open vs proprietary, etc).

So, what do you think is the best version control system?

答:
The following quote from Q4TD pretty much sums it up for me:

“I loved Git until I tried it. Now I love Mercurial.”

        — Tor Norbye, The Java Posse Podcast

Also, hgsubversion makes a pretty good subversion client for Linux (where I usually use the command line, unlike Windows where I usually use TortoiseSVN). The biggest advantage: no .svn sub-folder in every folder, just a .hg at the top level.

Update: In response to Alex's request in the comments to "say more about why git didn't work for you, and how Mercurial worked better":

I wouldn't say that Git doesn't work for me, but Murcurial does work better IMO.

In a nutshell, this is Mercurial:

and this is Git:

And I assert that Mercurial will do everything most developers will need it to do, without having to look up the manual to figure out how to do the everyday things.

Admittedly, I've only used Git occasionally, but the programming community has been going gaga over languages like Ruby and Python, in part, for their conciseness and elegance, whereas Git feels like a camel that was designed by a committee of camels.

Bah, now look at what you've done? There's rant all over the place. Move along, nothing to see... nothing to see...

Update 2: And another apropos tweet I just came across:

“Git gets easier once you get the basic idea that branches are homeomorphic endofunctors mapping submanifolds of a Hilbert space.”

ContentLicense: CC BY-SA 2.5


问:
Does RAD allow you changes or modification after the cutover phase without the need to redo the system?
Like other agile models, that is a close loop where changes after the system development can still be implemented without the need to redo the whole system, is this possible for the RAD model as well?

Since I was told that in the Waterfall model, if there are changes after the development, it can only be implemented by redoing the whole system since it is a one way approach unlike agile where you can just go back to the development stage again.

Example:

After the cutover phase of the system, when it is already released for a few months, then the client decided that "hey I want this and that to be implemented I think it would make the system better with it". Can this be done immediately following the process model of the RAD and just directly proceed to construction phase, or do we need to recreate the entire RAD process from the start or the requirements planning phase?

From what I understood of Waterfall method, if a change is needed during the maintenance period or way after the system has been completed, it is a requirement to redo the entire Waterfall process and start from the start, which is very different for agile models where you can just go to the development stage to apply the necessary changes.

答:
In any methodology, you can response to changes or modifications. However, some methodologies are better at responding to changes and delivering those changes at less expense (of time and/or money) to customers and users.

It sounds like you have a fundamental misunderstanding of what the difference between agile (iterative and incremental) and waterfall (sequential) models are. Even if you are following a sequential model, you don't need to redo the whole system if there are changes after development. In a traditional waterfall model, you capture your requirements up front and fix them. Then, you design those requirements and implement a solution, then test the solution, and finally release a system. The idea is that this system meets all of the needs of the customer and users. But there's still maintenance after the fact, and even in a sequential environment, you can release patches and new versions of the software. If you continue to follow a sequential model, you'll bundle up a large number of requirements at the front, and proceed sequentially through each of the other steps, ending in a release.

The iterative and incremental models focus on delivering smaller pieces of the software. These pieces are delivered quickly to the user and are used to obtain feedback and perform course corrections - identify mistakes or misunderstandings in the requirements or design. You don't fix the whole set of requirements for the product at the start, but instead prioritize and either demonstrate or deliver the things that will add the most value earlier in the project. This type of approach takes the mindset that software can continually evolve and change, and a project isn't done until the customers and/or users are satisfied.

Specifically, in RAD, if you get a change after Cutover, you'll have to do some level of Requirements Planning. You can't just go and implement a change (in most projects, especially larger, corporate projects). You'll need to ensure that any changes make sense within the product/project scope and constraints and that the requirements are clear and consistent. You also want to ensure that changes made for one stakeholder don't have negative impacts on other stakeholders. Most of the change effort should be in the User Design and Construction, though, and then you proceed to Cutover.


问:
Is the `<table>` tag and `display:table` a functional or a layout directive?

Background

I've always disliked the idea of using the CSS property of display:table. Using it feels like an ugly hack and I always prefer to use a direct
 tag instead. Of course, I use it only when necessary and always prefer a non-
 based layout when possible. But for those occasions when a table is the only choice, I prefer the tag not the CSS. Naturally I've been reproached several times about this. In a recent discussion with Toni Leigh I finally managed to define my feelings about this issue well enough to make an argument out of it, so I'd like to present it here and hear what you guys think.

Crucial definitions

In HTML every tag can be thought of containing three distinct properties - functionality, layout and semantics.

Functionality is what the tag does. An href=""> redirects the user when clicked. An <input type="text"> creates a textbox. A
 wraps a block of content so that it can be manipulated further as a whole. An  shows a picture. Etc.

Layout is what the tag looks like (and sounds like, if you're using a screen reader). This contains things like colors, borders, spacings, fonts, etc.

Semantics is what the tag means (or represents). It could be a menu item, a blog post, a photograph, a design element, etc.

The meat of the issue

In modern HTML the layout is the province of CSS, while both functionality and semantics are dependent of the tag name and occasionally a few attributes, which has the side effect of intertwining them. People try very hard to keep these two domains (layout and functionality/semantics) separated, so that the necessities of one would not affect the other. For that reason using a
 for layout is scoffed, because that imposes a semantic change where only a layout change should be. And the preferred workaround is to use display: table as an alternative for achieving table-like layout effects but without the semantic changes that come along with the
 tag. (Preemptive retort: no, a
 doesn't make a bigger tag soup. You get all the same tags with display:table too, they're just all called
 now)

The reason this feels so "wrong" to me is because I see the display:table as a CSS property which changes the functionality of a tag. It's like using CSS to change an  to a <form> or an <input type="text"> into a <button> or something (well, if it was possible). As far as I'm aware it's the only CSS property with such an effect. Everything else really does affect only layout, but display:table does more. For this reason it feels like it shouldn't even be in CSS. In particular:

New kind of relationships between tag sizes are created. In non-tabular layouts, only parent-child relationships between tags are able to affect each other. In a table, other tags in the same row/column can affect a cell's size, as well as the total width/height of the entire table.

To make it worse, you get colspans/rowspans (CSS browser support flaky at the moment, but it's getting there)

The display:table-header element will be printed on every page, when printing the document.

You get vertical alignment of text and border collapse.

And probably others I haven't thought of. Well, I guess they are sort of layout features, but they are a whole world apart from anything else that CSS can do. Treating some markup like a table switches the browser into a completely different mode, which is why I think it's a different functionality, not just layout.

The question

What do you think? Can all the changes that display:table brings be counted only as layout, or is it a change of functionality as well?

答:
I'd say that a
 tag has a semantic meaning. You are encoding tabular data. Mostly, layout is not tabular data, so using the
 tag for layout is imposing presentation issues on a data-containing tag.

Body: I'd say that a <table> tag has a semantic meaning. You are encoding tabular data. Mostly, layout is not tabular data, so using the <table> tag for layout is imposing presentation issues on a data-containing tag.


问:
what is the correct RAD model diagram
I am trying to use RAD for our school project and i was searching for a model diagram to follow but i found 2

1st is the ladder type found here:

2nd is this one found here:

so i'm really confuse as to which one i should follow here, also how will i assign my task in accordance to RAD model.

if i follow the 1st one 3 developers will do 3 task simultaneously as stated by the link above that ladder increment type model states that numerous software teams works in parallel on different system functions.

while on the other hand the 2nd option from what i can understand from the diagram user design and implementation "or construction" will be repeated until a satisfactory outcome is reached and thus it will proceed to the last step which is the cutover or "implementation", this too is what RAD or agile models are for to be able to adjust to change accordingly during the development period.

答:
Both diagrams appear to be representing the same thing, but in different ways.

The boxes for "Communication" and "Planning" in the top diagram correspond to "Requirements Planning" in the bottom diagram. This is where you agree on stakeholder needs, scope, constraints, and develop the technical requirements that the team will deliver at Cutover.

The box for "Modeling" in the top diagram corresponds to "User Design" in the lower diagram. This is where you create your models of the system. The box for "Construction" in the top diagram corresponds to "Construction" in the lower diagram. This is where you actually write code and unit tests. The difference is that the top diagram clearly shows that you may have several iterations of modeling /user design and construction before what the top diagram calls "Deployment" and the lower diagram calls "Cutover".

Generally, what both diagrams is saying, is that your Deployment or Cutover - your final acceptance testing, deployment of the new system, and user training happens once in the life of the project. Your user design / modeling and construction activities may iterate - you can perform design and construction 1 to N times before you deploy the system. That may also include several subteams working on different features or subsystems independently of each other or the same team working sequentially on the user design and construction steps for features or subsystems.

Title: what is the correct RAD model diagram


问:
How to prevent changes to an internal API from breaking other projects?
We have like 20 - 30 independent modules/solutions. Each of these has about 7 - 10 projects with different classes, components, etc.  These are all used internal to our company.

Our problem is when we make a change in one module, we then need to make sure to update all other modules which are accessing this code. This is difficult to know because it is in different codebases.

How can we document where all external uses of an API are? Or otherwise prevent small changes from breaking other modules?

答: The simplest solution, IMO, is to have a decent number of automated tests for each product. When you update a library, you run the test suite for each product. If the tests fail, then you will know which products need to be updated.</p>


答:
I would suggest against trying to document this (at least manually) as if you require people to update it, it'll require a high level of accuracy to work right. But you will never get that level of accuracy because adding this sort of documentation would... not be fun and no one will do it.

A few better options:

Have a script that generates a usable list of all method calls in all other modules, based on search criteria. Simple, crude, but probably effective.

Some sort of version system where you don't break backwards compatibility except on major version numbers. So if you change method foo from version 2.1 to to 2.2, all code referencing version 2.X will still work. When you need to break this compatibility upgrade the major version number (so in this case, 3.0) and announce this to all project owners responsible for other modules. Depending on how your release process works this might be simple - or incredibly complicated - to implement.

Have automated testing or a CI build process where each time you push code a build runs tests of some sort. This will allow you to identify where problems happen. My guess is with what you are describing as the problem you don't have this already in place..

Some sort of automatic documentation system for all your modules/repositories

You also might consider slowly refactoring the APIs to not be so brittle, but I expect that's outside the scope of what you can reasonably accomplish if you are an individual and have 20+ large scale modules to work with.

Title: How to prevent changes to an internal API from breaking other projects?

Body:
We have like 20 - 30 independent modules/solutions. Each of these has about 7 - 10 projects with different classes, components, etc.  These are all used internal to our company.

Our problem is when we make a change in one module, we then need to make sure to update all other modules which are accessing this code. This is difficult to know because it is in different codebases.

How can we document where all external uses of an API are? Or otherwise prevent small changes from breaking other modules?


答:
CSS "display:table" does not change the semantics of any element. The "functionality" effects you describe are just visual layout effects, which is the domain of CSS.

You are correct that element sizes outside of the parent-child relationships can effect each other when using table-layout. This is the whole point of table layouts in a nutshell. Other layout properties like float also affect the placement and sizing of other elements outside of parents/children. The effect is purely visual layout - its not like styling an element as a table suddenly turns another element from a form into a script. There are no semantic effects on other elements.

Vertical alignment and border collapse are also purely visual effects, and again there are many other examples where the css display model will affect the interpretation of other css properties. I.e. changing the display from 'inline' to 'block' or to 'inline-block' will change how border, padding and margins are interpreted.

All of the available values of the display-property can be said to switch the layout into a different mode which affect many other css properties.

Title: Is the `<table>` tag and `display:table` a functional or a layout directive?

Body:
Background

I've always disliked the idea of using the CSS property of display:table. Using it feels like an ugly hack and I always prefer to use a direct
 tag instead. Of course, I use it only when necessary and always prefer a non-
 based layout when possible. But for those occasions when a table is the only choice, I prefer the tag not the CSS. Naturally I've been reproached several times about this. In a recent discussion with Toni Leigh I finally managed to define my feelings about this issue well enough to make an argument out of it, so I'd like to present it here and hear what you guys think.

Crucial definitions

In HTML every tag can be thought of containing three distinct properties - functionality, layout and semantics.

Functionality is what the tag does. An href=""> redirects the user when clicked. An <input type="text"> creates a textbox. A
 wraps a block of content so that it can be manipulated further as a whole. An  shows a picture. Etc.

Layout is what the tag looks like (and sounds like, if you're using a screen reader). This contains things like colors, borders, spacings, fonts, etc.

Semantics is what the tag means (or represents). It could be a menu item, a blog post, a photograph, a design element, etc.

The meat of the issue

In modern HTML the layout is the province of CSS, while both functionality and semantics are dependent of the tag name and occasionally a few attributes, which has the side effect of intertwining them. People try very hard to keep these two domains (layout and functionality/semantics) separated, so that the necessities of one would not affect the other. For that reason using a
 for layout is scoffed, because that imposes a semantic change where only a layout change should be. And the preferred workaround is to use display: table as an alternative for achieving table-like layout effects but without the semantic changes that come along with the
 tag. (Preemptive retort: no, a
 doesn't make a bigger tag soup. You get all the same tags with display:table too, they're just all called
 now)

The reason this feels so "wrong" to me is because I see the display:table as a CSS property which changes the functionality of a tag. It's like using CSS to change an  to a <form> or an <input type="text"> into a <button> or something (well, if it was possible). As far as I'm aware it's the only CSS property with such an effect. Everything else really does affect only layout, but display:table does more. For this reason it feels like it shouldn't even be in CSS. In particular:

New kind of relationships between tag sizes are created. In non-tabular layouts, only parent-child relationships between tags are able to affect each other. In a table, other tags in the same row/column can affect a cell's size, as well as the total width/height of the entire table.

To make it worse, you get colspans/rowspans (CSS browser support flaky at the moment, but it's getting there)

The display:table-header element will be printed on every page, when printing the document.

You get vertical alignment of text and border collapse.

And probably others I haven't thought of. Well, I guess they are sort of layout features, but they are a whole world apart from anything else that CSS can do. Treating some markup like a table switches the browser into a completely different mode, which is why I think it's a different functionality, not just layout.

The question

What do you think? Can all the changes that display:table brings be counted only as layout, or is it a change of functionality as well?


答:
First of all, an API with external uses should not change.

As @BryanOakley mentioned, using automated unit-tests is very important and life-saving in such situations. Beyond that, a few suggestions that may (or may not, according to situation) help you

Many languages (like Java and C#) offers Function/Method Overriding. Languages like Python offers you to pass (unlimited number of) arguments and keyword arguments to a function:

Java:

public void disp(char c)
{
     System.out.println(c);
}

public void disp(char c, int num)
{
     System.out.println(c + " " + num);
}

disp("A")
disp("A", 3)
```

<h3>Python</h3>

<pre class="lang-python prettyprint-override"><code>def disp(c, *args):
    if args:
        num = args[0]
        print("%s %f" % (c, num))
    else:
        print("%s" % c)

disp("A")
disp("A", 3)
</code>```

Many languages offers public, private and protected methods. You can handle the function call in a public function and do the job in private/protected functions.

In python, there is no public/private definition for methods and functions, but a leading underscore (_) tell that a method is private and should not be used externally. External API calls is handed by a method which is open to outside world and all tasks are done in so called local functions:

def format_input(a, b, *args, **kwargs):
    # This function is open to anyone. so use *args and **kwargs to get
    # all possible available arguments. Do not change this function
    # definition and function parameters
    a = _evaluate_input(a)
    b  =_evaluate_input(b)
    # c is not used by all APIs and may not documented in all external
    # API docs. So chech if it was sent as a keyword argument. If yes
    # evalaute it too
    if "c" in kwargs:
        c  =_evaluate_input(kwargs.get("c"))
    _check_extra_arguments(args)
    _check_extra_keyward_arguments(kwargs)

def _evaluate_input(value):
    # This is a private method thus should not be called from the
    # outside world. You can change this method and parameter structure
    # to fit your needs and do not care for outside world since no
    # outer API should use this function directly.
    ...

def _check_extra_arguments(value):
    # We will check if any extra argument is passed and handle them accordingly
    ...
```

As I said, an API  definition which is (also) used by external applications should not changed so oftenty. You can look for ways to make your outer functions more flexible so you can change how the API works without breaking the current state.

Body:
First of all, an API with external uses should not change.

As @BryanOakley mentioned, using automated unit-tests is very important and life-saving in such situations. Beyond that, a few suggestions that may (or may not, according to situation) help you

Many languages (like Java and C#) offers Function/Method Overriding. Languages like Python offers you to pass (unlimited number of) arguments and keyword arguments to a function:

Java:

public void disp(char c)
{
     System.out.println(c);
}

public void disp(char c, int num)
{
     System.out.println(c + " " + num);
}

disp("A")
disp("A", 3)

Python

def disp(c, *args):
    if args:
        num = args[0]
        print("%s %f" % (c, num))
    else:
        print("%s" % c)

disp("A")
disp("A", 3)

Many languages offers public, private and protected methods. You can handle the function call in a public function and do the job in private/protected functions.

In python, there is no public/private definition for methods and functions, but a leading underscore (_) tell that a method is private and should not be used externally. External API calls is handed by a method which is open to outside world and all tasks are done in so called local functions:

def format_input(a, b, *args, **kwargs):
    # This function is open to anyone. so use *args and **kwargs to get
    # all possible available arguments. Do not change this function
    # definition and function parameters
    a = _evaluate_input(a)
    b  =_evaluate_input(b)
    # c is not used by all APIs and may not documented in all external
    # API docs. So chech if it was sent as a keyword argument. If yes
    # evalaute it too
    if "c" in kwargs:
        c  =_evaluate_input(kwargs.get("c"))
    _check_extra_arguments(args)
    _check_extra_keyward_arguments(kwargs)

def _evaluate_input(value):
    # This is a private method thus should not be called from the
    # outside world. You can change this method and parameter structure
    # to fit your needs and do not care for outside world since no
    # outer API should use this function directly.
    ...

def _check_extra_arguments(value):
    # We will check if any extra argument is passed and handle them accordingly
    ...

As I said, an API  definition which is (also) used by external applications should not changed so oftenty. You can look for ways to make your outer functions more flexible so you can change how the API works without breaking the current state.


答:
I have seen and coded APIs with version numbers in the function paths and/or names.

In this way you can have different API versions available - complete APIs and different version of the functions within an API.

This puts the work of keeping all the API versions in the code for the API - no other applications' code needs to be changed other than the one for which the new API facility was produced.

I think this is especially important when writing APIs that will be used by applications outside of your organisation.

As an example, here is a code sample to send an SMS using the bulksms api:

http://developer.bulksms.com/eapi/code-samples/csharp/send_sms/

from there is the line:

```<code>string url = ".../submission/send_sms/2/2.0";
</code>```

where the 2 and the 2.0 are version numbers of the API.

As this API is intended for use by many Bulk SMS's customers, a change to this API would potentially break many applications and have the support phone ringing off the hook.

Body:
I have seen and coded APIs with version numbers in the function paths and/or names.

In this way you can have different API versions available - complete APIs and different version of the functions within an API.

This puts the work of keeping all the API versions in the code for the API - no other applications' code needs to be changed other than the one for which the new API facility was produced.

I think this is especially important when writing APIs that will be used by applications outside of your organisation.

As an example, here is a code sample to send an SMS using the bulksms api:

http://developer.bulksms.com/eapi/code-samples/csharp/send_sms/

from there is the line:

string url = ".../submission/send_sms/2/2.0";

where the 2 and the 2.0 are version numbers of the API.

As this API is intended for use by many Bulk SMS's customers, a change to this API would potentially break many applications and have the support phone ringing off the hook.


问:
How to balance quality vs. speed during the development process
I've been in the professional software development world for over 5 years now. An intense frustration that I've had over the years is when a software product that I'm working on turns out unstable, fragile, and compromised. Of course this always seems to happen when I'm rushing to develop several new features and meet a very aggressive, seemingly arbitrary deadline.

The new features require careful design, solid development, and thorough testing. However the importance of meeting the deadline compromises every one of these processes, resulting in an extremely fragile application.

I'm confident in my skills and experience as a developer, and I know that I can produce a great software product given the right environment. But it seems like when these types of projects come up, it's impossible for me to do my job well, and I can't stand behind my work.

How do great software companies handle this problem so that the end result is a great product?

答:
How do great software companies handle this problem so that the end result is a great product?

Different companies handle it differently.

The most obvious candidate is that they don't have very aggressive or arbitrary deadlines.

Some companies don't give a crap about producing great software products, since consumers are fine with shoddy ones and/or great ones cost too much.

And some companies require an aggressive deadline because time to market is important for them. They go back later and pay down the technical debt.

But one thing to note is that there will always be compromise. You will rarely ever have the time you need to get things perfect. You will almost always include features that seem pointless.

These things shouldn't make your software unstable or fragile. These things shouldn't involve a death march to the deadline. That isn't compromise.

Compromise is coming to some middle ground where both parties' needs are addressed.


答:
Testing.

If you test fully each bit of code you produce, then you can have confidence in it.

If the test fails, you can say definitively that there is a fault and the business can make a decision to live with that fault, but you will have called it out.

If you want time to write more tests but the business won't let you have that time you can legitimately say you are not confident because it is not tested. (ensure you can list the tests you would like to run before you are confident!)

But at some point you have to had over some completed work which you are happy to say meets the requirements.

Of course hand in hand with this comes requirements. I think this tends to happen because devs are told to produce stuff that meets high requirements 'we need to support a billion users!' when really the actual requirements are much lower. 'why is it taking so long?! I just need something to demo at the show next week for 5 users!' then it goes live and you can be caught out, 'we said a billion users!! and its fallen over with only 1,000,000!'

One of the things a good dev will be able to do is walk the fine line between asking for requirements the business just cant tell you and looking belligerent and just knocking something up which demos ok but wont be a good product when live

Title: How to balance quality vs. speed during the development process

Body:
I've been in the professional software development world for over 5 years now. An intense frustration that I've had over the years is when a software product that I'm working on turns out unstable, fragile, and compromised. Of course this always seems to happen when I'm rushing to develop several new features and meet a very aggressive, seemingly arbitrary deadline.

The new features require careful design, solid development, and thorough testing. However the importance of meeting the deadline compromises every one of these processes, resulting in an extremely fragile application.

I'm confident in my skills and experience as a developer, and I know that I can produce a great software product given the right environment. But it seems like when these types of projects come up, it's impossible for me to do my job well, and I can't stand behind my work.

How do great software companies handle this problem so that the end result is a great product?


问:
Throwing an exception when a method does not complete or implement a work around?
I have been studying this subject quite a bit, and I am unsure of what's the best way. I still have a lot to go trough (books, blogs, stack exchange, etc), but I simply can't find a consensus.

Basically here is the situation: You have a function that is expected to return a result, but the function fails (parameter is bad, exception occurs, resource is blocked / unreachable, etc), should you throw an exception or return something?

I simply don't see a best way of doing this, here is how things look:

People seem to agree that returning null and coding for null (expecting null and checking for it) is bad practice and I wholeheartedly agree. But on the other side of things people don't seem to like "vexing exceptions", for example Int32.parseInt throws an exception instead of returning null, this function has a counterpart that does not do that (tryParse) which returns a boolean but has an output parameter:

bool result = Int32.TryParse(value, out number);
if (result)
{
    Console.WriteLine("Converted '{0}' to {1}.", value, number);
}
```

I personally find this method a bit vexing, I studied threading under linux and most methods were methodA(in, in, out, in) which is horrible. With a proper IDE this issue is way smaller, but it still left a bad taste in my mouth.

And in some languages (like Java) this is not exactly easy to do, since it has no out descriptor (or whatever it's called) and it passes by value.

So it comes down to this: How would this be better handled? Add exceptions to when your code can't complete (if it isn't a number, I can't parse it, I can't return a number), or should you work around the issue. In C, C++, or C# I would probably always go with the bool method(in, out) approach, but in Java this seems like a really painful way of solving the issue, and I'm not sure if it's available in other languages or not.

答:
You should throw an Exception.

People like the TryParse because otherwise you need an IsParsable function which probably does the same thing under the hood.

Personally I try to define my functions such that common exceptions can be handled within the function itself. eg

//returns empty list when no objects are found
IEnumerable<Objects> Repository.GetObjects(string search)
```

<p>The thing to avoid is a situation where throwing an exception will force the calling code to  use the exception as part of the logic ie.</p>

<pre class="lang-cs prettyprint-override"><code>Try {
    This();
}
catch() {
    That();
}
</code>```

As this is both expensive and prone to error when unexpected exceptions are thrown.

Body:
You should throw an Exception.

People like the TryParse because otherwise you need an IsParsable function which probably does the same thing under the hood.

Personally I try to define my functions such that common exceptions can be handled within the function itself. eg

//returns empty list when no objects are found
IEnumerable<Objects> Repository.GetObjects(string search)

The thing to avoid is a situation where throwing an exception will force the calling code to  use the exception as part of the logic ie.

Try {
    This();
}
catch() {
    That();
}

As this is both expensive and prone to error when unexpected exceptions are thrown.


答:
On the one hand, it all depends on the contract of the procedure.  Whatever you choose, you should make sure that what you expect of the caller (e.g., preconditions) and what the caller can expect from you (e.g., postconditions) are clear, including what the caller should expect on failure.  If you put the burden of checking return values on the caller, make sure the caller knows.

I favor exceptions.  Exceptions are beneficial because they make it difficult for client code to silently ignore the error.  In Java, for instance, non-Runtime exceptions must be explicitly caught or declared as thrown; Runtime exceptions generally shouldn't be caught and are intended to terminate the program due to a bug.  Exceptions also make it easier to separate the "real" logic of solving the problem from the "error handing" logic, which make the code easier to read.

What I wouldn't suggest is mixing using exceptions with using return codes – that'll confuse anyone using your API.

Title: Throwing an exception when a method does not complete or implement a work around?

Body:
I have been studying this subject quite a bit, and I am unsure of what's the best way. I still have a lot to go trough (books, blogs, stack exchange, etc), but I simply can't find a consensus.

Basically here is the situation: You have a function that is expected to return a result, but the function fails (parameter is bad, exception occurs, resource is blocked / unreachable, etc), should you throw an exception or return something?

I simply don't see a best way of doing this, here is how things look:

People seem to agree that returning null and coding for null (expecting null and checking for it) is bad practice and I wholeheartedly agree. But on the other side of things people don't seem to like "vexing exceptions", for example Int32.parseInt throws an exception instead of returning null, this function has a counterpart that does not do that (tryParse) which returns a boolean but has an output parameter:

bool result = Int32.TryParse(value, out number);
if (result)
{
    Console.WriteLine("Converted '{0}' to {1}.", value, number);
}
```

I personally find this method a bit vexing, I studied threading under linux and most methods were methodA(in, in, out, in) which is horrible. With a proper IDE this issue is way smaller, but it still left a bad taste in my mouth.

And in some languages (like Java) this is not exactly easy to do, since it has no out descriptor (or whatever it's called) and it passes by value.

So it comes down to this: How would this be better handled? Add exceptions to when your code can't complete (if it isn't a number, I can't parse it, I can't return a number), or should you work around the issue. In C, C++, or C# I would probably always go with the bool method(in, out) approach, but in Java this seems like a really painful way of solving the issue, and I'm not sure if it's available in other languages or not.


答:
One possibility is to use an Optional generic. For example, see one from Guava:

http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/base/Optional.html

In this case, your function becomes

```<code>Optional<Int32> parseInt(String);
</code>```

Now the calling code has to explicitly decide how it wants to handle the failure case. The Guava Optional generic has a variety of methods to make common use cases easy.

In some ways, this is like a null, in that you include the error state as a return value. However, this is explicit. If you try to pass a Optional where an Int32 is expected, you'll get a compiler error. At the point where Optional becomes an Int32, you know that you've got to think about handling the failure case.

Body:
One possibility is to use an Optional generic. For example, see one from Guava:

http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/base/Optional.html

In this case, your function becomes

Optional<Int32> parseInt(String);

Now the calling code has to explicitly decide how it wants to handle the failure case. The Guava Optional generic has a variety of methods to make common use cases easy.

In some ways, this is like a null, in that you include the error state as a return value. However, this is explicit. If you try to pass a Optional where an Int32 is expected, you'll get a compiler error. At the point where Optional becomes an Int32, you know that you've got to think about handling the failure case.


答:
The modern line of thinking, which is considered as best practice by most publications and in most workplaces out there, is to never return result codes, and instead to always throw an exception if you cannot return a valid value.  (But read more, there is a twist.)

The main reason why C# offers tryParse() is because in Microsoft Dot Net throwing an exception is outrageously slow, (because the DotNet runtime works in promiscuous collusion with the underlying Windows Operating System,) so you cannot really afford to have exceptions thrown and caught during normal flow of operations.  By contrast, in Java throwing an exception and catching it is relatively inexpensive, (because it gets handled entirely within the VM without hitting the underlying operating system, whichever that may be,) so people do it all the time and are not afraid of it.

If you are really concerned about the performance overhead of exceptions, then you may "bend" the best practices to make them suit your needs.

Best practices dictate that any exceptional situation must be reported by means of an exception, while any non-exceptional situation must not be reported by exception.  Thus, you can redefine, if you wish, what a non-exceptional situation is, as to include, for example, the case where you are unable to parse a given string.  If you expect failures to be common, this is reasonable.  So, in this scenario, it is permissible to indicate a parsing failure by some means other than an exception, because you have just redefined failure-to-parse as a non-exceptional situation.

So, in C#, besides the (admittedly awkward) try...() style of methods you can always return a nullable primitive (int?) or that "optional generic" suggested by Winston Ewert. In java, you can always return a boxed primitive, (Integer instead of int,) which can be null.


问:
What would you consider best practice workflow tools for web application (PHP) development?
I'm really hoping somebody with more experience can edit the question as per my examples of answers:

using version control

test driven development

debugging code (xdebug for php)

use of UML diagrams

use of OOP for maintainable, reusable code

use of frameworks (like Zend Framework for php) for rapid application development

Anything else or an elaboration of what I mentioned above?

Basically, I'm in the middle of forming a team of developers (I'm a developer myself) and I'd like some advice on how professional programmers/designers etc should work together and what standards/paradigms they should use.

Also, if anybody has any books or links on the subject I'd welcome that!

I did find this which I guess satisfies what I'm looking for, or at least part thereof:

http://www.ibm.com/developerworks/websphere/library/techarticles/0306_perks/perks2.html

答:
using version control

SVN is very common, but mercurial is more handsome, powerful and has solid gui support.

test driven development

well, if you do unit testing you're already on the winning side. for tools, it's a matter of choice. testing has to be as easy as possible, thats the reason why i ditched PHPUnit for SimpleTest.

debugging code

with unit tests you will hardly need xdebug. i use xdebug usually for profiling only. (check out KCachegrind btw)

use of UML diagrams

the biggest problem with everything that reflects code logic is that it is a lot of manual work to keep in sync. you can automate some tasks, but it's not that useful, because you usually wanna use uml before you have anything. the other problem is that diagram tools are much harder to use than pen and paper or a whiteboard. use uml if you have to communicate a problem with multiple developers or if you need an abstraction for yourself. ("dia" is a nice free tool. also mind mapping tools are very handy for brainstorming, some can actually compete with pen and paper.)

use of OOP for maintainable, reusable code

well, oop works out to some degree. :)
one good advice: composition > inheritance. inheritance is an powerful tool to reuse on the first sight, but maintenance and loose coupling will suffer for it. second good advice: maintenance > reuse. an abstract system can be very powerful, but also hard to maintain.

use of frameworks (like Zend Framework for php) for rapid application development

RAD is a good thing to get your app out early. but some components - especially ORM - will shoot your feet, at least if it comes to scalability. the major problem here is that you tie your domain logic to work with objects, which becomes very hard to factor out if you need a pure scalable database optimized solution. be aware of that and encourage your developers to utilize the database without high level abstraction layers. database abstraction is a myth, orm is a lie.

KISS

newcomers usually wanna apply all those best practices around, set up coding standards, use all the nice tool chains, whatever. it works out for some developers, but some will run into an mental blockade if things a too strict. unit testing and scm is really a must have, but someone new to unit testing really needs to learn it's value before he loves it. don't overdo, apply practices step by step and see how it works out.
KISS also boils down to code. sometimes the best way to solve a hard problem, is to solve it wrong. you need a six degrees of separation algorithm? just choose some friends at random. you can create a complete application around it, with wrong logic. if the customer eventually decides to to ditch it, everyone saved a lot of money.

agile

learn about agile methodologies, extreme programming, scrum, etc. there are many books out there. any book will make your team better, but it's the best to get every teammate into it.


答:
In general, throw an exception, do not implement a work around.

The basic principle is to write the method such that it is clear what the result will be from calling the function, and for the caller to expect that the function will succeed in its purpose.

The Int.TryParse() stretches this a little (but not beyond breaking point) in that it is clearly implied that the return from this function returns true/false on whether the string can be parsed into an integer.  The return value of false implies that the function successfully determined that the string parameter was not a correctly formatted Integer.

The key, is that it is not instead of exceptions.  This function will still throw exceptions (e.g. ThreadAbortException, InvalidArgException etc...).  Exceptions are not being hidden by a return value of 'false'.

When to use the Try Implementation?

I would typically use the Int.Parse() in normal use.

The exception to this, is if I was writing the actual parsing function for a larger data set.  Perhaps a configuration file, or validating user data entered in a UI.  In these situations the actual purpose of the code is to do a series of validation checks on the data, and I am at the right point in the code to implement different actions based on data formatting.

If my only other option was to do:

```<code>try
{
   ...
}
catch(ParseException ex)
{
   //  do this instead....
}
</code>```

and I would swallow the exception, then using the TryParse() makes sense.

Body:
In general, throw an exception, do not implement a work around.

The basic principle is to write the method such that it is clear what the result will be from calling the function, and for the caller to expect that the function will succeed in its purpose.

The Int.TryParse() stretches this a little (but not beyond breaking point) in that it is clearly implied that the return from this function returns true/false on whether the string can be parsed into an integer.  The return value of false implies that the function successfully determined that the string parameter was not a correctly formatted Integer.

The key, is that it is not instead of exceptions.  This function will still throw exceptions (e.g. ThreadAbortException, InvalidArgException etc...).  Exceptions are not being hidden by a return value of 'false'.

When to use the Try Implementation?

I would typically use the Int.Parse() in normal use.

The exception to this, is if I was writing the actual parsing function for a larger data set.  Perhaps a configuration file, or validating user data entered in a UI.  In these situations the actual purpose of the code is to do a series of validation checks on the data, and I am at the right point in the code to implement different actions based on data formatting.

If my only other option was to do:

try
{
   ...
}
catch(ParseException ex)
{
   //  do this instead....
}

and I would swallow the exception, then using the TryParse() makes sense.


问:
Is this taking DRY too far?
A colleague and I are working together on a Meteor app.

One of us thinks that the following code in two places should be wrapped in a function to avoid duplication -- the other thinks that it leads to needless indirection/complexity.

I won't tell you which is me, because I would like the answers to be unbiased :).

We disagree on this particular point and would like some help coming to a conclusion which is beneficial to our codebase.

One file has:

```<code>Email.send({
    from: sender,
    to: recipient,
    subject: "Message from our company",
    message: message
});
</code>```

Another file has the same code:

```<code>Email.send({
    from: sender,
    to: recipient,
    subject: "Message from our company",
    message: message
});
</code>```

Please be keep your comments respectful.

Question:
Should this code be abstracted out into a function?

Clarification/Edit: The "Message from our company" is the same in both Email.send calls.

答:
It depends.

Assuming that the subject (and not the sender, for instance) is the only "repeated" parameter here, I'd turn the question back on you: If the subject text changes in one place, does it necessarily change in both?

DRY isn't so much about eliminating the amount you copy+paste. It's actually quite easy to copy and paste! What we aim to avoid is the typical fallout of a copy-paste event: N > 1 lines of code that need to be maintained in perfect unison.

Title: Is this taking DRY too far?


问:
Qt, LGPL, Android and GooglePlay licensing
So, you can build Qt apps and deploy them to android.

Taking a look at this post, I was wondering if making a game using Qt would be fine with LGPL legal terms given the following facts:

The app would be at GooglePlay so, using third party libraries is a concern

Source code for the app would be public

Possible in-game purchasing. I don't really know if this is ok with making the code public, since I ignore what info is needed in the app for achieving this.

Would it be ok to use Qt LGPL license with all that? does any point fails at using LGPL? if so, which one?

答:
The following libraries: Qt Location, Qt 3D, Qt Canvas 3D, Qt WebView, Qt Quick Controls and parts of Qt WebEngine are currently only available through LGPLv3, creating some serious responsibilities for the developer. As long as you don't use these and meet up with the rest of the minor requirements it's ok.

"With LGPLv3 it is also explicitly stated that the user needs to be able to run the re-linked binary on it’s intended target device. It is your obligation to provide the user with all necessary tools to enable this process. For embedded devices, this includes making the full toolchain used to compile the library available to users. For parts licensed under LGPLv3 you are obliged to provide full instructions on how to install the modified library on the target device (this is not clearly stated with LGPLv2.1, although running the application against the modified version of the library clearly is the stated intention of the license)." - FAQ


答:
Generally speaking, the purpose of DRY is to avoid repeated application logic. I don't see any repeated application logic here. Email.send is already a high-level reusable method.

One approach might be to use a constant as the subject.

Title: Is this taking DRY too far?

Body:
A colleague and I are working together on a Meteor app.

One of us thinks that the following code in two places should be wrapped in a function to avoid duplication -- the other thinks that it leads to needless indirection/complexity.

I won't tell you which is me, because I would like the answers to be unbiased :).

We disagree on this particular point and would like some help coming to a conclusion which is beneficial to our codebase.

One file has:

```<code>Email.send({
    from: sender,
    to: recipient,
    subject: "Message from our company",
    message: message
});
</code>```

Another file has the same code:

```<code>Email.send({
    from: sender,
    to: recipient,
    subject: "Message from our company",
    message: message
});
</code>```

Please be keep your comments respectful.

Question:
Should this code be abstracted out into a function?

Clarification/Edit: The "Message from our company" is the same in both Email.send calls.


问:
Why do build tools use a scripting language different than underlying programming language?
I've recently been using some build tools for a Nodejs project at work when I realized that most languages' main build tool/system use a different language than the underlying programming language itself.

For example, make does not use C or C++ to write scripts and ant (nor Maven) doesn't use Java as their language for scripting.

Newer languages like Ruby do use the same language for build tools like rake, which makes sense to me. But why hasn't this always been the case? What't the advantage to having a build tool that uses a different language from the underlying language?

答:
The choice of what programming language to use to get anything done must depend on the specific features of that goal and not on the other tasks related to that project.

A build tool does a very specific job, and no matter what language you're using for the main project, a build tool is a software by itself.

Trying to tie the main project and its build tool could be a very bad decision. What you should need with a build tool is mainly a fast process of simple rules, with a fast management of files and IO.

If languages like ruby do use themselves to implement their build tool, is related more on the features of that language than on the assumed need to keep everything written with the same language.


答:
It's not impossible to write a build tool that uses a language such as C or Java as their scripting languages. However, build tools benefit from using more declarative scripting languages. Imagine the pain and boilerplate of writing a makefile (or build.xml, or whatever) in C.

Build tools benefit from the use of DSLs, a task for which C isn't a particularly good choice. C is too general for a build tool, and too concerned with error-prone & low-level details. For example, you do not want to be concerned with explicit memory-management in a build tool! So even if using C-like syntax, you'd probably be using garbage collection in your scripting tool, at which point why not simply use a dedicated DSL?

It makes sense that Ruby can be used for this, since it's a higher-level, more declarative language than C or Java. Also see: Gradle, sbt.

Title: Why do build tools use a scripting language different than underlying programming language?

Body:
I've recently been using some build tools for a Nodejs project at work when I realized that most languages' main build tool/system use a different language than the underlying programming language itself.

For example, make does not use C or C++ to write scripts and ant (nor Maven) doesn't use Java as their language for scripting.

Newer languages like Ruby do use the same language for build tools like rake, which makes sense to me. But why hasn't this always been the case? What't the advantage to having a build tool that uses a different language from the underlying language?


答:
It depends, but if you are in doubt, throw an exception.

The one thing you should never do, its to return an error indicator value which is the same type as a legitimate result. Like having IndexOf() returning -1 if the substring is not found. This is bad because you easily forget to check for this special value, so you might hide errors without handling them, which leads to hard-to-find bugs. Returning null also have this issue because null is a valid value for any object type, so it leads to the same problem.

The TryParse() pattern is somewhat better, since it is explicit in that it returns a flag which indicates if the operation succeeded. You can of course decide to ignore the return value, but you are not likely to do it by mistake. Furthermore, the TryParse()-method has the counterpart Parse(), which thrown an exception if parsing fails. So if your code is not able to recover form a parse error you just use the Parse() version anyway. If TryParse() didn't have the Parse() counterpart, you would run the risk of people ignoring the error code and hiding errors, but this is not likely when it is simply easier to use Parse().

So even if you implement a Try...-pattern method, you should have a counterpart which throws on error. Which leads back to the point that you as as default should implement the version which throws on error.


答: I would at least like to have the string constant "Message from our company" consolidated in one place. If this is standard boilerplate, then some UI guy/copy editor is going to want it changed at some point and would want it to be consistent across the app.

Title: Is this taking DRY too far?


问:
Why does `length - 2` recursively give you the center of a linked list?
I am reading through an Algorithms book and am working through a recursive solution to the following question:

Implement a function to check if a linked list is a palindrome

This is an easy enough task, but the book suggests a recursive solution that I can't seem to wrap my head around. It states that to know when we are at the center of the linked list, we can perform the following operation:

```<code>recurse(Node n, int length) {
  if (length == 0 || length == 1) {
    return [something]; // At middle
  }
  recurse(n.next, length - 2);
  ...
}
</code>```

Why does length - 2 recursively get you to the middle? Can someone explain this in detail? I understand that it works, but not mathematically why it works.

答:
If you advance one step and subtract two from the length, you get a new sublist with the ends removed.  Observe that the code does not just subtract from the length.  It starts the sublist at n.next.

For example, start with the list abcde, with length 5.  On the first iteration, you have the sublist of length 3 starting at the second position, or bcd.  On the second, you get the sublist of length 1 starting at the second position of the sublist (the third position of the original list), or c.  Since length is 1, you stop, and this is the middle position.


答:
I don't think avoiding duplication of "code" is really the reason you should put this into another function but because this "requirement" is a duplication.

Is someone going to say, we no longer want to send an email in this case, but send something else like a Facebook post. This is contingent on why you have this in two code places to begin with. You could be doing it in two places for two entirely different reasons.

Just because two lines of code do the same thing doesn't mean they do it for the same reason. If the requirements indicate these two things should always be the same, put them in the same function.

Title: Is this taking DRY too far?


答:
I just completed a project where I was wrapping practically ALL my methods that do this sort of work in a "message" type class, which (in C#) has a concrete boolean for success/fail, along with error/success messaging string collections. There's a generic <T> version that inherits from the non-generic version, so a method might return a plain TransactionResult (which just signals if a data call failed or succeeded, with extra messaging as needed), or a TransactionResult<string> which is the former, plus a string field which holds the return value (in this case, a string).

Now I can just inspect the returned object's 'Success' field, without having to know that "-1" means "string not found". I could even wrap my "ResultObject" property with a backing private field and check it for null when setting (if I was so inclined). No more error handling for common stuff, and overall its been quite easy to implement.

```<code>public class TransactionResult
    {
        public bool Success { get; set; }
        public ValidationResult[] TransactionErrors { get; private set; }
        public string[] SuccessMessages { get; private set; } // in case you need to signal a partial success, or a custom result message

        public TransactionResult()
        {
            TransactionErrors = new ValidationResult[0];
            SuccessMessages = new string[0];
            Success = true; // Easier to let it be 'true' by default, and 'false' only when you add an error or set it manually!
        }

        public void AddTransactionError(string errorMessage, string[] memberNames = null)
        {
            var newError = new ValidationResult(errorMessage, memberNames ?? new[] { "" });
            TransactionErrors = TransactionErrors.Concat(new[] { newError }).ToArray();
            Success = false;
        }

        public void MergeWithErrorsFrom(TransactionResult transaction)
        {
            if (transaction == null || !transaction.TransactionErrors.Any())
                return;

            SuccessMessages = SuccessMessages.Concat(transaction.SuccessMessages).ToArray();
            TransactionErrors = TransactionErrors.Concat(transaction.TransactionErrors).ToArray();
            if (TransactionErrors.Any())
                Success = false;
        }

        public void AddSuccessMessage(string message)
        {
            SuccessMessages = SuccessMessages.Concat(new[] { message }).ToArray();
        }
    }

    public class TransactionResult<T> : TransactionResult
    {
        public T ResultObject { get; set; }

        public void MergeWithResultsFrom(TransactionResult<T> transaction)
        {
            MergeWithErrorsFrom(transaction);
            ResultObject = transaction.ResultObject;
        }
    }
</code>```

The above class uses the System.ComponentModel.DataAnnotations.ValidationResult class as its sorta backing "error" class, since I'm working in MVC4, which means my results can bind to my UI pretty much automatically, but you could roll your own version of that error as well. I wouldn't mind working in a language where this sort of structure was automatically part of the language/framework instead of having to roll my own, but that's pretty opinionated.

Body:
I just completed a project where I was wrapping practically ALL my methods that do this sort of work in a "message" type class, which (in C#) has a concrete boolean for success/fail, along with error/success messaging string collections. There's a generic <T> version that inherits from the non-generic version, so a method might return a plain TransactionResult (which just signals if a data call failed or succeeded, with extra messaging as needed), or a TransactionResult<string> which is the former, plus a string field which holds the return value (in this case, a string).

Now I can just inspect the returned object's 'Success' field, without having to know that "-1" means "string not found". I could even wrap my "ResultObject" property with a backing private field and check it for null when setting (if I was so inclined). No more error handling for common stuff, and overall its been quite easy to implement.

public class TransactionResult
    {
        public bool Success { get; set; }
        public ValidationResult[] TransactionErrors { get; private set; }
        public string[] SuccessMessages { get; private set; } // in case you need to signal a partial success, or a custom result message

        public TransactionResult()
        {
            TransactionErrors = new ValidationResult[0];
            SuccessMessages = new string[0];
            Success = true; // Easier to let it be 'true' by default, and 'false' only when you add an error or set it manually!
        }

        public void AddTransactionError(string errorMessage, string[] memberNames = null)
        {
            var newError = new ValidationResult(errorMessage, memberNames ?? new[] { "" });
            TransactionErrors = TransactionErrors.Concat(new[] { newError }).ToArray();
            Success = false;
        }

        public void MergeWithErrorsFrom(TransactionResult transaction)
        {
            if (transaction == null || !transaction.TransactionErrors.Any())
                return;

            SuccessMessages = SuccessMessages.Concat(transaction.SuccessMessages).ToArray();
            TransactionErrors = TransactionErrors.Concat(transaction.TransactionErrors).ToArray();
            if (TransactionErrors.Any())
                Success = false;
        }

        public void AddSuccessMessage(string message)
        {
            SuccessMessages = SuccessMessages.Concat(new[] { message }).ToArray();
        }
    }

    public class TransactionResult<T> : TransactionResult
    {
        public T ResultObject { get; set; }

        public void MergeWithResultsFrom(TransactionResult<T> transaction)
        {
            MergeWithErrorsFrom(transaction);
            ResultObject = transaction.ResultObject;
        }
    }

The above class uses the System.ComponentModel.DataAnnotations.ValidationResult class as its sorta backing "error" class, since I'm working in MVC4, which means my results can bind to my UI pretty much automatically, but you could roll your own version of that error as well. I wouldn't mind working in a language where this sort of structure was automatically part of the language/framework instead of having to roll my own, but that's pretty opinionated.