答:
In Computer Science what concepts you learn will be of no use until you show it.The problem is of main concern that needs to be solved so algorithm is a brief planning of how the problem will be solved in general. Therefore it is of major concern in the Computer Science world.

I think almost every aspect of Computer Science need Algorithm Let me show you this
The following list would include various Computer Science areas and which algorithms they use.

Automata

Powerset construction. Algorithm to convert nondeterministic automaton to deterministic automaton.
Todd-Coxeter algorithm. Procedure for generating cosets.

Artificial intelligence

Alpha-beta. Alpha max plus beta min. Widely used in board games.
Ant-algorithms. The ant colony optimisation is a set of algorithms inspired by ant behavior to solve a problem, find the best path between two locations.
DE (Differential evolution). Solve the Chebyshev polynomial fitting problem.
Semi-Supervised Recognition of Sarcastic Sentences in Online Product Reviews.
Algortithm that recognize sacarsms or irony in a tweet or an online document. A such algorithm will be essential for humanoid robots programming too.

Computer vision

Epitome. Represent an image or video by a smaller one.
Counting objects in an image. Uses the connected-component labeling algorithm to first label each object, and count then the objects.
O'Carroll algorithm. From a mathematical conversion of insect vision, this algorithm evaluates how to get around avoiding objects.

Genetic algorithms

They uses three operator. selection (choose solution), reproduction (use choosen solutions to construct other ones), replacement (replace solution if better).

Fitness proportionate selection. Also known as roulette-wheel selection, is a function used for selecting solutions.
Truncation selection. Another method for selecting solutions, ordered by fitness.
Tournament selection. Select the best solution by a kind of tournament.
Stochastic universal sampling. The individuals are mapped to contiguous segments of a line, such that each individual's segment is equal in size to its fitness exactly as in roulette-wheel selection.

Neural networks

Hopfield net. Recurrent artificial neural network that serve as content-addressable memory systems with binary threshold units. They converge to a stable state.
Backpropagation. Supervised learning technique used for training artificial neural networks.
Self-organizing map (Kohonen map). Neural networks trained using unsupervised learning to produce low dimensional (2D, 3D) representation of the training samples. Good for visualizing high-dimensional data.

Bioinformatics

Needleman-Wunsch. Performs a global alignment on two sequences, for protein or nucleotide sequences.
Smith-Waterman. Variation of the Needleman-Wunsch.

Compression

Lossless compression algorithms

Burrows-Wheeler transform. Preprocessing useful for improving lossless compression.
Deflate. Data compression used by ZIP.
Delta encoding. Aid to compression of data in which sequential data occurs frequently.
Incremental encoding. Delta encoding applied to sequences of strings.
LZW. (Lempel-Ziv-Welch). Successor of LZ78. Builds a translation table from the data to compress. Is used by the GIF graphical format.
LZ77 and 78. The basis of further LZ variations (LZW, LZSS, ...). They are both dictionary coders.
LZMA. Short for Lempel-Ziv-Markov chain-Algorithm.
LZO. Data compression algorithm that is focused on speed.
PPM (Prediction by Partial Matching). Adaptive statistical data compression technique based on context modeling and prediction.
Shannon-Fano coding. Constructs prefix codes based on a set of symbols and their probabilities.
Truncated binary. An entropy encoding typically used for uniform probability distributions with a finite alphabet. Improve binary encoding.
Run-length encoding. Primary compression that replaces a sequence of same code by the number of occurences.
Sequitur. Incremental grammar inference on a string.
EZW (Embedded Zerotree Wavelet). Progressive encoding to compress an image into a bit stream with increasing accuracy. May be lossy compression also with better results.

Entropy encoding
Coding scheme that assigns codes to symbols so as to match code lengths with the probabilities of the symbols .

Huffman coding. Simple lossless compression taking advantage of relative character frequencies.
Adaptive Huffman coding. Adaptive coding technique based on Huffman coding.
Arithmetic coding. Advanced entropy coding.
Range encoding. Same as arithmetic coding, but looked at in a slightly different way.
Unary coding. Code that represents a number n with n ones followed by a zero.
Elias delta, gamma, omega coding. Universal code encoding the positive integers.
Fibonacci coding. Universal code which encodes positive integers into binary code words.
Golomb coding. Form of entropy coding that is optimal for alphabets following geometric distributions.
Rice coding. Form of entropy coding that is optimal for alphabets following geometric distributions.

Lossy compression algorithms

Linear predictive coding. Lossy compression by representing the spectral envelope of a digital signal of speech in compressed form.
A-law algorithm. Standard companding algorithm.
Mu-law algorithm. Standard analog signal compression or companding algorithm.
Fractal compression. Method used to compress images using fractals.
Transform coding. Type of data compression for data like audio signals or photographic images.
Vector quantization. Technique often used in lossy data compression.
Wavelet compression. Form of data compression well suited for image and audio compression.

Cryptography

Secret key (symmetric encryption)

Use a secret key (or a pair of directly related keys) for both decryption and encryption.

Advanced Encryption Standard (AES), also known as Rijndael.
Blowfish. Designed by Schneier as a general-purpose algorithm, intended as a replacement for the aging DE.
Data Encryption Standard (DES), formerly DE Algorithm.
IDEA (International Data Encryption Algorithm). Formerly IPES (Improved PES), another replacement for DES. Is used by PGP (Pretty Good Privacy). Performs transformations on data splitted in blocks, using a key.
RC4 or ARC4. Stream cipher widely-used in protocols such as SSL for Internet traffic and WEP for wireless networks.
Tiny Encryption Algorithm. Easy to implement block cipher algorithme using some formulas.
PES (Proposed Encryption Standard). Older name for IDEA.

Public key (asymmetric encryption)

Use a pair of keys, designated as public key and private key. The public key encrypt the message, only the private key permits to decrypt it.

DSA (Digital Signature Algorithm). Generate keys with prime and random numbers. Was used by US agencies, and now public domain.
ElGamal. Based on Diffie-Hellman, used by GNU Privacy Guard software, PGP, and other cryptographic systems.
RSA (Rivest, Shamir, Adleman). Widely used in electronic commerce protocols. Use prime numbers.
Diffie-Hellman (Merkle) key exchange (or exponential key exchange). Method and algorithm to share secret over an unprotected communications channel. Used by RSA.
NTRUEncrypt. Make use of rings of polynomials with convolution multiplications.

Message digest functions

A message digest is a code resulting of the encryption of a string or data of any length, processed by a hash function.

MD5. Used for checking ISO images of CDs or DVDs.
RIPEMD (RACE Integrity Primitives Evaluation Message Digest). Based upon the principles of MD4 and similar to SHA-1.
SHA-1 (Secure Hash Algorithm 1). Most commonly used of the SHA set of related cryptographic hash functions. Was designed by the NSA agency.
HMAC. keyed-hash message authentication.
Tiger (TTH). Usually used in Tiger tree hashes.

Cryptographic using pseudo-random numbers
See. Random Number Generators

Techniques in cryptography

Secret sharing, Secret Splitting, Key Splitting, M of N algorithms.

Shamir's secret sharing scheme. This is a formula based on polynomial interpolation.
Blakley's secret sharing scheme. Is geometric in nature, the secret is a point in an m-dimensional space.

Other techniques and decryption

Subset sum. Given a set of integers, does any subset sum equal zero? Used in cryptography.
Shor's algorithm. Quantum algorithm able to decrypt a code based on asymetric functions such as RSA.

Geometry

Gift wrapping. Determining the convex hull of a set of points.
Gilbert-Johnson-Keerthi distance. Determining the smallest distance between two convex shapes.
Graham scan. Determining the convex hull of a set of points in the plane.
Line segment intersection. Finding whether lines intersect with a sweep line algorithm.
Point in polygon. Tests whether a given point lies within a given.
Ray/Plane intersection.
*Line/Triangle intersection.* Particular case of Ray/Plane intersection.
Polygonization of implicit surfaces. Approximate an implicit surface with a polygonal representation.
Triangulation. Method to evaluate the distance to a point from angles to other points, whose distance is known.

Graphs
3D Surface Tracker Technology. Process to add images on walls in a video while hidden surfaces are taken into account.
Bellman-Ford. Computes shortest paths in a weighted graph (where some of the edge weights may be negative).
Dijkstra's algorithm. Computes shortest paths in a graph with non-negative edge weights.
Perturbation methods. An algorithm that computes a locally shortest paths in a graph.
Floyd-Warshall. Solves the all pairs shortest path problem in a weighted, directed graph.
Floyd's cycle-finding. Finds cycles in iterations.
Johnson. All pairs shortest path algorithm in sparse weighted directed graph.
Kruskal. Finds a minimum spanning tree for a graph.
Prim's. Finds a minimum spanning tree for a graph. Also called DJP , Jarník or Prim–Jarník algorithm.
*Boruvka.* Finds a minimum spanning tree for a graph.
Ford-Fulkerson. Computes the maximum flow in a graph.
Edmonds-Karp. Implementation of Ford-Fulkerson.
Nonblocking Minimal Spanning Switch. For a telephone exchange.
Woodhouse-Sharp. Finds a minimum spanning tree for a graph.
Spring based. Algorithm for graph drawing.
Hungarian. Algorithm for finding a perfect matching.
Coloring algorithm. Graph coloring algorithm.
Nearest neighbour. Find nearest neighbour.
Topological sort. Sort a directed acyclic graph in such a manner that each node comes before all nodes to which it has edges (according to directions).
Tarjan's off-line least common ancestors algorithm. Compute lowest common ancestors for pairs of nodes in a tree.

Graphics

Bresenham's line algorithm. Uses decision variables to plots a straight line between 2 specified points.
Landscape Draw a 3D scenery.
*DDA line algorithm.* Uses floating-point math to plots a straight line between 2 specified points.
Flood fill. Fills a connected region with a color.
Image Restoring. Restore photo, improve images.
Xiaolin Wu's line algorithm. Line antialiasing.
Painter's algorithm. Detects visible parts of a 3-dimensional scenery.
Ray tracing. Realistic image rendering.
Phong shading. An illumination model and an interpolation method in 3D computer graphics.
Gouraud shading. Simulate the differing effects of light and colour across the surface of a 3D object.
Scanline rendering. Constructs an image by moving an imaginary line.
Global illumination. Considers direct illumination and reflection from other objects.
Interpolation. Constructing new data points such as in digital zoom.
Resynthesizer. Remove an object on a photo and rebuild the background Used by Photoshop and The Gimp. Resynthesizer tutorial .
Slope-intercept algorithm. It is an implementation of the slope-intercept formula for drawing a line.
Spline interpolation. Reduces error with Runge's phenomenon.
3D Surface Tracker Technology. Adding images or vidéo on walls in a vidéo, hidden surfaces being taken into account.

Lists, arrays and trees

Searching

Dictionary search. See predictive search.
Selection algorithm. Finds the kth largest item in a list.
Binary search algorithm. Locates an item in a sorted list.
Breadth-first search. Traverses a graph level by level.
Depth-first search. Traverses a graph branch by branch.
Best-first search. Traverses a graph in the order of likely importance using a priority queue.
A tree search.* Special case of best-first search that uses heuristics to improve speed.
Uniform-cost search. A tree search that finds the lowest cost route where costs vary.
Predictive search. Binary like search which factors in magnitude of search term versus the high and low values in the search.
Hash table. Associate keys to items in an unsorted collection, to retrieve them in a linear time.
Interpolated search. See predictive search.

Sorting

Binary tree sort. Sort of a binary tree, incremental, similar to insertion sort.
Bogosort. Inefficient random sort of a desk card.
Bubble sort. For each pair of indices, swap the items if out of order.
Bucket sort. Split a list in buckets and sort them individually. Generalizes pigeonhole sort.
Cocktail sort (or bidirectional bubble, shaker, ripple, shuttle, happy hour sort). Variation of bubble sort that sorts in both directions each pass through the list.
Comb sort. Efficient variation of bubble sort that eliminates "turtles", the small values near the end of the list and makes use of gaps bewteen values.
Counting sort. It uses the range of numbers in the list A to create an array B of this length. Indexes in B are used to count how many elements in A have a value less than i.
Gnome sort. Similar to insertion sort except that moving an element to its proper place is accomplished by a series of swaps, as in bubble sort.
Heapsort. Convert the list into a heap, keep removing the largest element from the heap and adding it to the end of the list.
Insertion sort. Determine where the current item belongs in the list of sorted ones, and insert it there.
Introsort. Or introspective sort. It begins in quicksort and switches to heapsort at certain recursion level.
Merge sort. Sort the first and second half of the list separately, then merge the sorted lists.
Pancake sort. Reverse elements of some prefix of a sequence.
Pigeonhole sort. Fill an empty array with all elements of an array to be sorted, in order.
Postman sort. Hierarchical variant of bucket sort, used by post offices.
Quicksort. Divide list into two, with all items on the first list coming before all items on the second list.; then sort the two lists. Often the method of choice.
Radix sort. Sorts keys associated to items, or integer by processing digits.
Selection sort. Pick the smallest of the remaining elements, add it to the end of the sorted list.
Shell sort. Improves insertion sort with use of gaps between values.
Smoothsort. See heapsort.
Stochastic sort. See bogosort.

and many more...

Title: Is programming or computer science in general, all about algorithms?

Body:
As a grad student, I find it more and more common for prestigious companies (like Google, Facebook, Microsoft,...) to put algorithm questions in their test and interviews. A few startups I applied to also asked about algorithms. I wonder if algorithms fluency is the most important thing for software developer in those companies?

If the answer being yes, what are the best method or resources for one to learn & practice about algorithms effectively? I can't seem to get interested in solving seemingly too complicated problems found in most textbook or websites. Though easily understand basic algorithms (like quicksort, bubblesort,...), I find it immensely difficult to remember and reuse them later.

Thanks.

P/S: If you ask me what I like, it's building good softwares to solve users' problems innovatively. I suppose that does not necessarily mean the software has to be very complicated.


问:
Real world T4 usage
How many of you have ever used (or even know of)  the Visual Studio Text Templating Engine?

I mean, T4 templates have been around for quite some years now. The where initially included in Visual Studio 2005 SDK, and they where moved to Visual Studio 2008 core just before releasing it.

What I want to know is if this is really used out there. I mean, why doesn't the VS team invested in a descent minimal editor for the templates? It doesn't even have syntax couloring!
I know there are a couple of good T4 editors out there, but as they don't come built in, I tend to think that this is an unknown feature for most developers.

Have you ever used T4 templates in any of your projects? I'm really interested in answers that explain how they created their own T4 to accelerate, automate, or generate artifacts for your project.

答: One of my peer groups migrated a lot of custom templates from Codesmith to T4. They like it pretty well, but they have a couple of people who have been modifying and creating custom templates for years so the lack of tools and polish does not affect them so much. In my team we still use Codesmith.</p>

ContentLicense: CC BY-SA 2.5

Title: Real world T4 usage


问:
Where can I safely learn about computer security?
I find it really hard to find resources about computer security. I asked questions on message boards about key loggers and viruses and I got negative assumption from people assuming the the worse. Also, I don't think that I can trust random message boards.

I know that it is a broad topic, but are there any good websites that I can follow and learn from that are targeted to beginner with some samples?

I am a developer (or at least want to be one) and I have a CS degree if that helps.

答: The reality is that computer security is no as far removed from real world security models as most people, particularly the media and programmrs think. I believe that you ned to understand secuirity concepts before you can understand computer security. Hence my advise if to read up on the works of Bruce Schneier. The book is Cryptonomicon is entertaining and insiteful reading. </p>

ContentLicense: CC BY-SA 3.0


问:
How do you keep code with continuations/callbacks readable?
Summary: Are there some well-established best-practice patterns that I can follow to keep my code readable in spite of using asynchronous code and callbacks?

I'm using a JavaScript library that does a lot of stuff asynchronously and heavily relies on callbacks. It seems that writing a simple "load A, load B, ..." method becomes quite complicated and hard to follow using this pattern.

Let me give a (contrived) example. Let's say I want to load a bunch of images (asynchronously) from a remote web server. In C#/async, I'd write something like this:

```<code>disableStartButton();

foreach (myData in myRepository) {
    var result = await LoadImageAsync("http://my/server/GetImage?" + myData.Id);
    if (result.Success) {
        myData.Image = result.Data;
    } else {
        write("error loading Image " + myData.Id);
        return;
    }
}

write("success");
enableStartButton();
</code>```

The code layout follows the "flow of events": First, the start button is disabled, then the images are loaded (await ensures that the UI stays responsive) and then the start button is enabled again.

In JavaScript, using callbacks, I came up with this:

```<code>disableStartButton();

var count = myRepository.length;

function loadImage(i) {
    if (i >= count) {
        write("success");
        enableStartButton();
        return;
    }

    myData = myRepository[i];
    LoadImageAsync("http://my/server/GetImage?" + myData.Id,
        function(success, data) {
            if (success) {
                myData.Image = data;
            } else {
                write("error loading image " + myData.Id);
                return;
            }
            loadImage(i+1);
        }
    );
}

loadImage(0);
</code>```

I think the drawbacks are obvious: I had to rework the loop into a recursive call, the code that's supposed to be executed in the end is somewhere in the middle of the function, the code starting the download (loadImage(0)) is at the very bottom, and it's generally much harder to read and follow. It's ugly and I don't like it.

I'm sure that I'm not the first one to encounter this problem, so my question is: Are there some well-established best-practice patterns that I can follow to keep my code readable in spite of using asynchronous code and callbacks?

答:
Disclaimer: this answer doesn't answer specifically your problem, it's a generic answer to the question: "Are there some well-established best-practice patterns that I can follow to keep my code readable in spite of using asynchronous code and callbacks?"

From what I know, there is no "well-established" pattern to handle this. However, I've seen two kinds of methods used to avoid the nested callbacks nightmares.

1/ Using named functions instead of anonymous callbacks

```<code>    function start() {
        mongo.findById( id, handleDatas );
    }

    function handleDatas( datas ) {
        // Handle the datas returned.
    }
</code>```

This way, you avoid the nesting by sending the logic of the anonymous function in another function.

2/ Using a flow management library. I like to use Step, but it's just a matter of preference. It's the one LinkedIn uses, by the way.

```<code>    Step( {
        function start() {
            // the "this" magically sends to the next function.
            mongo.findById( this );
        },

        function handleDatas( el ) {
            // Handle the datas.
            // Another way to use it is by returning a value,
            // the value will be sent to the next function.
            // However, this is specific to Step, so look at
            // the documentation of the library you choose.
            return value;
        },

        function nextFunction( value ) {
            // Use the returned value from the preceding function
        }
    } );
</code>```

I use a flow management library when I use a lot of nested callbacks, because it's a lot more readable when there is a lot of code using it.

Body:
Disclaimer: this answer doesn't answer specifically your problem, it's a generic answer to the question: "Are there some well-established best-practice patterns that I can follow to keep my code readable in spite of using asynchronous code and callbacks?"

From what I know, there is no "well-established" pattern to handle this. However, I've seen two kinds of methods used to avoid the nested callbacks nightmares.

1/ Using named functions instead of anonymous callbacks

    function start() {
        mongo.findById( id, handleDatas );
    }

    function handleDatas( datas ) {
        // Handle the datas returned.
    }

This way, you avoid the nesting by sending the logic of the anonymous function in another function.

2/ Using a flow management library. I like to use Step, but it's just a matter of preference. It's the one LinkedIn uses, by the way.

    Step( {
        function start() {
            // the "this" magically sends to the next function.
            mongo.findById( this );
        },

        function handleDatas( el ) {
            // Handle the datas.
            // Another way to use it is by returning a value,
            // the value will be sent to the next function.
            // However, this is specific to Step, so look at
            // the documentation of the library you choose.
            return value;
        },

        function nextFunction( value ) {
            // Use the returned value from the preceding function
        }
    } );

I use a flow management library when I use a lot of nested callbacks, because it's a lot more readable when there is a lot of code using it.


问:
When do I need to use a framework?
I am new to web programming and at this time I am learning about PHP. I would like to know when do I need to use a PHP framework such as CakePHP? What are things that this and other similar PHP frameworks offer for me?  And is it really important to use a framework to be a professional?

And can I create my own framework to provide the features I like into it?

答:
Frameworks are always good to use from the get go. It promotes code re usability, better readability and save a bit of development time.

I'm not very experienced with PHP, but I would encourage any framework usage.

Title: When do I need to use a framework?


问:
Is it customary to write Java domain objects / data transfer objects with public member variables on mobile platforms?
We performed a code review recently of mobile application Java code that was developed by an outside contractor and noticed that all of the domain objects / data transfer objects are written in this style:

```<code>public class Category {
    public String name;
    public int id;
    public String description;
    public int parentId;
}

public class EmergencyContact {
    public long id;
    public RelationshipType relationshipType;
    public String medicalProviderType;
    public Contact contact;
    public String otherPhone;
    public String notes;
    public PersonName personName;
}
</code>```

Of course, these members are then accessed directly everywhere else in the code. When we asked about this, the developers told us that this is a customary performance enhancement design pattern that is used on mobile platforms, because mobile devices are resource-limited environments. It doesn't seem to make sense; accessing private members via public getters/setters doesn't seem like it could add much overhead. And the added benefits of encapsulation seem to outweigh the benefits of this coding style.

Is this generally true? Is this something that is normally done on mobile platforms for the reasons given above? All feedback welcome and appreciated -

答:
You're right that on its own this probably not a significant difference in performance, but if it's a sign that in general the developers are keeping an eye on reducing the number of method calls (which are relatively expensive) and other overhead, it means they're doing a good job.

I'd discuss more in depth with them about their general coding philosophy to see if this is what they're aiming for.

(you're also making the assumption that public fields are unquestionably bad and getters/setters are the only way, which I don't think you'd find 100% support for, even on this site)


问:
Do you ever read language specs?
While trying to answer questions on SO site (particularly C++ questions) I find that most of the times one person or the other includes a quote from the standard. I myself have hardly read the language spec. Even when I try to read it, I can't get my head around the language used in the specs. So, my question to become a good developer in a particular language is it essential to read its spec and know all its dusty corners which invoke undefined/implementation defined behavior? </p>

答: I never read specs because I mostly use newer languages that don't have a formal, legalese spec yet.  My main languages are Python and D and AFAIK neither one has a formal spec, i.e. one whose intended audience is language lawyers/implementers, not average users.</p>

Title: Do you ever read language specs?


答:
the developers told us that this is a customary performance enhancement design pattern that is used on mobile platforms, because mobile devices are resource-limited environments

Assuming that authors did not bother to provide authoritative references to justify their "design decision", you are pretty safe to consider this a brain-fart of underqualified coders.

I've been in mobile Java racket for several years (still having 2 or 3K SO reputation in related tags because I use it as the way to not forget the basics), reviewed and wrote plenty of code, studied multiple specifications, tutorials and style guides - none of these ever mentioned even a shade of difference this kind design could possibly have compared to Java SE. If you're interested in finding such tutorials and guides yourself, check SO tag wikis in relevant tags like java-me, midp etc.

Title: Is it customary to write Java domain objects / data transfer objects with public member variables on mobile platforms?

Body:
We performed a code review recently of mobile application Java code that was developed by an outside contractor and noticed that all of the domain objects / data transfer objects are written in this style:

```<code>public class Category {
    public String name;
    public int id;
    public String description;
    public int parentId;
}

public class EmergencyContact {
    public long id;
    public RelationshipType relationshipType;
    public String medicalProviderType;
    public Contact contact;
    public String otherPhone;
    public String notes;
    public PersonName personName;
}
</code>```

Of course, these members are then accessed directly everywhere else in the code. When we asked about this, the developers told us that this is a customary performance enhancement design pattern that is used on mobile platforms, because mobile devices are resource-limited environments. It doesn't seem to make sense; accessing private members via public getters/setters doesn't seem like it could add much overhead. And the added benefits of encapsulation seem to outweigh the benefits of this coding style.

Is this generally true? Is this something that is normally done on mobile platforms for the reasons given above? All feedback welcome and appreciated -


答:
I'd recommend thinking carefully why you would use framework x, y or z. Every framework does not only provide functionality, that you don't have to implement yourself, but also a way of thinking. Just look at Spring, JEE, Rails or Django. Four frameworks providing enough to stomp out a web application in a little time.

But all four promote their own way of thinking on how web apps should be built. An if that doesn't fit your needs or match your flavor, you'll have a hard time taking things further.

This has been a very radical comparison but perhaps building up a small library does fit your needs better. But knowing frameworks will definitely help you work in larger teams or finding new jobs, since that shows that you interested in software development as a whole.

Title: When do I need to use a framework?

Body:
I am new to web programming and at this time I am learning about PHP. I would like to know when do I need to use a PHP framework such as CakePHP? What are things that this and other similar PHP frameworks offer for me?  And is it really important to use a framework to be a professional?

And can I create my own framework to provide the features I like into it?


答:
Is this generally true?  Is this something that is normally done on mobile platforms for the reasons given above?

It may be true that historically it was done this way; e.g. because earlier mobile platforms are particularly resource constrained ... or have particularly poor optimizers ( not naming any names :-) ).

But I don't think that "customary" or "historical practice" is relevant.  You have an absolute right to say what coding standards you want your contractor to adhere to.  Besides, if your platform is not resource constrained (or whatever) then your contractor's "customary practice" justification is empty ... because the conditions that make this unfortunate practice acceptable do not exist.


问:
If immutable objects are good, why do people keep creating mutable objects?
If immutable objects¹ are good, simple and offer benefits in concurrent programming why do programmers keep creating mutable objects²?

I have four years of experience in Java programming and as I see it, the first thing people do after creating a class is generate getters and setters in the IDE (thus making it mutable). Is there a lack of awareness or can we get away with using mutable objects in most scenarios?

¹ Immutable object is an object whose state cannot be modified after it is created.

² Mutable object is an object which can be modified after it is created.

答:
Both mutable and immutable objects have their own uses, pros and cons.

Immutable objects do indeed make life simpler in many cases. They are especially applicable for value types, where objects don't have an identity so they can be easily replaced. And they can make concurrent programming way safer and cleaner (most of the notoriously hard to find concurrency bugs are ultimately caused by mutable state shared between threads). However, for large and/or complex objects, creating a new copy of the object for every single change can be very costly and/or tedious. And for objects with a distinct identity, changing an existing objects is much more simple and intuitive than creating a new, modified copy of it.

Think about a game character. In games, speed is top priority, so representing your game characters with mutable objects will most likely make your game run significantly faster than an alternative implementation where a new copy of the game character is spawned for every little change.

Moreover, our perception of the real world is inevitably based on mutable objects. When you fill up your car with fuel at the gas station, you perceive it as the same object all along (i.e. its identity is maintained while its state is changing) - not as if the old car with an empty tank got replaced with consecutive new car instances having their tank gradually more and more full. So whenever we are modeling some real-world domain in a program, it is usually more straightforward and easier to implement the domain model using mutable objects to represent real-world entities.

Apart from all these legitimate reasons, alas, the most probable cause why people keep creating mutable objects is inertia of mind, a.k.a. resistance to change. Note that most developers of today have been trained well before immutability (and the containing paradigm, functional programming) became "trendy" in their sphere of influence, and don't keep their knowledge up to date about new tools and methods of our trade - in fact, many of us humans positively resist new ideas and processes. "I have been programming like this for nn years and I don't care about the latest stupid fads!"


答:
From my point of view, that's a lack of awareness. If you look at other known JVM languages (Scala, Clojure), mutable objects are seen rarely in the code and that's why people start using them in scenarios where single threading is not enough.

I'm currently learning Clojure and have a little experience in Scala (4 years + in Java as well) and your coding style changes because of the awareness of state.

Title: If immutable objects are good, why do people keep creating mutable objects?

Body:
If immutable objects¹ are good, simple and offer benefits in concurrent programming why do programmers keep creating mutable objects²?

I have four years of experience in Java programming and as I see it, the first thing people do after creating a class is generate getters and setters in the IDE (thus making it mutable). Is there a lack of awareness or can we get away with using mutable objects in most scenarios?

¹ Immutable object is an object whose state cannot be modified after it is created.

² Mutable object is an object which can be modified after it is created.


问:
finding houses within a radius
During an interview I was asked given the following: A real estate application that lists all houses that are currently on the market (i.e., for sale) within a given distance (say for example the user wants to find all houses within 20 miles), how would you design your application (both data structure and alogirithm) to build this type of service?

Any ideas? How would you implement it? I told him I didn't know becaue I've never done any geo-related stuff before.

答:
They are probably after an answer mentioning spatial indexing, most likely by selecting a database that provides spatial indexing out of the box, but you might also get a few points by mentioning it can be implemented in the application itself if needed e.g. by implementing an R-Tree (might be handy if the DB selection is fixed for other reasons? but also demonstrates you know how spatial databases work). Spatial indexing will allow you to rapidly get a subset of locations that fit inside a search box, you can refine this further by calculating actual distance (if necessary, the rectangle alone may be good enough of course) for each one to give a true search circle/ellipse

Given that distances are likely 20M or less you are probably OK assuming a flat earth to calculate distance though you will start to see noticeable errors toward the 20M end, if much larger ranges are needed accurately you would also need to start looking at better distance models for the globe e.g. Haversine distance

there are also of course a myriad other details that could be discussed e.g. UI design, DB schema which could be whole topics in their own right

Title: finding houses within a radius


答:
Likely the easiest way is to use a quadtree to store the locations of your houses, assuming distributed in a 2D landscape. Searching should be fairly straightforward.

If you're using a GIS-enabled RDBMS to store your stuff, then you really need not worry about that. See this question for some info on performance of the lead players.

Title: finding houses within a radius

Body:
During an interview I was asked given the following: A real estate application that lists all houses that are currently on the market (i.e., for sale) within a given distance (say for example the user wants to find all houses within 20 miles), how would you design your application (both data structure and alogirithm) to build this type of service?

Any ideas? How would you implement it? I told him I didn't know becaue I've never done any geo-related stuff before.


问:
Getting data from a webpage in a stable and efficient way
Recently I've learned that using a regex to parse the HTML of a website to get the data you need isn't the best course of action.

So my question is simple: What then, is the best / most efficient and a generally stable way to get this data?

I should note that:

There are no API's

There is no other source where I can get the data from (no databases, feeds and such)

There is no access to the source files. (Data from public websites)

Let's say the data is normal text, displayed in a table in a html page

I'm currently using python for my project but a language independent solution/tips would be nice.

As a side question: How would you go about it when the webpage is constructed by Ajax calls?

EDIT:

In the case of HTML parsing, I know that there is no actual stable way to get the data. As soon as the page changes, your parser is done for. What I mean with stable in this case is: an efficient way to parse the page, that always hands me the same results (for the same set of data obviously) provided that the page does not change.

答:
Parsing HTML is not a completely trivial task, since one has to deal with possibly incorrect markup (tag soup). During the years, browsers have implemented more or less the same strategy to deal with errors, and that algorithm has been christened in the HTML5 specification (yes, the HTML5 specification specifies what to do with things that are not HTML5).

The are libraries for all major languages to parse HTML, for instance this one.

In any case, what you will get is not stable in any sense. Each time the webpage format changes, you have to adapt your scraper.


答:
As a side question: How would you go about it when the webpage is constructed by Ajax calls?

If ajax calls are being made, then its very likely its either some POST or GET url with some variables.

I would examine the JavaScript to find out what the endpoints and parameters are. After that its very likely that either the data returned is json/xml/plain text or perhaps partial html.

Once you know the above information, you simply make a GET or POST request to that endpoint, and parse the returned data.

Title: Getting data from a webpage in a stable and efficient way

Body:
Recently I've learned that using a regex to parse the HTML of a website to get the data you need isn't the best course of action.

So my question is simple: What then, is the best / most efficient and a generally stable way to get this data?

I should note that:

There are no API's

There is no other source where I can get the data from (no databases, feeds and such)

There is no access to the source files. (Data from public websites)

Let's say the data is normal text, displayed in a table in a html page

I'm currently using python for my project but a language independent solution/tips would be nice.

As a side question: How would you go about it when the webpage is constructed by Ajax calls?

EDIT:

In the case of HTML parsing, I know that there is no actual stable way to get the data. As soon as the page changes, your parser is done for. What I mean with stable in this case is: an efficient way to parse the page, that always hands me the same results (for the same set of data obviously) provided that the page does not change.


问:
Why are there so many string classes in the face of std::string?
It seems to me that many bigger C++ libraries end up creating their own string type. In the client code you either have to use the one from the library (QString, CString, fbstring etc., I'm sure anyone can name a few) or keep converting between the standard type and the one the library uses (which most of the time involves at least one copy).

So, is there a particular misfeature or something wrong about std::string (just like auto_ptr semantics were bad)? Has it changed in C++11?

答:
Apart from the reasons posted here there is also another one - binary compability. Libraries' writers have no control over which std::string implementation you are using and whether it has the same memory layout as theirs.

std::string is a template, so its implementation is taken from your local STL headers. Now imagine that you are locally using some performance-optimised STL version, fully compatible with the standard. For example, you may have chosen to intrudce static buffer in each std::string to reduce the number of dynamic allocations and cache misses. As a result, memory layout and/or size of your implementation is different than library one's.

If only the layout is different, some std::string member function calls on instances passed from library to the client or the other way around may fail, dependending on which members were shifted.

If the size is different as well, all library types having std::string member will appear to have different sizeof when checked in the library and in the client code. Data members following std::string member will have offsets shifted as well, and any direct access/inline accessor called from the client will return rubbish, despite "looking OK" when debugging the library itself.

Bottomline - if library and the client code are compiled agains different std::string versions, they will link just fine, but it may result in some nasty, hard to understand bugs. If you change your std::string implementation all libraries exposing members from STL have to be recompiled to match the client's std::string layout. And because programmers want their libraries to be robust you'll rarely see std::string exposed anywhere.

To be fair, this applies to all STL types. IIRC they don't have standarised memory layout.

OwnerDisplayName: fish


问:
Pros and cons for open or close sourcing niche type products
One of the products I developed, is really a niche type application (2 or 3 other competitors in the market). Although for the most part, I'm the lone user of these applications, I'm thinking of selling the app right now.

My question is:

Because of the small market share and that it's a niche project would you advise I open or close my application?

答:
Pros:

people can contribute

possibly keep a buzz going

Cons:

your app is now free

people can rehack your code without your permission

I generally feel that academic code should be stringently open source. I feel that non-academic work should be able to be sold without guilt, with a caveat - if there is seminal work involved, it is a public service to publish the sketch of the seminal work and/or publish the source when the product is discontinued.


答:
For domain objects this is rubbish design - you want logic encapsulated on the domain objects, or you at least want to be able to add logic easily later.

But for DTO's I use similar design as they have no logic - they are DTOs.

I would assume that any performance overhead (which would be minuscule anyway) will be optimized out by the java virtual machine anyway.


问:
How to temporarily save the result of the query, to use in another?
I have this problem I think you may help me with.

P.S. I'm not sure how to call this, so if anyone finds a more appropriate title, please do edit.

Background

I'm making this application for searching bus transit lines.

Bus lines are a 3 digit number, and is unique and will never change.

The requirement is to be able to search for lines from stop A to stop B.

The user interface is already successful in hinting the user to only use valid stop names.

The requirement is to be able to display if a route has a direct line, and if not, display a 2-line and even 3-line combination.

Example:

I need to get from point A to point D. The program should show:

If there's a direct line A-D.

If not, display alternative, 2 line combos, such as A-C, C-D.

If there aren't any 2-line combos, search for 3-line combos: A-B, B-C, C-D.

Of course, the app should display bus line numbers, as well as when to switch buses.

What I have:

My database is structured as follows (simplified, actual database includes locations and times and whatnot):

```<code>+-----------+
| bus_stops |
+----+------+
| id | name |
+----+------+

+-------------------------------+
|    lines_stops_relationship   |
+-------------+---------+-------+
|  bus_line   | stop_id | order |
+-------------+---------+-------+
</code>```

Where lines_stops_relationship describe a many-to-many relationship between the bus lines and the stops.

Order, signifies the order in which stops appear in a single line. Not all lines go back and forth, and order has meaning (point A with order 2 comes after point B with order 1).

The Problem

We find out if a line can pass through the route easily enough. Just search for a single line which passes through both points in the correct order.

How can I find if there's a 2/3 line combo? I was thinking to search for a line which matches the source stop, and one for the destination stop, and see if I can get a common stop between them, where the user can switch buses. How do I remember that stop?

3 line combo is even trickier, I find a line for the source, and a line for the destination, and then what? Search for a line which has 2 stops I guess, but again, How do I remember the stops?

tl;dr

How do I remember results from a query to be able to use it again? I'm hoping to achieve this in a single query (for each, a query for 1-line routes, a query for 2, and a query for 3-line combos).

Note: I don't mind if someone suggests a completely different approach than what I have, I'm open to any solutions.

Will award any assistance with a cookie and an upvote. Thanks in advance!

答:
Let's say a user wants to go from $start_id to $end_id (both are valid stop_id values). You may use these queries to find a valid route from $start_id to $end_id:

Search for direct route (single line):

```<code>SELECT *
FROM bus_stops bs1, bus_stops bs2
WHERE bs1.stop_id=$start_id AND bs2.stop_id=$end_id AND bs1.bus_line=bs2.bus_line
</code>```

If there's no result with the previous query, then search for a route using 2 lignes:

```<code>SELECT *
FROM bus_stops bs1, bus_stops bs2, bus_stops bs3, bus_stops bs4
WHERE bs1.stop_id=$start_id
    AND bs1.bus_line=bs2.bus_line
AND bs2.stop_id=bs3.stop_id
    AND bs3.bus_line=bs4.bus_line
AND bs4.stop_id=$end_id
</code>```

Replace * with the fields you really need to retrieve.

OwnerDisplayName: Truth

Body:
Let's say a user wants to go from $start_id to $end_id (both are valid stop_id values). You may use these queries to find a valid route from $start_id to $end_id:

Search for direct route (single line):

SELECT *
FROM bus_stops bs1, bus_stops bs2
WHERE bs1.stop_id=$start_id AND bs2.stop_id=$end_id AND bs1.bus_line=bs2.bus_line

If there's no result with the previous query, then search for a route using 2 lignes:

SELECT *
FROM bus_stops bs1, bus_stops bs2, bus_stops bs3, bus_stops bs4
WHERE bs1.stop_id=$start_id
    AND bs1.bus_line=bs2.bus_line
AND bs2.stop_id=bs3.stop_id
    AND bs3.bus_line=bs4.bus_line
AND bs4.stop_id=$end_id

Replace * with the fields you really need to retrieve.


问:
Software Agent construction guidelines
I'm currently studying a Master in Computer Science and I am interested in working with Software Agents and C#.

I have found so much information regarding theoretical aspects but just a few articles focused on practical programming aspects such as architecture, design or implementation.

Could anybody suggest any good source that I could explore? I wouldn't like to "reinvent the wheel" but to contribute with something better to the current state of the art.

答:
Assuming you're interested in intelligent agents (a software agent is merely an application with a particular life cycle), as pointed out, Norvig's textbook and maybe Wooldridge's "Introduction to MAS" are a great start for the general stuff.

If you're going for rational agents, you may want to take a look at the BDI model and layered architectures. The study of reactive agents is inhomogeneous and closely tied to the problem - maybe give us more details of what you're interested in, if this is the case, because the implementations range from optimized for swarm intelligence to optimized for HW implementations.

For rational agents, one of the more high-profile issues is how agents communicate (because this impacts the architecture of the MAS), with one emerging communication standard (FIPA-ACL) superseding the old KQML. Using and dissecting JADE (in Java or through its .NET adapter if you only do C#) is a very good starting point for understanding the architecture of a MAS because a) JADE is a general, mature, FIPA-ACL compliant platform and b) JADE is relatively simple - it's actually in the curricula for many postgraduate MAS courses.

If you're less into the AI stuff and more into the hardcore CS stuff, take a look at the Actor model, which formalizes software agents. One way to start here for a C# developer is the (discontinued) Axum programming language from MS Research, which is an implementation of the actor model in a C#-like language.

OwnerDisplayName: lsoto

Title: Software Agent construction guidelines


答:
You may not want to make this drastic of a change at this point, but what you describe is exactly the use case for Graph Databases. Graph Databases are based on graph theory, which is what you are touching upon by trying to find a path between 'X' and 'Y' across a directed graph of bus routes.

If you haven't already looked into one, take a peek at something like Neo4J. It has a REST API and you can find PHP clients for it.

You'll find a bunch of people of Stack Overflow who could help with the implementation side of things.

Title: How to temporarily save the result of the query, to use in another?

Body:
I have this problem I think you may help me with.

P.S. I'm not sure how to call this, so if anyone finds a more appropriate title, please do edit.

Background

I'm making this application for searching bus transit lines.

Bus lines are a 3 digit number, and is unique and will never change.

The requirement is to be able to search for lines from stop A to stop B.

The user interface is already successful in hinting the user to only use valid stop names.

The requirement is to be able to display if a route has a direct line, and if not, display a 2-line and even 3-line combination.

Example:

I need to get from point A to point D. The program should show:

If there's a direct line A-D.

If not, display alternative, 2 line combos, such as A-C, C-D.

If there aren't any 2-line combos, search for 3-line combos: A-B, B-C, C-D.

Of course, the app should display bus line numbers, as well as when to switch buses.

What I have:

My database is structured as follows (simplified, actual database includes locations and times and whatnot):

```<code>+-----------+
| bus_stops |
+----+------+
| id | name |
+----+------+

+-------------------------------+
|    lines_stops_relationship   |
+-------------+---------+-------+
|  bus_line   | stop_id | order |
+-------------+---------+-------+
</code>```

Where lines_stops_relationship describe a many-to-many relationship between the bus lines and the stops.

Order, signifies the order in which stops appear in a single line. Not all lines go back and forth, and order has meaning (point A with order 2 comes after point B with order 1).

The Problem

We find out if a line can pass through the route easily enough. Just search for a single line which passes through both points in the correct order.

How can I find if there's a 2/3 line combo? I was thinking to search for a line which matches the source stop, and one for the destination stop, and see if I can get a common stop between them, where the user can switch buses. How do I remember that stop?

3 line combo is even trickier, I find a line for the source, and a line for the destination, and then what? Search for a line which has 2 stops I guess, but again, How do I remember the stops?

tl;dr

How do I remember results from a query to be able to use it again? I'm hoping to achieve this in a single query (for each, a query for 1-line routes, a query for 2, and a query for 3-line combos).

Note: I don't mind if someone suggests a completely different approach than what I have, I'm open to any solutions.

Will award any assistance with a cookie and an upvote. Thanks in advance!


问:
What electronic user-story-mapping tools can you recommend?
Agile software development relies heavily on a work item type called user stories.  For example, you have a backlog full of user stories and you can select a few of them to work on during the next sprint.

But where and how do you find user stories to put into the backlog?  There is a popular technique for doing that called story mapping.  Jeff Patton invented it and here is the definitive guide on how to do it.

The question is, what electronic tools are out there that support Patton's story-mapping technique?

I've done a bit of research, found Pivotal and Rally plug-ins (but I'm not a customer of either) and I'm currently experimenting with SilverStories.

What other tools are out there?  What have you used?  What do you (not) recommend?  Why?

UPDATE: Some people who wrote comments seem to lean towards an answer that applying this technique is simply impossible with an electronic tool and we should just accept that.  Can't someone write it up as an answer?

UPDATE (to clarify the question in light of Alex Feinman's comment): the question is about identifying options for story-mapping.  Since Jeff Patton's technique can obviously be done on a white-board with stickies, the question focuses on additional options that might be provided with electronic tools.  (Premature) commitment to any particular tool or class of tools is not the point of this question.

答: We use Pivotal Tracker to manage our projects. For story mapping, we use trackerstorymaps which syncs stories from PT and uses labels as mapping areas!</p>


问:
How do "custom software companies" deal with technical debt?
What are "custom software companies"?

By "custom software companies" I mean companies that make their money primarily from building custom, one off, bits of software. Example are agencies or middle-ware companies, or contractors/consultants like Redify.

What's the opposite of "custom software companies"?

The opposite of the above business model are companies that focus on long term products, whether they be deployable desktop/mobile apps, or SaaS software.

A sure fire way to build up technical debt:

I work for a company that attempts to focus on a suite of SaaS products. However, due to certain constraints we sometimes end up bending to the will of certain clients and we end building bits of custom software that can only be used for that client.

This is a sure fire way to incur technical debt. Now we have a bit of software to maintain that adds nothing to our core product.

If custom work is a sure fire way to build technical debt, how do agencies handle it?

So that got me thinking. Companies who don't have a core product as the center of their business model, well they're always doing custom software work. How do they cope with the notion of technical debt? How does it not drive them into technical bankruptcy?

答:
When I am faced with custom development requests I filter them through the cool filter which splits requests into 3 piles:

awesome things which will be useful for everyone and are
relatively easy to implement

awesome things which will be useful
for everyone and are hard to implement

stupid one-of things which
are only needed for this one client which doesn't really need them
anyways

Category 1 gets implemented in the current dev cycle.

Category 2 gets implemented in the next dev cycle.

Category 3 gets a quote of 1 man month of dev time after which most clients realize that their request is not worth it.

Honestly, this never failed and I don't think we ended up implementing any of the category 3 features.  And of course none of the customers walked (sales wouldn't have let me pull this off otherwise :)

(this experience was at an ISV company)

OwnerDisplayName: andy


答:
If you can bend the user-specific requirements into something that will be useful for everyone, great. If the client is willing to pay the ongoing support costs for the feature, also great. But if you're a small team and you find yourself struggling to support all your features, there's nothing for it but to make some hard decisions about the features you need the least, and then invest some time rooting them out of your codebase.

SaaS puts you in a good position to gather usage statistics. You should look at tooling up your features if you haven't already so that you can keep a track of who's using what. Our experience is that the most idiomatic customers are usually also the most dysfunctional; that guy who stamped his feet and held his breath until you gave him an export-to-MS-Access button probably hasn't used it in over a year. Some features are kept alive even though only one customer is using them, because that customer is loud and threatens to take his business elsewhere every time something's not to his satisfaction. Discontinuing the feature might cost you a customer now, but the time consumed in supporting that feature may cost you dozens of customers over the years. It is a measure of the quality of your management team, whether or not they are willing to make that sort of decision.

When you do discontinue a feature, make sure to announce the decision to your customers (or at least those affected) well in advance, anywhere between six months and three years is reasonable. In fact if you find yourself agreeing to build user-specific features you might try to get your sales staff to build an expiry date in from the start. Call it the "support lifetime", and make it clear that the longer they want it the more money it will cost. Try to provide workarounds for your clients so they're not left floundering when a feature goes, for example a script that converts your exported XML files into MS-access format, or a bit of advice about choosing a better RDBMS.

Something that's worked for us as a preventative measure is to get a report from our sales team sent to our development team and management on a monthly basis. This report covers feedback from clients -- what features are most popular, what features are most requested, what proposed features are generating the most buzz. This is interesting if you're a developer but the real benefit is to the sales team, who are now thinking a bit more about each feature in the context of the bigger picture, instead of sending in an endless stream of feature requests and prioritizing based on which client was the loudest. The effect has been to make our sales staff more hard-nosed when it comes to new feature requests in a negotiation because they are more conscious of where each feature might fit into the overall value proposition of our product.

Having modular code with lots of automated tests will help you when you're hacking features into your product and hacking them back out again, but ultimately this isn't a programming question but a management one. Writing code to make a sale is a fool's game.

Title: How do "custom software companies" deal with technical debt?

Body:
What are "custom software companies"?

By "custom software companies" I mean companies that make their money primarily from building custom, one off, bits of software. Example are agencies or middle-ware companies, or contractors/consultants like Redify.

What's the opposite of "custom software companies"?

The opposite of the above business model are companies that focus on long term products, whether they be deployable desktop/mobile apps, or SaaS software.

A sure fire way to build up technical debt:

I work for a company that attempts to focus on a suite of SaaS products. However, due to certain constraints we sometimes end up bending to the will of certain clients and we end building bits of custom software that can only be used for that client.

This is a sure fire way to incur technical debt. Now we have a bit of software to maintain that adds nothing to our core product.

If custom work is a sure fire way to build technical debt, how do agencies handle it?

So that got me thinking. Companies who don't have a core product as the center of their business model, well they're always doing custom software work. How do they cope with the notion of technical debt? How does it not drive them into technical bankruptcy?

OwnerDisplayName: dslh


答:
I've been reading "purely functional data structures", and it's made me realize that there are quite a few data structures that are much easier to implement using mutable objects.

To implement a binary search tree, you have to return a new tree every time: Your new tree will have had to make a copy of each node that has been modified (the un-modified branches are shared). For your insert function this isn't too bad, but for me, things got fairly inefficient quickly when I started to work on delete and re-balance.

The other thing to realize is that you can go years writing object oriented code, and never really realize how terrible shared mutable state can be, if your code isn't run in a way that will expose concurrency problems.


答:
There is a place for mutability.  Domain driven design principles provide a solid understanding of what should be mutable and what should be immutable.  If you think about it you will realize it is impractical to conceive of a system in which every change of state to an object requires the destruction and re-composition of it, and to every object that referenced it.  With complex systems this could easily lead to completely wiping and rebuilding the entire system's object graph

Most developers don't make anything where the performance requirements are significant enough that they need to focus on concurrency (or a lot of other issues that are universally considered good practice by the informed).

There are some things you simply can't do with immutable objects, like have bidirectional relationships.  Once you set an association value on one object, it's identity changes.  So, you set the new value on the other object and it changes as well.  The problem is the first object's reference is no longer valid, because a new instance has been created to represent the object with the reference.  Continuing this would just result in infinite regressions.  I did a little case study after reading your question, here's what it looks like.  Do you have an alternative approach that allows such functionality while maintaining immutability?

```<code>    public class ImmutablePerson {

     public ImmutablePerson(string name, ImmutableEventList eventsToAttend)
     {
          this.name = name;
          this.eventsToAttend = eventsToAttend;
     }
     private string name;
     private ImmutableEventList eventsToAttend;

     public string Name { get { return this.name; } }

     public ImmutablePerson RSVP(ImmutableEvent immutableEvent){
         // the person is RSVPing an event, thus mutating the state
         // of the eventsToAttend.  so we need a new person with a reference
         // to the new Event
         ImmutableEvent newEvent = immutableEvent.OnRSVPReceived(this);
         ImmutableEventList newEvents = this.eventsToAttend.Add(newEvent));
         var newSelf = new ImmutablePerson(name, newEvents);
         return newSelf;
     }
    }

    public class ImmutableEvent {
     public ImmutableEvent(DateTime when, ImmutablePersonList peopleAttending, ImmutablePersonList peopleNotAttending){
         this.when = when;
         this.peopleAttending = peopleAttending;
         this.peopleNotAttending = peopleNotAttending;
     }
     private DateTime when;
     private ImmutablePersonList peopleAttending;
     private ImmutablePersonList peopleNotAttending;
     public ImmutableEvent OnReschedule(DateTime when){
           return new ImmutableEvent(when,peopleAttending,peopleNotAttending);
     }
     //  notice that this will be an infinite loop, because everytime one counterpart
     //  of the bidirectional relationship is added, its containing object changes
     //  meaning it must re construct a different version of itself to
     //  represent the mutated state, the other one must update its
     //  reference thereby obsoleting the reference of the first object to it, and
     //  necessitating recursion
     public ImmutableEvent OnRSVPReceived(ImmutablePerson immutablePerson){
           if(this.peopleAttending.Contains(immutablePerson)) return this;
           ImmutablePersonList attending = this.peopleAttending.Add(immutablePerson);
           ImmutablePersonList notAttending = this.peopleNotAttending.Contains( immutablePerson )
                                ? peopleNotAttending.Remove(immutablePerson)
                                : peopleNotAttending;
           return new ImmutableEvent(when, attending, notAttending);
     }
    }
    public class ImmutablePersonList
    {
      private ImmutablePerson[] immutablePeople;
      public ImmutablePersonList(ImmutablePerson[] immutablePeople){
          this.immutablePeople = immutablePeople;
      }
      public ImmutablePersonList Add(ImmutablePerson newPerson){
          if(this.Contains(newPerson)) return this;
          ImmutablePerson[] newPeople = new ImmutablePerson[immutablePeople.Length];
          for(var i=0;i<immutablePeople.Length;i++)
              newPeople[i] = this.immutablePeople[i];
          newPeople[immutablePeople.Length] = newPerson;
      }
      public ImmutablePersonList Remove(ImmutablePerson newPerson){
          if(immutablePeople.IndexOf(newPerson) != -1)
          ImmutablePerson[] newPeople = new ImmutablePerson[immutablePeople.Length-2];
          bool hasPassedRemoval = false;
          for(var i=0;i<immutablePeople.Length;i++)
          {
             hasPassedRemoval = hasPassedRemoval || immutablePeople[i] == newPerson;
             newPeople[i] = this.immutablePeople[hasPassedRemoval ? i + 1 : i];
          }
          return new ImmutablePersonList(newPeople);
      }
      public bool Contains(ImmutablePerson immutablePerson){
         return this.immutablePeople.IndexOf(immutablePerson) != -1;
      }
    }
    public class ImmutableEventList
    {
      private ImmutableEvent[] immutableEvents;
      public ImmutableEventList(ImmutableEvent[] immutableEvents){
          this.immutableEvents = immutableEvents;
      }
      public ImmutableEventList Add(ImmutableEvent newEvent){
          if(this.Contains(newEvent)) return this;
          ImmutableEvent[] newEvents= new ImmutableEvent[immutableEvents.Length];
          for(var i=0;i<immutableEvents.Length;i++)
              newEvents[i] = this.immutableEvents[i];
          newEvents[immutableEvents.Length] = newEvent;
      }
      public ImmutableEventList Remove(ImmutableEvent newEvent){
          if(immutableEvents.IndexOf(newEvent) != -1)
          ImmutableEvent[] newEvents = new ImmutableEvent[immutableEvents.Length-2];
          bool hasPassedRemoval = false;
          for(var i=0;i<immutablePeople.Length;i++)
          {
             hasPassedRemoval = hasPassedRemoval || immutableEvents[i] == newEvent;
             newEvents[i] = this.immutableEvents[hasPassedRemoval ? i + 1 : i];
          }
          return new ImmutableEventList(newPeople);
      }
      public bool Contains(ImmutableEvent immutableEvent){
         return this.immutableEvent.IndexOf(immutableEvent) != -1;
      }
    }
</code>```

Body:
There is a place for mutability.  Domain driven design principles provide a solid understanding of what should be mutable and what should be immutable.  If you think about it you will realize it is impractical to conceive of a system in which every change of state to an object requires the destruction and re-composition of it, and to every object that referenced it.  With complex systems this could easily lead to completely wiping and rebuilding the entire system's object graph

Most developers don't make anything where the performance requirements are significant enough that they need to focus on concurrency (or a lot of other issues that are universally considered good practice by the informed).

There are some things you simply can't do with immutable objects, like have bidirectional relationships.  Once you set an association value on one object, it's identity changes.  So, you set the new value on the other object and it changes as well.  The problem is the first object's reference is no longer valid, because a new instance has been created to represent the object with the reference.  Continuing this would just result in infinite regressions.  I did a little case study after reading your question, here's what it looks like.  Do you have an alternative approach that allows such functionality while maintaining immutability?

    public class ImmutablePerson {

     public ImmutablePerson(string name, ImmutableEventList eventsToAttend)
     {
          this.name = name;
          this.eventsToAttend = eventsToAttend;
     }
     private string name;
     private ImmutableEventList eventsToAttend;

     public string Name { get { return this.name; } }

     public ImmutablePerson RSVP(ImmutableEvent immutableEvent){
         // the person is RSVPing an event, thus mutating the state
         // of the eventsToAttend.  so we need a new person with a reference
         // to the new Event
         ImmutableEvent newEvent = immutableEvent.OnRSVPReceived(this);
         ImmutableEventList newEvents = this.eventsToAttend.Add(newEvent));
         var newSelf = new ImmutablePerson(name, newEvents);
         return newSelf;
     }
    }

    public class ImmutableEvent {
     public ImmutableEvent(DateTime when, ImmutablePersonList peopleAttending, ImmutablePersonList peopleNotAttending){
         this.when = when;
         this.peopleAttending = peopleAttending;
         this.peopleNotAttending = peopleNotAttending;
     }
     private DateTime when;
     private ImmutablePersonList peopleAttending;
     private ImmutablePersonList peopleNotAttending;
     public ImmutableEvent OnReschedule(DateTime when){
           return new ImmutableEvent(when,peopleAttending,peopleNotAttending);
     }
     //  notice that this will be an infinite loop, because everytime one counterpart
     //  of the bidirectional relationship is added, its containing object changes
     //  meaning it must re construct a different version of itself to
     //  represent the mutated state, the other one must update its
     //  reference thereby obsoleting the reference of the first object to it, and
     //  necessitating recursion
     public ImmutableEvent OnRSVPReceived(ImmutablePerson immutablePerson){
           if(this.peopleAttending.Contains(immutablePerson)) return this;
           ImmutablePersonList attending = this.peopleAttending.Add(immutablePerson);
           ImmutablePersonList notAttending = this.peopleNotAttending.Contains( immutablePerson )
                                ? peopleNotAttending.Remove(immutablePerson)
                                : peopleNotAttending;
           return new ImmutableEvent(when, attending, notAttending);
     }
    }
    public class ImmutablePersonList
    {
      private ImmutablePerson[] immutablePeople;
      public ImmutablePersonList(ImmutablePerson[] immutablePeople){
          this.immutablePeople = immutablePeople;
      }
      public ImmutablePersonList Add(ImmutablePerson newPerson){
          if(this.Contains(newPerson)) return this;
          ImmutablePerson[] newPeople = new ImmutablePerson[immutablePeople.Length];
          for(var i=0;i<immutablePeople.Length;i++)
              newPeople[i] = this.immutablePeople[i];
          newPeople[immutablePeople.Length] = newPerson;
      }
      public ImmutablePersonList Remove(ImmutablePerson newPerson){
          if(immutablePeople.IndexOf(newPerson) != -1)
          ImmutablePerson[] newPeople = new ImmutablePerson[immutablePeople.Length-2];
          bool hasPassedRemoval = false;
          for(var i=0;i<immutablePeople.Length;i++)
          {
             hasPassedRemoval = hasPassedRemoval || immutablePeople[i] == newPerson;
             newPeople[i] = this.immutablePeople[hasPassedRemoval ? i + 1 : i];
          }
          return new ImmutablePersonList(newPeople);
      }
      public bool Contains(ImmutablePerson immutablePerson){
         return this.immutablePeople.IndexOf(immutablePerson) != -1;
      }
    }
    public class ImmutableEventList
    {
      private ImmutableEvent[] immutableEvents;
      public ImmutableEventList(ImmutableEvent[] immutableEvents){
          this.immutableEvents = immutableEvents;
      }
      public ImmutableEventList Add(ImmutableEvent newEvent){
          if(this.Contains(newEvent)) return this;
          ImmutableEvent[] newEvents= new ImmutableEvent[immutableEvents.Length];
          for(var i=0;i<immutableEvents.Length;i++)
              newEvents[i] = this.immutableEvents[i];
          newEvents[immutableEvents.Length] = newEvent;
      }
      public ImmutableEventList Remove(ImmutableEvent newEvent){
          if(immutableEvents.IndexOf(newEvent) != -1)
          ImmutableEvent[] newEvents = new ImmutableEvent[immutableEvents.Length-2];
          bool hasPassedRemoval = false;
          for(var i=0;i<immutablePeople.Length;i++)
          {
             hasPassedRemoval = hasPassedRemoval || immutableEvents[i] == newEvent;
             newEvents[i] = this.immutableEvents[hasPassedRemoval ? i + 1 : i];
          }
          return new ImmutableEventList(newPeople);
      }
      public bool Contains(ImmutableEvent immutableEvent){
         return this.immutableEvent.IndexOf(immutableEvent) != -1;
      }
    }


问:
List structures in memory
Could anyone give an overview of how list structures which are composed of a
head and a tail which references the rest of the list i.e linked list are represented in memory of the computer? Does the computer make use of cpu registers to hold the pointers the head and rest of the list?</p>

答:
Could anyone give an overview of how list structures which are composed of a head and a tail which references the rest of the list i.e linked list are represented in memory of the computer?

In a naive implementation, each node is allocated separately, so the nodes would be spread more or less randomly through the heap memory, wherever the memory allocator found some free space.

In practice, implementations typically try to improve cache locality by allocating space in bulk for a reasonable number of nodes at a time and keep track of that preallocated space.

Does the computer make use of cpu registers to hold the pointers the head and rest of the list?

Any good compiler, when compiling code that iterates over the list, will almost certainly produce machine code that does this, but that's implementation detail that has nothing to do with the concept of the data structure.

Title: List structures in memory


问:
Using visitor pattern with large object hierarchy
Context

I've been using with a hierarchy of objects (an expression tree) a "pseudo" visitor pattern (pseudo, as in it does not use double dispatch) :

```<code> public interface MyInterface
 {
      void Accept(SomeClass operationClass);
 }

 public class MyImpl : MyInterface
 {
      public void Accept(SomeClass operationClass)
      {
           operationClass.DoSomething();
           operationClass.DoSomethingElse();
           // ... and so on ...
      }
 }
</code>```

This design was, however questionnable, pretty comfortable since the number of implementations of MyInterface is significant (~50 or more) and I didn't need to add extra operations.

Each implementation is unique (it's a different expression or operator), and some are composites (ie, operator nodes that will contain other operator/leaf nodes).

Traversal is currently performed by calling the Accept operation on the root node of the tree, which in turns calls Accept on each of its child nodes, which in turn... and so on...

But the time has come where I need to add a new operation, such as pretty printing :

```<code> public class MyImpl : MyInterface
 {
      // Property does not come from MyInterface
      public string SomeProperty { get; set; }

      public void Accept(SomeClass operationClass)
      {
           operationClass.DoSomething();
           operationClass.DoSomethingElse();
           // ... and so on ...
      }

      public void Accept(SomePrettyPrinter printer)
      {
           printer.PrettyPrint(this.SomeProperty);
      }
 }
</code>```

I basically see two options :

Keep the same design, adding a new method for my operation to each derived class, at the expense of maintainibility (not an option, IMHO)

Use the "true" Visitor pattern, at the expense of extensibility (not an option, as I expect to have more implementations coming along the way...), with about 50+ overloads of the Visit method, each one matching a specific implementation ?

Question

Would you recommand using the Visitor pattern ? Is there any other pattern that could help solve this issue ?

答:
I have been using the visitor pattern to represent expression trees over the course of 10+ years on six large-scale projects in three programming languages, and I am very pleased with the outcome. I found a couple of things that made applying the pattern a lot easier:

Do not use overloads in the interface of the visitor

Put the type into the method name, i.e use

```<code>IExpressionVisitor {
    void VisitPrimitive(IPrimitiveExpression expr);
    void VisitComposite(ICompositeExpression expr);
}
</code>```

rather than

```<code>IExpressionVisitor {
    void Visit(IPrimitiveExpression expr);
    void Visit(ICompositeExpression expr);
}
</code>```

Add an "catch unknown" method to your visitor interface.

It would make it possible for users who cannot modify your code:

```<code>IExpressionVisitor {
    void VisitPrimitive(IPrimitiveExpression expr);
    void VisitComposite(ICompositeExpression expr);
    void VisitExpression(IExpression expr);
};
</code>```

This would let them build their own implementations of IExpression and IVisitor that "understands" their expressions by using run-time type information in the implementation of their catch-all VisitExpression method.

Provide a default do-nothing implementation of IVisitor interface

This would let users who need to deal with a subset of expression types build their visitors faster, and make their code immune to you adding more methods to IVisitor. For example, writing a visitor that harvests all variable names from your expressions becomes an easy task, and the code will not break even if you add a bunch of new expression types to your IVisitor later on.

Body:
I have been using the visitor pattern to represent expression trees over the course of 10+ years on six large-scale projects in three programming languages, and I am very pleased with the outcome. I found a couple of things that made applying the pattern a lot easier:

Do not use overloads in the interface of the visitor

Put the type into the method name, i.e use

IExpressionVisitor {
    void VisitPrimitive(IPrimitiveExpression expr);
    void VisitComposite(ICompositeExpression expr);
}

rather than

IExpressionVisitor {
    void Visit(IPrimitiveExpression expr);
    void Visit(ICompositeExpression expr);
}

Add an "catch unknown" method to your visitor interface.

It would make it possible for users who cannot modify your code:

IExpressionVisitor {
    void VisitPrimitive(IPrimitiveExpression expr);
    void VisitComposite(ICompositeExpression expr);
    void VisitExpression(IExpression expr);
};

This would let them build their own implementations of IExpression and IVisitor that "understands" their expressions by using run-time type information in the implementation of their catch-all VisitExpression method.

Provide a default do-nothing implementation of IVisitor interface

This would let users who need to deal with a subset of expression types build their visitors faster, and make their code immune to you adding more methods to IVisitor. For example, writing a visitor that harvests all variable names from your expressions becomes an easy task, and the code will not break even if you add a bunch of new expression types to your IVisitor later on.


答:
In my experience, using .NET environment, you can take advantage of HTML Agility Pack.

If the page is formatted as XHTML you can also use a regular XML parser. There's a lot out of there for any environment you can imagine.

For the side question about AJAX, you can use regular HTTP networking code to get data and parse it.

Again if your AJAX stack returns XML, you'll got a lot of choices. If it returns JSON, consider a library that allow you to map the stream to typed objects. In .NET I suggest you Newtonsoft.Json.

ContentLicense: CC BY-SA 4.0


答:
Without mutable objects you have no state.  Admittedly, this is a good thing if you can manage it and if there is any chance an object might be referenced from more than one thread.  But the program is going to be rather boring.  A lot of software, particularly web servers, avoids taking responsibility for mutable objects by pushing mutability off on databases, operating systems, system libraries, etc.  As a practical matter, this does free the programmer from mutability problems and makes web (and other) development affordable.  But the mutability is still there.

In general, you have three types of classes:  normal, non-thread-safe classes, that have to be carefully guarded and protected; immutable classes, which can be used freely; and mutable, thread-safe classes that can be used freely but which must be written with extreme care.  The first type is the troublesome one, with the worst ones being those that are thought to be of the third type.  Of course, the first type are the easy ones to write.

I usually end up with lots of normal, mutable classes that I have to watch very carefully.  In a multi-thread situation, the synchronization necessary slows everything down even when I can avoid a deadly embrace.  So I'm ususally making immutable copies of the mutable class and handing those off to whoever can use it.  A new immutable copy is needed every time the orignal mutates, so I imagine at times I may have a hundred copies of the original out there.  I'm utterly dependent on Garbage Collection.

In summary, non-thread-safe, mutable objects are fine if you are not using multiple threads.  (But multithreading is inflitrating everywhere--be careful!)  They can be used safely otherwise if you restrict them to local variables or rigorously synchronize them.  If you can avoid them by using other people's proven code (DBs, system calls, etc.) do so.  If you can use an immutable class, do so.  And I think, in general, people are either unaware of multithreading problems or are (sensibly) terrified of them and using all kinds of tricks to avoid multithreading (or rather, pushing responsibility for it elsewhere).

As a P.S., I sense that Java getters and setters are getting out of hand.  Check this out.


答: Java, in many cases mandates mutable objects, eg when you want to count or return something in a visitor or runnable, you need final variables, even without multithreading.</p>


问:
Is it common to declare class properties at the bottom of the class declaration?
I work in a PHP development shop and several new developers have joined the team.  One of the new members insists on declaring class properties at the bottom of the class declaration rather than the at the top, as one would normally expect.

In all my 5 years of working in web development, I have never seen this done. Is it a common coding style?

答:
Some dev environments get around these kinds of issues by using static analysis tools - StyleCop for .NET and Checkstyle for Java. If there are any for php, I'd suggest getting one.

Likewise, it doesn't so much matter what style you, or your company, uses - just that it stays consistent. If you can't find a static analysis tool for php, then just document known style issues (don't discuss it, just document what exists in current, stable source files that people haven't had style problems with) and call that the style guide.

This is an issue that can explode into all kinds of bikeshedding. It's important to not let it become an issue where people feel like they can provide input.

Toss that up onto your project wiki/shared folder/whatever and send out an e-mail to the team.

If you're not a lead/senior/someone with the authority to do this then give the doc to your lead and ask that they do it.

Enforce it, and change it as appropriate, in code reviews.


答: I think you've all missed the most obvious answer.  Most developers create mutable objects because mutability is the default in imperative languages.  Most of us have better things to do with our time than to constantly modify code away from the defaults--more correct or not. And immutability is not a panacea any more than any other approach.  It makes some things easier but makes others much more difficult as some answers have already pointed out.</p>


问:
What is the best practice to develop a visual component in Flex Hero?
What is the best practice to develop a visual component in Flex Hero?

I do it like this:
I consider a component has 2 "parts", the declarative part (the visual sub-components) which I define in the skin (just mxml) and the code part (event handlers...) which I define in an action script class. I load the skin in the ctor of the action script class. I also define skin parts, states, and I bind event handlers in the partAdded function.

I am having an argument about this; that I should define the component purely in an .mxml, with listeners in the script tag, and maybe attach a skin (but the skin should be loose - maybe for reuse :-?)

I come from .NET and maybe I am biased with the code behind pattern, and I am wondering from your experience and Adobe's intent, what is the best practice to usually implement a visual component?

答:
There are 3 ways to implement components.

1 pure ActionScript

Extend UIComponent.

2 MXML

MXML is just an abstraction of ActionScript. When compiling a Flex application, the source code will translate MXML to ActionScript. So, there is basically nothing wrong to have ActionScript in the fx:Script tag. There are some people who say, that a view should contain no logic (when implemented in MXML) but that is plain stupid, since you need ActionScript to define behavior.

3 SkinnableComponent (separation of behavior and visual appearance)

You extend SkinnableComponent and define a skin class. This is extremely helpful, when theming for different clients or load different visual appearances depending on a user's role.

The code behind aprouch can be achieved easily in ActionScript, wether be aggregation or inheritance. Also an MVVM can be implemented easily. But generally, that's not the pattern you use. Usually in Flex, you use MVC/MVCS.

You may want to take a look: Flash builder 4.6 - code behind approach


答:
I do read language standards on occasion, but not everybody does.  Certainly it's possible to be a very good developer without ever touching one.

What you need is the knowledge of the parts of the language you're going to use, and somewhere to go to find out about what the stuff you don't understand in other people's programs does.  You don't need to know exactly what constitutes undefined behavior if you make sure you never go near it.

To give an example, in C++ you could avoid starting any identifier name you write with an underscore.  There are rules as to what names starting with underscores are reserved for the implementation with what linkages that you could learn, or you could just stop writing your own identifiers with leading underscores.

Title: Do you ever read language specs?

Body: While trying to answer questions on SO site (particularly C++ questions) I find that most of the times one person or the other includes a quote from the standard. I myself have hardly read the language spec. Even when I try to read it, I can't get my head around the language used in the specs. So, my question to become a good developer in a particular language is it essential to read its spec and know all its dusty corners which invoke undefined/implementation defined behavior? </p>


答:
due to certain constraints we sometimes end up bending to the will of
  certain clients and we end building bits of custom software that can
  only be used for that client.

Your problem isn't that you're creating code that's used for only one client. The problem is that you're incorporating code that's used only for one client into a product that you're selling to many other clients who don't need that functionality.

Companies who don't have a core product as the center of their
  business model, well they're always doing custom software work. How do
  they cope with the notion of Technical Debt? How does it not drive
  them into Technical Bankruptcy?

They deliver the product. And then they move on. When you're developing a product under contract, everything you do on that project is for that one client. Any technical debt that may have accrued during development becomes the client's problem after the contract ends, and the developer moves on to another project for another client.

That doesn't mean that it's okay to do a crappy job, of course. Your number one goal is to make your client want to keep working with you, and doing quality work is the way to get there. It also doesn't mean that technical debt isn't a problem for contract developers. Even if you consistently write clean code yourself, chances are that you'll be hired at some point to work on a project that has already accrued a pile of debt. That can be good (the client wants to pay you to clean up the mess) or not (the client has no idea how bad the code is and doesn't understand why "just" adding a few more features is going to take so long).


答:
Overview

Weird. I just discover I worked several years, putting properties at the bottom of the class declaration. And, later, several years, putting properties at the top of the class declaration, and, I almost didn't notice the change ...

Answer

Both, cases are good, but, int this specific case, your developer should stick to this company standard.

Title: Is it common to declare class properties at the bottom of the class declaration?

Body:
I work in a PHP development shop and several new developers have joined the team.  One of the new members insists on declaring class properties at the bottom of the class declaration rather than the at the top, as one would normally expect.

In all my 5 years of working in web development, I have never seen this done. Is it a common coding style?


答:
I disagree with the premise that custom work is guaranteed to generate technical debt.  Avoiding technical debt doesn't mean avoiding certain kinds of functionality -- it means avoiding needless rigidity, dependency issues, and things that make a code base difficult (i.e. expensive) to change.  Custom functionality doesn't imply any of this -- it just implies a narrow base for the functionality.  So, they key is to factor as much common, reusable logic as possible out of the implementation as possible, leaving the custom, one-off stuff as its own standalone module that can be deployed for the requesting customer.

For instance, let's say that you had a deliverable that was an internal web application that your customers would install on an intranet.  One day, a customer comes along and offers to drive a dump truck full of money up to your company if you'll make a version for them that has a "rich client" desktop application instead of a browser interface.  Well, if your system is well architected and your dependencies well managed, this is just a matter of reusing the domain, data access, and service components while creating a new presentation component.  Technical debt is not a consideration, even though you have only one customer that wants to return to 1999 and have a desktop application for this.


问:
Is it okay to have people with multiple roles in a Scrum team?
I'm evaluating some Agile-style methodologies for possible introduction to my team.  With Scrum, is it allowable to have the same person perform multiple roles?  We have a small team of four developers and a web designer; we don't really have a lead (I fulfill this role), QA testers or business analysts, and all of our development tasks come from the CIO.  Automated testing is seen as a total waste of time, and everything focuses on speed and not quality.

What will happen is the CIO will come up with a development task (whether a feature or a bug) and give it to a developer (not to the whole team, to an individual, often in private or out of the blue) who is then expected to get it completed.  The CIO doesn't gather requirements beyond the initial idea (and this has bitten us before as we'll implement something only to find out that none of the end users can use the feature, because they weren't consulted or even informed about it before we developed it, and in a panic we'll be told to revert the change) but requires say in/approval of everything that we do.

First things first, is a Scrum style something to consider to introduce some standards and practices?  From reading, Scrum seems to rely on a bit more trust and communication and focuses more on project management than on development, which is something we are completely devoid of as we don't have any semblance of project management at present.

Second, if it can work is it unreasonable for someone, let's say myself, to act as both ScrumMaster and a developer?  Or for a developer to also be the Product Owner (although chances are this will be the CIO, who isn't a developer)?  I realize the Scrum Master and the Product Owner should be different people but at the same time I don't think we have anyone who has the qualities of a Product Owner (chances are it would turn into a "I need all these stories, I don't care how but get it done" type of deal and/or any freeze would be unfrozen on a whim).

It seems to me that I might need to pick and choose pieces of Scrum/XP/Lean to compensate for how things are done currently, as it's highly unlikely that the mentality can be changed; for instance Pair Programming would never fly (seen as a waste, you get half the tasks done if you need two people for everything), TDD would be a hard sell, but short cycles would be welcomed.

答:
As I noted here, if either the Scrum Master or the Product Owner have actionable tasks, they are also members of the Team.

That said, you are going to have serious problems going Agile unless you have real buy-in from both your CIO and your customers.  Is your CIO willing to abide by the fact he cannot add a work item mid-sprint, but will have to wait until the next?  Is he willing to prioritize the items to be developed?  From what you've written, it sounds like he owns what you do, and thus should be the Product Owner.  If he is unwilling, you're not going to succeed any more than you currently do.