问:
When is an API considered to be an embedded DSL?
What is the difference between an API and an embedded Domain Specific Language (DSL)?

Is it just syntax?

Consider an API like OpenGL.  How is that different from a graphics DSL?

In other words, if an API is sufficiently complex, can it be considered an embedded DSL?

答:
Here, from DslBoundary by Martin Fowler

From the article, my understanding is basically embedded DSLs and APIs are not so difference. But however, there is a bit difference here.

API emphasises on providing a new facility.

DSL does not just provide you a new facility but provide you a new syntax and the new way to coding also.

But if talk about external DSL it will be another story. The external DSL is just like an small programming language but for sure, it is not a general-purpose language that mean it can not solve all the problems but specific problem instead.


问:
What to call requirements that are "assumed"/"invisible"/"very obvious"
I'm looking for an umbrella term for all the nitty-gritty requirements that it's helpful to have specified up front but which the client never thinks about in his excitement about the product.

In the client's mind, they think of the headline requirements, the main user stories such as "I need to be able to view the status of all equipment" and "I need to receive automatically any notification equipment failure". But at times the mundane ones are there but the client never mentions them until the result differs from his unspoken expectations: "Well of course a user must be able to change his own password" and "Of course the user's session must time out after 30 mins"

Any development team can anticipate what's likely to be needed but is there a name for this category of requirements? Obvious/infrastructural/boring/details?

答:
I came across this from another site and wanted to add my two cents.

In my view the moment a client/manager starts using phrases like 'it is obvious' or 'it was obvious' the contract between the project owner and the developers breaks down.

When a developer implements something correctly, but misses something that was 'obvious' (but not specified) it will be the developer's fault; when they implement something that was not specified it will be their fault for not following the specification. This is how software development works in companies where the developers are blamed for every failing - usually with a weak manager who is happy to have people to offload the blame onto.

All requirements should be documented, explicitly, even if it seems that the obvious is being stated. That way there is no doubt about what is being built and a clear audit trail to explain why something is not as the client would have hoped.

ContentLicense: CC BY-SA 3.0


问:
Refactoring case-when statement
The Product model which can have 2 price tables. The method price calculates the price of the product based on product's price table which is defined by price_table field.

```<code>class Product < ActiveRecord::Model
  NORMAL_PRICE_TABLE = 1
  PROMOTIONAL_PRICE_TABLE = 2

  validates_presence_of :price_table

  def price(quantity)
    case price_table
    when NORMAL_PRICE_TABLE
      # calculate using normal price table rules
    when PROMOTIONAL_PRICE_TABLE
      # calculate using promotional price table rules
    else
      raise 'unknown data price'
    end
  end
end
</code>```

I want to refactor this code. The cleaner way I get is this:

```<code>class NormalPriceTable

  def initialize(raw_product_price, quantity)
    @raw_product_price = raw_product_price
    @quantity = quantity
  end

  def price
    # calculate
  end
end

class PromotionalPriceTable

  def initialize(raw_product_price, quantity)
    @raw_product_price = raw_product_price
    @quantity = quantity
  end

  def price
    # calculate
  end
end

class Product < ActiveRecord::Model
  NORMAL_PRICE_TABLE = 1
  PROMOTIONAL_PRICE_TABLE = 2

  def price(quantity)
    price_table_class.new(raw_price, quantity).price
  end

  def price_table_class
    case price_table
    when NORMAL_PRICE_TABLE
      NormalPriceTable
    when PROMOTIONAL_PRICE_TABLE
      NormalPriceTable.new(raw_price, quantity).price
    else
      raise 'unknown price table'
    end
  end
end
</code>```

Two questions:

It's possible to get rid of this case clause on Product class?

Which are the other alternatives to refactor this code?

答:
Replacing conditional with polymorphism (see Refactoring, Martin Fowler, page 255) is indeed the refactoring which comes into mind.

You've already did it partially, by:

Creating classes, one for normal price, another for the promotional one.

Moving the business logic to the newly created classes.

The remaining part is to:

Create the hierarchy. It makes sense to derive both normal and promotional prices tables from an abstract PriceTable class. While PriceTable class won't contain any logic, it will at least provide the common interface for other classes which use the price tables.

Determine the location of the separation between the normal and the promotional prices. You do it in Product class, but is it the best location? Or would it be better to inject the type of the price into the Product class? What about a Product class which doesn't care about the actual type of the price table, as soon as it corresponds to the interface of the abstract PriceTable class?

This is an open question. It depends completely on your business logic and the remaining part of the program. It looks like Product shouldn't care about the actual type of the price table, as soon as it is derived from the abstract ProductTable, but this also means that somewhere, you'll still have to do your choice of the appropriate concrete type (either in a switch or through a map as suggested by rudi). Maybe in a factory class. Maybe somewhere else.

Title: Refactoring case-when statement


答:
I think in the very most cases (not all?) you can replace a case condition with a hash; at least in OOP. Because what else is the case condition than choosing the right value based on the input parameter? This is exactly what a map does: Based on a key it returns a value.

But what if some logic is necessary instead of a value? Well in this case the map-value is a function / static method or a object that implements that certain logic.
For example if a new object is necessary each time that method is called, the map value can be a factory class that creates such object.
In strongly typed programming languages all "map value objects" should implement the same interface or extend the same class.

In your case the Product class could look like this:

```<code>class AbstractPriceTable
  def price
    raise 'unknown price table'
  end
end

class Product
  NORMAL_PRICE_TABLE = 1
  PROMOTIONAL_PRICE_TABLE = 2

  PRICE_TABLE_CLASSES = {
    1 => NormalPriceTable,
    2 => PromotionalPriceTable }

  def price(quantity)
    (PRICE_TABLE_CLASSES[price_table] || AbstractPriceTable).
      new(raw_price, quantity).price
  end
end
</code>```

With such a construct, case conditions would not be necessary anymore. The advantage of the map solution is the better extensibility and its more dynamic, as you could "register" more functions during runtime.
If you don't want that or need a simple fixed structure, case conditions may still be the better choice; at least better than massive if..elsif..elsif..else..end constructs!

btw: I took the idea for this refactoring from this talk.

Title: Refactoring case-when statement

Body:
The Product model which can have 2 price tables. The method price calculates the price of the product based on product's price table which is defined by price_table field.

```<code>class Product < ActiveRecord::Model
  NORMAL_PRICE_TABLE = 1
  PROMOTIONAL_PRICE_TABLE = 2

  validates_presence_of :price_table

  def price(quantity)
    case price_table
    when NORMAL_PRICE_TABLE
      # calculate using normal price table rules
    when PROMOTIONAL_PRICE_TABLE
      # calculate using promotional price table rules
    else
      raise 'unknown data price'
    end
  end
end
</code>```

I want to refactor this code. The cleaner way I get is this:

```<code>class NormalPriceTable

  def initialize(raw_product_price, quantity)
    @raw_product_price = raw_product_price
    @quantity = quantity
  end

  def price
    # calculate
  end
end

class PromotionalPriceTable

  def initialize(raw_product_price, quantity)
    @raw_product_price = raw_product_price
    @quantity = quantity
  end

  def price
    # calculate
  end
end

class Product < ActiveRecord::Model
  NORMAL_PRICE_TABLE = 1
  PROMOTIONAL_PRICE_TABLE = 2

  def price(quantity)
    price_table_class.new(raw_price, quantity).price
  end

  def price_table_class
    case price_table
    when NORMAL_PRICE_TABLE
      NormalPriceTable
    when PROMOTIONAL_PRICE_TABLE
      NormalPriceTable.new(raw_price, quantity).price
    else
      raise 'unknown price table'
    end
  end
end
</code>```

Two questions:

It's possible to get rid of this case clause on Product class?

Which are the other alternatives to refactor this code?

Body:
I think in the very most cases (not all?) you can replace a case condition with a hash; at least in OOP. Because what else is the case condition than choosing the right value based on the input parameter? This is exactly what a map does: Based on a key it returns a value.

But what if some logic is necessary instead of a value? Well in this case the map-value is a function / static method or a object that implements that certain logic.
For example if a new object is necessary each time that method is called, the map value can be a factory class that creates such object.
In strongly typed programming languages all "map value objects" should implement the same interface or extend the same class.

In your case the Product class could look like this:

class AbstractPriceTable
  def price
    raise 'unknown price table'
  end
end

class Product
  NORMAL_PRICE_TABLE = 1
  PROMOTIONAL_PRICE_TABLE = 2

  PRICE_TABLE_CLASSES = {
    1 => NormalPriceTable,
    2 => PromotionalPriceTable }

  def price(quantity)
    (PRICE_TABLE_CLASSES[price_table] || AbstractPriceTable).
      new(raw_price, quantity).price
  end
end

With such a construct, case conditions would not be necessary anymore. The advantage of the map solution is the better extensibility and its more dynamic, as you could "register" more functions during runtime.
If you don't want that or need a simple fixed structure, case conditions may still be the better choice; at least better than massive if..elsif..elsif..else..end constructs!

btw: I took the idea for this refactoring from this talk.


问:
How to iterate between these elements
I have an array of elements:

```<code>int[] elem = new int[] {A, B, C};
</code>```

I need to calculate the sum of ALL the combinations of those elements, where only some of the elements can be optionally selected. I will use the notation:

{0,1} mean element can be selected or not

{1} mean element is always selected.

Therefore, I need to calculate the following value, which could be something like:

```<code>result = {0,1}*A + {1}*B + {0,1}*C;
</code>```

I have tried to use nested for loops, like so:

```<code>for (int i=0;i<2;i++)
    for (int j=1;j<2;j++) // notice I start at 1, not 0, because B is {1}
        for (int k=0;k<2;k++)
            Console.Write( i*A + j*B + k*C );
</code>```

There are two problems with this:

These nested for loops are rather cumbersome

My example only has three elements, but the array could be of any size. For example, if the array had 10 elements, I would need to have 10 nested loops instead of three. I don't know to change the number of for loops based on the array size.

How would I iterate to get all sum values for all combinations in an arbitrary sized array?

答:
You are computing the sum of all the combinations of a set of numbers.

The number of combinations you have is 2n, where n is the number of numbers. For example, if your array had 5 elements, it would be 25 which is 32.  The combinations that you will be iterating through are the binary representations of 0..31.

00000

00001

00010

00011

...

11111

This has the additional constraint that the value must be .1... which can be easily filtered with a bitmask:

```<code> for i = 0 .. 2**elem.size - 1
     next unless i & 0b01000
</code>```

After this, you walk the bits and sum up the values:

```<code>    sum = 0
    mask = 0b00001  # written out, its just 1
    for j = 0 .. elem.size
        sum += elem[j] * ((i & mask) >> j)
        mask <<= 1
    print sum
</code>```

I'll admit to the possibility that there's a fencepost or off by one error in there, but this is fairly simple loops that may be even simpler (or harder) depending on the implementation language.  Just a little bit of bit math.

The key part of this is walking over the bits of i. We start out with a max with only the lowest bit set (0b00001 which is also just a plain old 1). Then, for each element of the array, do a bitwise and of the mask and i.  If the bit is set, this is a power of 2 (20, 21, 22 ...) that we then have to shift back the number of positions to make it either 1 (if the bit was set) or 0 (if the bit was not set).  This is done with the >> operator. This could also be done by dividing by the appropriate power of 2.

Once we get 1 or 0 back from the bit math, multiply it by the element at the associated array index and then add that to the sum.

To move to the next position, the mask is shifted to the left one bit (so that 0b00001 becomes 0b00010) which can also be done with a *= 2 if you didn't want to work with bits.

Title: How to iterate between these elements

Body:
You are computing the sum of all the combinations of a set of numbers.

The number of combinations you have is 2n, where n is the number of numbers. For example, if your array had 5 elements, it would be 25 which is 32.  The combinations that you will be iterating through are the binary representations of 0..31.

00000

00001

00010

00011

...

11111

This has the additional constraint that the value must be .1... which can be easily filtered with a bitmask:

 for i = 0 .. 2**elem.size - 1
     next unless i & 0b01000

After this, you walk the bits and sum up the values:

    sum = 0
    mask = 0b00001  # written out, its just 1
    for j = 0 .. elem.size
        sum += elem[j] * ((i & mask) >> j)
        mask <<= 1
    print sum

I'll admit to the possibility that there's a fencepost or off by one error in there, but this is fairly simple loops that may be even simpler (or harder) depending on the implementation language.  Just a little bit of bit math.

The key part of this is walking over the bits of i. We start out with a max with only the lowest bit set (0b00001 which is also just a plain old 1). Then, for each element of the array, do a bitwise and of the mask and i.  If the bit is set, this is a power of 2 (20, 21, 22 ...) that we then have to shift back the number of positions to make it either 1 (if the bit was set) or 0 (if the bit was not set).  This is done with the >> operator. This could also be done by dividing by the appropriate power of 2.

Once we get 1 or 0 back from the bit math, multiply it by the element at the associated array index and then add that to the sum.

To move to the next position, the mask is shifted to the left one bit (so that 0b00001 becomes 0b00010) which can also be done with a *= 2 if you didn't want to work with bits.


问:
Solutions To Partial ORM Retrieval
I have read the following question regarding whether it is best to use objects with fully or partially populated data members. The 3 suggestions were:

that perhaps using a fully populated ORM model may not have as much overhead as you might expect (testing requried);

an incorrect model was being used if only a limited number of items were being populated, and;

use a 'lite' function or entire class/model to access the pieces of information that are needed.

I cannot tell whether the following solution is using the second or the third suggestion, or more likely, neither:

Example / Solution: Using a table for user information, with each row being a user and storing their user information. For most page views, only 3 out of 12 columns are needed to customize the page for the user. Does it make sense to copy these pieces of information over to another table which holds other relevant session information and is also accessed during every pageload? In this case, there is data duplication but for most tasks a single row is fetched with all relevant information. At the end of the session, this row is removed.

答:
Generally speaking, it is not good to duplicate values unless there is a valid reason. For the above solution, it is definitely not a good idea.

Though it might seem counter-intuitive, ORMs by default (Hibernate) prefer to load and save all the fields of an entity (recommended upto a limit ~50 columns). The main reason is that they can pre-build these SQLs during startup and populate the statement cache for better performance.

If you opt to do dynamic update or dynamically specify user specified columns, such as if you were implementing the server component of an OData implementation, then it might make sense to build in some optimization.

Also, if you are looking at a table that has a large number of columns. This mostly can happen if the "Table per class hierarchy" strategy is used, then you might want to look at crafting specific HQL queries to only select the needed columns.
JPA 2.1 has introduced a feature called Entity graph that addresses this specific issue of partially selecting an object graph.

Title: Solutions To Partial ORM Retrieval


问:
Does this data processing pipeline design need to be so complex?
I have a data processing pipeline with well defined stages and IO boundaries.  I can choose a language to suit the needs of this design.  It starts with an InputObject.  At the end of each stage, there is some additional data derived from some or all of the results of previous steps and the InputObject.  That is, StageN extends Stage and produces an immutable ResultsObjN, which extends Result.  After the pipeline is complete, the final output I'm interested in is some subset of all results from each step:

```<code>InputObject
    |
    |
    v
 -----------
|  Stage 1  | <-- Tunable Parameters
 -----------
    |
    |
InputObject
ResultsObj1
    |
    |
    v
 -----------
|  Stage 2  | <-- Tunable Parameters
 -----------
    |
    |
InputObject
ResultsObj1
ResultsObj2
    |
    |
    v
   ...
</code>```

Currently, I'm modeling each stage as a Stage object.  I'll explain what I mean by "tunable" parameters soon.

Each Stage has each tunable parameters as attributes exposed by getters and setters.

Each Stage is constructed with its input dependencies.

Each Stage has a method to compute its result based on the tunable parameters

Here it is in almost-UML:

```<code> --------------------------------------------------
| <<abstract>>                                     |
| Stage                                            |
|--------------------------------------------------|
|--------------------------------------------------|
| +computeResult() : Result                        |
 --------------------------------------------------
</code>```

For instance, Stage3 computes ResultObj3 using the InputObject and the results of Stage2.  There are 2 parameters that can be set to change the results.

```<code> --------------------------------------------------
| Stage3                                           |
|--------------------------------------------------|
| -param1 : Int                                    |
| -param2 : Float                                  |
|--------------------------------------------------|
| +Stage3(raw : InputObject, corners : ResultObj2) |
| +get/setParam1()                                 |
| +get/setParam2()                                 |
| +computeResult() : ResultObj3                    |
 --------------------------------------------------
</code>```

I would like to reuse this processing pipeline pattern, with different stages in different quantities with different input dependencies.  The tunable parameters may be tweaked by an automated optimizer or by a human using some UI.  In either case, they follow the same feedback process.  Here it is tuning stage 3:

```<code>ResultObj3 performStage(InputObject raw, ResultObj2 corners):

1. Stage processor = new Stage3(raw, corners)
2. ResultObj result = processor.computeResult()
3. while (notAcceptable(result)):
4.     tuneParams(processor, result)  // tune to "more acceptable" params
5.     result = processor.computeResult()
6. return result
</code>```

I feel like the tuning process should be performed for each step by a queue executor, but I'm not yet sure how to accommodate the growing set of differently typed results , or the varying construction requirements of each stage.

Sample Pipeline

The point of this pipeline is to read a set of coordinates from a CSV (into String raw) and pass the string through a pipeline that will parse the string, cluster the points, and plot the clusters.  The cluster data and plot images are extracted from the pipeline after it is run.

The string is parsed into a collection of Point objects at the "ExtractPoints" stage.  That collection is clustered/segmented into a set of Cluster objects at the "ClusterPoints" stage.  The Point and Cluster data is used to plot the points visually into a Plot image object at the "PlotClusters" stage.

```<code>QueuedPipeline pipeline = new QueuedPipeline()

String raw = readFile("points.csv")
pipeline.setInput(raw)

// addStage() takes the name of the stage, the stage runner, and the names of the stages it needs results from
pipeline.addStage("ExtractPoints", new StageTuner(PointExtractor, ), [])
pipeline.addStage("ClusterPoints", new StageTuner(PointClusterer), ["ExtractPoints"])
pipeline.addStage("PlotClusters", new StageTuner(ClusterPlotter), ["ExtractPoints", "ClusterPoints"])

// tune and execute each stage with the StageTuners
PipelineResultList results = pipeline.run()

// pipeline is done, collect the interesting results
Result pointClusters = results.getResult("ClusterPoints")
Result clusterPlot = results.getResult("PlotClusters")

saveClusterFile(pointClusters)
saveCllusterPlotImage(clusterPlot)
</code>```

The classes PointExtractor, PointClusterer, and ClusterPlotter all inherit from Stage.

Each StageTuner takes the class of the Stage to tune and implements the above feedback loop.

Variables pointClusters and clusterPlots are of Result type

Problem

But simply passing around classes for construction is a headache in some languages.  Also, I'm not really sure how to generically construct and set parameters of each Stage subclass because of the different number/type of parameters - perhaps each Stage subclass needs a corresponding StageRunner subclass.  Finally, casting Result objects to the type I need within the final two functions sounds dangerous.  ...but these are just symptoms of my real problem:  This is all starting to get complex and fuzzy for what I expected to be a straightforward problem.

Am I defining my objects and behaviours badly?  Or is this not as straightforward a problem as I thought?  Something else entirely?

答:
You might be able to model this using a workflow engine.
You can model each of your stage as a workflow step and also parameterize each step. Depending on the engine chosen, manual action can be configured against each step.

The following post might help you decide if it is relevant to your use case:
https://stackoverflow.com/questions/2353564/use-cases-of-the-workflow-engine

If your expertise is with Java and Spring then the following post will help:
http://www.javaworld.com/article/2071865/web-app-frameworks/use-spring-to-create-a-simple-workflow-engine.html


问:
Will using multiple recordsets in one database request be more efficient?
I am redeveloping an old ecommerce website which is currently written in C# ASP.NET 2.0.

Because this is a reasonably high volume website and has suffered performance issues I want to develop in the most efficient manner possible.

To minimise database trips (using SQL Server 2012), as database performance is the usual bottleneck in the current system, I am looking at creating stored procedures that return everything required for the current page in one query as multiple recordsets.

For example the home page query might return the promotional banner image filenames and text, the new release products, the top selling products, etc. The product page might return the product details, related products, etc.

I can't remember seeing this technique used often if at all. Is returning all data in a single database request more efficient than retrieving the same data in several database requests? Are there any flaws in doing this?

答:
Your description of the home page seems like a dashboard page. The best practice for powering a dashboard page is to allow the data to be populated with queries that are executed in parallel. This allows any bad performing queries to be tuned independently.

This also makes it easier to add a new data view if needed when the home page evolves.

If all the views are going to be powered by a single source, then the display of the results are constrained to when the single source starts to return data. This time will be at least the time it takes to execute the slowest query.


答:
This sort of problem is well-suited to dynamic typing.  That will give you the most straightforward solution, with the obvious trade offs.

If you wish to use static typing, you'll have better luck if you don't centralize your pipeline construction.  Your stages are the ones who know the most about the types of their dependencies and results, so you should give them the responsibility for managing that.  I would start by making your example read something like this:

```<code>extractedPoints = new PointExtractor(raw)
clusterPoints   = new PointClusterer(extractedPoints)
plot            = new ClusterPlotter(extractedPoints, clusterPoints)

saveClusterFile(clusterPoints.result())
saveClusterPlotImage(plot.result())
</code>```

Note this directly uses the type system, which makes for a very natural-looking and idiomatic syntax.  Inside result() you would call common code to handle the tuning and memoize the result.  Because each calculation calls result() on each dependency before performing its own calculation, your "queue" ends up getting naturally formed in the call stack, and memoization prevents multiple execution.

Title: Does this data processing pipeline design need to be so complex?

Body:
I have a data processing pipeline with well defined stages and IO boundaries.  I can choose a language to suit the needs of this design.  It starts with an InputObject.  At the end of each stage, there is some additional data derived from some or all of the results of previous steps and the InputObject.  That is, StageN extends Stage and produces an immutable ResultsObjN, which extends Result.  After the pipeline is complete, the final output I'm interested in is some subset of all results from each step:

```<code>InputObject
    |
    |
    v
 -----------
|  Stage 1  | <-- Tunable Parameters
 -----------
    |
    |
InputObject
ResultsObj1
    |
    |
    v
 -----------
|  Stage 2  | <-- Tunable Parameters
 -----------
    |
    |
InputObject
ResultsObj1
ResultsObj2
    |
    |
    v
   ...
</code>```

Currently, I'm modeling each stage as a Stage object.  I'll explain what I mean by "tunable" parameters soon.

Each Stage has each tunable parameters as attributes exposed by getters and setters.

Each Stage is constructed with its input dependencies.

Each Stage has a method to compute its result based on the tunable parameters

Here it is in almost-UML:

```<code> --------------------------------------------------
| <<abstract>>                                     |
| Stage                                            |
|--------------------------------------------------|
|--------------------------------------------------|
| +computeResult() : Result                        |
 --------------------------------------------------
</code>```

For instance, Stage3 computes ResultObj3 using the InputObject and the results of Stage2.  There are 2 parameters that can be set to change the results.

```<code> --------------------------------------------------
| Stage3                                           |
|--------------------------------------------------|
| -param1 : Int                                    |
| -param2 : Float                                  |
|--------------------------------------------------|
| +Stage3(raw : InputObject, corners : ResultObj2) |
| +get/setParam1()                                 |
| +get/setParam2()                                 |
| +computeResult() : ResultObj3                    |
 --------------------------------------------------
</code>```

I would like to reuse this processing pipeline pattern, with different stages in different quantities with different input dependencies.  The tunable parameters may be tweaked by an automated optimizer or by a human using some UI.  In either case, they follow the same feedback process.  Here it is tuning stage 3:

```<code>ResultObj3 performStage(InputObject raw, ResultObj2 corners):

1. Stage processor = new Stage3(raw, corners)
2. ResultObj result = processor.computeResult()
3. while (notAcceptable(result)):
4.     tuneParams(processor, result)  // tune to "more acceptable" params
5.     result = processor.computeResult()
6. return result
</code>```

I feel like the tuning process should be performed for each step by a queue executor, but I'm not yet sure how to accommodate the growing set of differently typed results , or the varying construction requirements of each stage.

Sample Pipeline

The point of this pipeline is to read a set of coordinates from a CSV (into String raw) and pass the string through a pipeline that will parse the string, cluster the points, and plot the clusters.  The cluster data and plot images are extracted from the pipeline after it is run.

The string is parsed into a collection of Point objects at the "ExtractPoints" stage.  That collection is clustered/segmented into a set of Cluster objects at the "ClusterPoints" stage.  The Point and Cluster data is used to plot the points visually into a Plot image object at the "PlotClusters" stage.

```<code>QueuedPipeline pipeline = new QueuedPipeline()

String raw = readFile("points.csv")
pipeline.setInput(raw)

// addStage() takes the name of the stage, the stage runner, and the names of the stages it needs results from
pipeline.addStage("ExtractPoints", new StageTuner(PointExtractor, ), [])
pipeline.addStage("ClusterPoints", new StageTuner(PointClusterer), ["ExtractPoints"])
pipeline.addStage("PlotClusters", new StageTuner(ClusterPlotter), ["ExtractPoints", "ClusterPoints"])

// tune and execute each stage with the StageTuners
PipelineResultList results = pipeline.run()

// pipeline is done, collect the interesting results
Result pointClusters = results.getResult("ClusterPoints")
Result clusterPlot = results.getResult("PlotClusters")

saveClusterFile(pointClusters)
saveCllusterPlotImage(clusterPlot)
</code>```

The classes PointExtractor, PointClusterer, and ClusterPlotter all inherit from Stage.

Each StageTuner takes the class of the Stage to tune and implements the above feedback loop.

Variables pointClusters and clusterPlots are of Result type

Problem

But simply passing around classes for construction is a headache in some languages.  Also, I'm not really sure how to generically construct and set parameters of each Stage subclass because of the different number/type of parameters - perhaps each Stage subclass needs a corresponding StageRunner subclass.  Finally, casting Result objects to the type I need within the final two functions sounds dangerous.  ...but these are just symptoms of my real problem:  This is all starting to get complex and fuzzy for what I expected to be a straightforward problem.

Am I defining my objects and behaviours badly?  Or is this not as straightforward a problem as I thought?  Something else entirely?

Body:
This sort of problem is well-suited to dynamic typing.  That will give you the most straightforward solution, with the obvious trade offs.

If you wish to use static typing, you'll have better luck if you don't centralize your pipeline construction.  Your stages are the ones who know the most about the types of their dependencies and results, so you should give them the responsibility for managing that.  I would start by making your example read something like this:

extractedPoints = new PointExtractor(raw)
clusterPoints   = new PointClusterer(extractedPoints)
plot            = new ClusterPlotter(extractedPoints, clusterPoints)

saveClusterFile(clusterPoints.result())
saveClusterPlotImage(plot.result())

Note this directly uses the type system, which makes for a very natural-looking and idiomatic syntax.  Inside result() you would call common code to handle the tuning and memoize the result.  Because each calculation calls result() on each dependency before performing its own calculation, your "queue" ends up getting naturally formed in the call stack, and memoization prevents multiple execution.


问:
Short circuit evaluation, is it bad practice?
Something that I've known for a while but never considered is that in most languages it is possible to give priority to operators in an if statement based on their order. I often use this as a way to prevent null reference exceptions, e.g.:

if (smartphone != null && smartphone.GetSignal() > 50)
{
   // Do stuff
}
```

In this case the code will result in first checking that the object is not null and then using this object knowing that it exists. The language is clever because it knows that if the the first statement if false then there is no point even evaluating the second statement and so a null reference exception is never thrown. This works the same for and and or operators.

This can be useful in other situations too such as checking that an index is in the bounds of an array and such a technique can be performed in various languages, to my knowledge: Java, C#, C++, Python and Matlab.

My question is: Is this sort of code a representation of bad practice? Does this bad practice arise from some hidden technical issue (i.e. this could eventually result in an error) or does it lead to a readability issue for other programmers? Could it be confusing?

答:
One consideration, not mentioned in other answers: sometimes having these checks can hint at a possible refactoring to the Null Object design pattern. For example:

if (currentUser && currentUser.isAdministrator())
  doSomething();
```

<p>Could be simplified to just be:</p>

<pre class="lang-cs prettyprint-override"><code>if (currentUser.isAdministrator())
  doSomething ();
</code>```

If currentUser is defaulted to some 'anonymous user' or 'null user' with a fallback implementation if the user isn't logged in.

Not always a code smell, but something to consider.

ContentLicense: CC BY-SA 4.0

Body:
One consideration, not mentioned in other answers: sometimes having these checks can hint at a possible refactoring to the Null Object design pattern. For example:

if (currentUser && currentUser.isAdministrator())
  doSomething();

Could be simplified to just be:

if (currentUser.isAdministrator())
  doSomething ();

If currentUser is defaulted to some 'anonymous user' or 'null user' with a fallback implementation if the user isn't logged in.

Not always a code smell, but something to consider.


答:
This is my full answer. At the end didn't understand the (i & mask >> j)part so I use an IF. To see if the elem is part of the sum.

```<code>int[] elem = new int[] { 1, 2, 3, 4, 5 };
double maxElem = Math.Pow(2, elem.Length);

for (int first = 0; first < maxElem; first++)
{
    int mask = 1;
    int sum = 0;
    for (int run = 0; run < elem.Length; run++)
    {
        if ((first & mask) > 0)
        {
            sum += elem[run];
        }
        mask <<= 1;
    }
    Debug.Write(sum + " - ");
 }
</code>```

Result

0 - 1 - 2 - 3 - 3 - 4 - 5 - 6 - 4 - 5 - 6 - 7 - 7 - 8 - 9 - 10 - 5 - 6 - 7 - 8 - 8 - 9 - 10 - 11 - 9 - 10 - 11 - 12 - 12 - 13 - 14 - 15

Title: How to iterate between these elements

Body:
I have an array of elements:

```<code>int[] elem = new int[] {A, B, C};
</code>```

I need to calculate the sum of ALL the combinations of those elements, where only some of the elements can be optionally selected. I will use the notation:

{0,1} mean element can be selected or not

{1} mean element is always selected.

Therefore, I need to calculate the following value, which could be something like:

```<code>result = {0,1}*A + {1}*B + {0,1}*C;
</code>```

I have tried to use nested for loops, like so:

```<code>for (int i=0;i<2;i++)
    for (int j=1;j<2;j++) // notice I start at 1, not 0, because B is {1}
        for (int k=0;k<2;k++)
            Console.Write( i*A + j*B + k*C );
</code>```

There are two problems with this:

These nested for loops are rather cumbersome

My example only has three elements, but the array could be of any size. For example, if the array had 10 elements, I would need to have 10 nested loops instead of three. I don't know to change the number of for loops based on the array size.

How would I iterate to get all sum values for all combinations in an arbitrary sized array?

Body:
This is my full answer. At the end didn't understand the (i & mask >> j)part so I use an IF. To see if the elem is part of the sum.

int[] elem = new int[] { 1, 2, 3, 4, 5 };
double maxElem = Math.Pow(2, elem.Length);

for (int first = 0; first < maxElem; first++)
{
    int mask = 1;
    int sum = 0;
    for (int run = 0; run < elem.Length; run++)
    {
        if ((first & mask) > 0)
        {
            sum += elem[run];
        }
        mask <<= 1;
    }
    Debug.Write(sum + " - ");
 }

Result

0 - 1 - 2 - 3 - 3 - 4 - 5 - 6 - 4 - 5 - 6 - 7 - 7 - 8 - 9 - 10 - 5 - 6 - 7 - 8 - 8 - 9 - 10 - 11 - 9 - 10 - 11 - 12 - 12 - 13 - 14 - 15


问:
Add arguments to mysql in Django's "dbshell"
I'd like to add a couple of command-line arguments to my Django's ./manage.py dbshell command, and I can't figure out how.

In specific I'd like to add -A to prevent MySQL from scanning every table and every column, and --prompt=LOCAL: since I frequently keep multiple shells open.

I can't figure out how to do this! I'm only idea is to create my own "mysql" command in /usr/local/bin and have it be a wrapper for mysql with my own flags. But I'd really like to avoid doing that.

答:
No, there is no direct way to do this.

You need to modify the code django/db/backends/mysql/client.py. Don't worry, it is eay.


问:
Code Inside Catch Block
This question is related to both of these questions - Efficient try / catch block usage? and Dealing with error in data - Idempotent approach.

When I encounter a void while reading a GIS data file I throw an Exception. This Exception is a subclass of java.lang.Exception. The question is of what happens after the void data exception is reached. Post processing would require that I go to another URL where the void filled data file is available and then download that and then proceed to read it in.

From the "efficient-try-catch-block-usage" question it appears doing inside this the catch block is a strict NO. Would it better then instead of throwing an application specific exception it is better to check for a return code or a post condition and proceed to do the post processing ?

答:
It depends on how much of a performance hit you can reasonably accept when throwing the exception.  If it only happens once, it might not be a big deal.  If it happens every line of the file, it could be a huge problem.

Normally exceptions are thrown when something happens that you can't do anything about, and execution is stopped.

Title: Code Inside Catch Block


问:
Using prefix/postfix increment operator for simple incremental array access rather than actual index numbers
I cannot decide which method is better with consideration of both readability and fault-tolerance.

Method #1:

```<code>Object[] objects = getArrayOfKnownLength();
int i = -1;
someMethod(objects[++i], objects[++i], objects[++i], objects[++i]);
</code>```

Method #2:

```<code>Object[] objects = getArrayOfKnownLength();
someMethod(objects[0], objects[1], objects[2], objects[3]);
</code>```

I have always done the second method, but there have been several cases where I got IndexOutOfBoundsException because I made some changes which affected the expected array element position. So I have been considering using the first method to prevent this issue. My only concern is that it may be less readable.

I know this is a very minor thing to be questioning, but I am curious what the majority opinion is.

答:
The First method is horrible on my vision and it does not prevent "Exception".
The same feelings I have when I see "magic numbers"! XD

If possible, you need to change the architecture, something is wrong!
Or You need to check it before to use it in the method!
Or be right that those params always come with their values filled and in the same order!

Did You change the position of parameters? Refactoring or change the design...!


问:
Is it worth to rewrite the logic that already done client side on the server side or vice versa?
Few month ago, we created a logic to calculate customer progress in JavaScript. We didn't thought that it will be useful if we store them in our database.

Now, i'm facing 2 options,

Rewrite calculation_pogress in ruby (our server side programming language).

Create an API endpoint that catch the value from clients.

Is it worth for me to rewrite it in ruby ? Are there any other options ?

答:
Remember that anything coming from the client side has to be treated as untrusted.  If you actually make some decisions based on "progress" it would be better to calculate it on
the server side.</p>


问:
How do I show 'creates' in a UML class diagram?
Let's say my code is going to look like this:

```<code>public class MyController
{
    private Foo foo;
    private FooGenerator fooGen;

    //setters

    public void work()
    {
        this.foo = this.fooGen.generateFoo();
        System.out.println(this.foo.toString());
    }
}

public class FooGenerator()
{
    public Foo generateFoo()
    {
        String[] names = ["Andy", "Bob", "Charlie"];
        String[] colors =  ["Red", "Green", "Blue"];

        int nameIndex = new Random().nextInt(names.length);
        int colorIndex = new Random().nextInt(colors.length);

        return new Foo(names[nameIndex], colors[colorIndex], new Random().nextInt(100));
    }
}

public class Foo()
{
    private String color;
    private String name;
    private int age;

    public Foo(String color, String name, int age)
    {
        this.color = color;
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString()
    {
         return "This Foo's name is " + this.name + ", color is " + this.color + ", and is " + this.age + " years old.";
    }
}
</code>```

So what we have is MyController HAS a FooGenerator, and is passed back a Foo from the FooGenerator, which MyController then utilises.

The question is, how do I represent this in a UML diagram?

答:
[Disclaimer: I'm not a UML jockey, so don't take my word for it.]

I think, it would look like this.

Some details omitted for brevity.


答:
Your first method has 4 side effects (the pre-increments) while the second has none.

In general you should avoid side-effects if you can.

Furthermore, the second one is more clear about your intent. If I would see the first one in someone else's code I would ask myself if there was any reason for performing the pre-increments.

So, stick with the second option.

Title: Using prefix/postfix increment operator for simple incremental array access rather than actual index numbers

Body:
I cannot decide which method is better with consideration of both readability and fault-tolerance.

Method #1:

```<code>Object[] objects = getArrayOfKnownLength();
int i = -1;
someMethod(objects[++i], objects[++i], objects[++i], objects[++i]);
</code>```

Method #2:

```<code>Object[] objects = getArrayOfKnownLength();
someMethod(objects[0], objects[1], objects[2], objects[3]);
</code>```

I have always done the second method, but there have been several cases where I got IndexOutOfBoundsException because I made some changes which affected the expected array element position. So I have been considering using the first method to prevent this issue. My only concern is that it may be less readable.

I know this is a very minor thing to be questioning, but I am curious what the majority opinion is.


问:
Example of writing unit test for a method
I am writing unit tests for an iOS application. I clearly understand the benefits of writing unit tests & TDD, but I am confused about what kind of tests you can write for methods like this;

```<code>-(void)setCurrentView:(NSString *)view data:(NSString *)data
{
    if (!isEmpty(view)) {
        [Crashlytics setObjectValue:view forKey:kCurrentView];
        if (!isEmpty(data)) {
            [Crashlytics setObjectValue:data forKey:kCurrentViewData];
        }
    }
}
</code>```

It is written in objective C & Crashlytics is a third part API.

We can pass NULL & nil values & test that it doesn't throw any exception

Other than this we can test that the values are set properly (isEqual)

Any other test cases we can use here..

Update
The above method doesn't work as excepted. Calling class method "setObjectValue" is not sending anything to Crashlytics dashboard. I have to call the the instance method in Crashlytics to make it work..

```<code>[[Crashlytics sharedInstance] setObjectValue:view forKey:kCurrentView];
</code>```

答:
Despite the other answers to this question, there is in fact a lot of behaviour in this method, and therefore a lot of things that could go wrong if we don't test them. Here are the cases I would check:

When view is not empty and data is not empty, then both view and data are set in the Crashlytics object. This test is sufficient to get statement coverage.

When view is not empty but data is empty, only the view is set in the Crashlytics object.

When view is empty but data is not empty, nothing is set. The cases until here give us branch coverage.

When view is empty and data is empty, nothing is set. The cases until here would give us a kind of parameter coverage.

Setting the view uses the expected key.

Setting the data uses the expected key.

Note that there are different kinds of “empty”, which you should all test. There is a bit of a combinatorial explosion here, so you can either loop over an array of all empty values (preferred) or pick a representative empty value for the primary tests listed above, and then add a few additional tests to check that other empty values work as well, without aiming for full coverage.

Together, these cases form a kind of specification for the method. If you refactor the method or change a related part of the system, these tests will alert you when the specification was violated.

Of course, we don't want to make actual API calls for these tests – after all, we are only testing that our code is correct, not that the API works as expected. It would therefore be wise to use a mock object for these tests, and to use object members rather than global variables (i.e. use some kind of dependency injection).


问:
When writing a library or an API, when should and when shouldn't I validate or automatically correct errors in data provided by another developer?
I have a fairly good understanding of when data validation should and shouldn't exist when it comes when writing an application that is only dealt with by an end-user, and all the different tiers of the application and what code should check what (or, I would at least hope that I have a good idea).

However, as I've begun writing code that is not only going to interact with data provided by an end-user, but which will have developers (other than myself) specifying parameters and data to it, I've come across this conundrum numerous times. When should I validate data? When should I let it break? When is validation too little, or too comprehensive in scope? What about performance; how may it be hindered by additional layers of validation?

To provide a concrete example (out of many), one such case I had was when I was building a component that would manage a single-page web application. This component would accept a JSON object representing the web application's navigation map, in which were defined routes and the URLs at which page content resided for each route. It would use this tree of data to register the appropriate routes with a client-side Javascript router, and then handle loading and presenting pages when the right route was selected. When doing this, I encountered the following hypothetical scenario:

Developer specifies two pages in the navigation map, let's say page-1 and page-2 for example.

Developer defines page-1 to have the route #!/path.

Developer also defines (mistakenly?) page-2 to have #!/path as its route.

The problem here is that, for the two pages in the example above, there's no way for my component to determine which of the two pages to load, as both have the same route: https://localhost/my-app/#!/path leads to two entirely different pages! My component would only be aware of the last page with that registered route, as it would overwrite the definition of the first page for that route.

At first, having shortly before worked with a bunch of code related to user input validation, I added logic into the recursive function which parsed the navigation tree to maintain a list of existing routes/paths, and if a conflict was found, append an incrementing number to the end to avoid conflicts. So, for example, page-1's route would stay #!/path, but when parsing the next page, the code would note that it already encountered #!/path as a route and rename the route to #!/path-2.

I didn't think much of it at the time. But recently, as I was going back to port the code to a new format, I was looking over this part of the initialization function and I wasn't sure if it was the right thing to do.

So far, the farthest I've come to a solution to instances like these, is the thought that perhaps both methods of conquering this problem, validation and the lack thereof, have their own pros and cons.

Also, and I feel this is important to note, I'm not only talking about situations where I fix an error for a developer. I find myself in the same situation when I need to decide when to check for an error in developer-provided data that is going to another component that is encapsulated within my component. When should I let the mistake go unchecked by my code, and let the library/component I'm using throw an error on its own? When should I step in and validate the data first before relaying it over?

The pros I've thought of are:

The code continues to work even when an error is made, which may be desirable for an environment where downtime is a big problem.

A developer can use the error-correction behavior to their advantage, and rely on it as a sort of shorthand, in the sense of having to code less.

Similar to point 2, the developer doesn't have to watch their code using my API for errors/omissions/mistakes as strictly, which may fit their environment and the applications more flexibly, making it less likely for an issue to come up when using another product/component, or may just make it easier for them to manage the application if there are many similar lines of code using my API.

The cons I've thought of are:

This could lead to undesired behavior as an error could go unnoticed for a long time without throwing any errors. Depending on the circumstances, it may make finding the source of the bug tougher to find.

If developers begin relying on the error-correction mechanism I implement, that means that as more people depend on it working the way it does, the more likely it is for a new version of my element to break functionality for a lot of people. This may make improving my library/component/API tougher over time.

Additional validation logic at such a low level may potentially hinder performance. In some cases, this may not be noticeable if at small scale, but when used at larger scale, may become a significant problem.

Validation logic and error-correction logic further complicates my code. This may complicate maintenance as well, and with more moving parts, the more points of failure.

I'm not sure what's a good workflow to follow to get to a resolution in this case, and what rules/guidelines I should follow when working with such systems. I've certainly learned that developing a self-encapsulated program is much easier than developing an extensible system!

I've tried looking up information to help me figure out this problem, but so far the vast majority of what I've found relates only to working with end-user input, not developing APIs or libraries for other developers to use. With end-user-provided data, I need to be worried about my application breaking. But, in this case, I don't have that worry - instead it's the developer who wrote their own program using my library that will encounter an issue - with their program. How much should I be concerned with their application breaking due to their own mistake?

So, in conclusion:

What steps should I follow to determine, when developing APIs or extensible components, when I should or when I should not correct mistakes validate data provided by other developers?

How should I determine when my code should let 3rd-party data go unchecked to a deeper API/program component not written by me to let it throw an error, and when it should step in and check for errors first before relaying the data?

Footnote:

To be clear, I understand there isn't always (or, rather, is rarely) a one-size-fits-all solution, and I am not asking for the "best option." Really what I'm looking to understand is how do I evaluate which route to take in this case, and what variables may I be not considering? From the experience of others who have worked with similar situations, what did you learn along the way, and what should I do to figure this problem out? I'm coming at this without any practical experience working in an office or on a project team, only experience with the technology itself. The more I've developed, the more I've learned that there is far more to programming than just the code itself.

答:
Validation of input parameters/data should be performed whenever the data can come from an untrusted source. To be on the safe side, if anyone outside the development team can supply data to an interface, then that interface should validate its inputs.

The more interesting part of the question is what to do when the validation finds a problem.

First of all, don't presume you know what input was meant. This also means you shouldn't try to autocorrect the provided input. For normal text on an interactive interface, you could make suggestions for (spelling) corrections, but you should always give the user the final authority on what data they want to provide to the interface.

If the validation error is such that your software can't operate in a meaningful way with that input, then you should generate an error and either keep working with the old data you already have or refuse to work at all.

If the validation error is such that your software can operate, but with some sub-optimal or surprising results, then it would be best to accept the data, but also indicate a warning that there are problems with it. Where to draw the line depends on your requirements.

For example, with the configuration where two pages have the same route, you could flag that as a warning, and otherwise accept the configuration. This has the effect that, if the user chooses to go to 'page 1', they appear to end up on 'page 2', but that might just be the desired behavior.

When providing data to a third-party API, assume they will do their validation in the harshest possible way: the application terminates if the preconditions for calling a function are not met.


问:
What is the maximum number of steps to find a bug using bisecting?
Assume that a node A in the commit tree of a codebase contains a bug but some ancestor B of A is clean from that very bug. Given the topology of the commit tree [B,A] leading from B to A, can we predict c the maximum number of steps needed to locate the commit where the bug appeared, in terms of number of vertices, arrows, merges or cycles and other simple graph invariants?

There is two easy cases:

If the history from B to A is linear, then c is the binary logarithm of the number of nodes between B and A.

If the history is totally parallel, i.e. a family of independent commits A → Mᵢ → B, then c is the number of such commits.

From the two examples, we learn that linear history leads to cheaper bisection processes, but can we be more precise than this?  It is easy to write a program to compute c – which I still haven't done yet – but I am looking for a pretty algebraic formula. If the problem is hard, it could be interesting and easier to have expected value of c given, for instance, the number of vertices, arrows and merges, or something similar.

答:
Files in various code versioning systems typically gain bugs at one commit and remain bugged until they are fixed in another commit. This is because there are two popular methods for using versioning: file locks and merges. In the case of a file lock, developers lock files they're working on so nobody else can make changes. After unlocking, the other developers must then checkout the same file, ensuring that those changes won't be lost.

In a merging code versioning system, the main branch stores all of the commits from all branches merged into it, creating a linear history. This usually works on the concepts of diffs/patches, meaning that a commit that introduces a bug from one branch won't be automatically wiped out from a commit in another branch that is merged in later. In some rare cases, conflicts occur, and then a manual resolution is required. Bugs usually survive such merges, too, so they still appear continuously through the master branch's timeline.

No matter how many branches or developers you have working on a project, you can practically guarantee that simply by identifying the last known good commit before the bug, and last known bad commit for a bug (usually the current ref position), you can clearly isolate the commit that caused the bug, no matter which branch it occurred in. This is a side-effect of the linear nature of the logs and how they integrate to the main branch.

You can always identify the maximum search time for finding a bug by counting the number of commits from the last known good to the last known bad commit, and finding log2(x). You can often search an entire repository's history for a bug in well less than 20 iterations (16,000 commits, for example, is just 14 iterations). If you know the release a bug appeared in, that's usually in the order of about 5-10 iterations (your mileage may vary).

I only have experience with SVN and Git, but it's clear that any system that uses something better than "zip all the files in the repo into a hidden commit folder" (e.g. using literal file system snapshots) should have roughly the same logarithmic performance for finding bugs. There will always be just one linear log that needs to be read, ignoring all branches, even those that were later discarded or rebased, because the timeline will always appear linear.

For a crazy example, take a look at this image:

In this image, they show how Git works. And while Git is doing the job it was given, this is not a pretty sight. However, you'll notice how each commit nicely lines up linearly, despite merging from a ton of different branches at different times. Of course, there are tools to fix this history so its legible again (for example, this article about how they ended up in that mess).

The point is, the versioning system will keep things straight for you, so a simple binary search in a simple linear history is really all that's required.


问:
What is a reasonable level of detail for a style guide?
We are developing embedded software for an automotive product in ANSI-C in a large team. Yesterday, in our regular code review, we had a general discussion about the style of our code. We have a coding guide line (roughly 30 pages), which mainly ensures certain required practices and besides that treats hungarian notation, level of indentation, newline strategy for code blocks etc.

The discussion yesterday was about, whether there should be more detailed guide lines for how the code is formatted. Example declarations:

```<code>int int1,
    int2 = 0;
short short1;
long_type_name_defined var1;
const * type var2;
type const* var3;
</code>```

One colleague had the opinion, that there should be a clear guideline of how to consistently align the variables (e.g.: spaces up to longest regular type), whether or not there should be spaces between "*" and type / qualifier, where the qualifier is located, whether or not to use the "," operator etc. Things which could be somehow declared as a matter of personal taste.

Pro:

increased readability

better to understand for "externals" (who are new to the code or typically not dealing with it)

Con:

because it is somehow a matter of personal taste, what increases readability for one person, could decrease it for another (?)

might be hard to clearly define

style rule set would be enlarged a lot

a lot of work to always follow a large rule set

The main question is somehow: Does it really help to have a very specific rule set? We could not find a clear consensus about that. A style guide should increase the readability/maintainability of code, so there will be some optimum between size of the rule set and "beauty" of the code (?).

I would like to get your opinions about where this optimum is.

答:
Does it really help to have a very specific rule set?

No. Quite frankly, no. Or, to put it more constructively - take a look at any source code off github or sourceforge, can you read tit? If the answer is "yes" then you've just demonstrated that a single style is not important for readability.

What does help readability is readable code, which may sound daft, but is often very simple to describe without extensive rules - judicious use of whitespace, indenting and blank lines make code readable. Cramming everything together to use as few keystrokes as possible is the antitheses of readable.

What I have found is that extensive style guides simply don't get followed. 30 pages is nuts - you have to spend more time figuring out if you're following the guide than learning the code! And then so does everyone who reviews code, and even then people will just miss parts. (though it can be satisfying when the most ardent advocate of the extensive style guide gets his own code wrong)

The best guide I've ever seen simply said "make sure your code looks the same as the code you're working on". That's easy to follow, and its obvious where you get things wrong- they stand out as being "different". It doesn't matter what the original style used was, as long as its consistent.

Now, for 'externals': style guides don't help here. Who cares how many spaces you put in front of a variable declaration? Such trivialities do not matter. What does help though is layout - if declarations are always put at the top of a function or class, then your newbies will know where to look to find the variables. If the file naming convention has some rules, then they will easily be able to find the UI code as it'll always be in a file called "customerUI.c" (say). Little things like that matter far more than any amount of style.

You could say "structure is what the standards enforce, style just needs to be of a minimum standard"


答:
I think its fairly clear that a consistent programming style can help some things especially if you are publishing your code.

Prevents unusual style

Limited code difference between versions on check in to source control

Consistent style across large code base eases understanding

However, I would say its definitely an expensive thing to have. You have extra costs over a company which doesn't enforce a coding style.

New Employee, time spent reading/learning the style (all companies have different styles)

Senior dev, time spent writing the style

Senior dev, time spent maintaining the style wiki/sharepoint

Senior dev, time spent enforcing the style

Team, time spent arguing about the style

Team, time spent typing extra things required by the style (ie. format of comments etc)

DevOps, time spent getting the CI to enforce the style

Perhaps the only exception is where you use an automated tool like style cop to automatically style your code. In which case if you just accept the default rules you can get a fairly consistent style with a couple of clicks.

Title: What is a reasonable level of detail for a style guide?

Body:
We are developing embedded software for an automotive product in ANSI-C in a large team. Yesterday, in our regular code review, we had a general discussion about the style of our code. We have a coding guide line (roughly 30 pages), which mainly ensures certain required practices and besides that treats hungarian notation, level of indentation, newline strategy for code blocks etc.

The discussion yesterday was about, whether there should be more detailed guide lines for how the code is formatted. Example declarations:

```<code>int int1,
    int2 = 0;
short short1;
long_type_name_defined var1;
const * type var2;
type const* var3;
</code>```

One colleague had the opinion, that there should be a clear guideline of how to consistently align the variables (e.g.: spaces up to longest regular type), whether or not there should be spaces between "*" and type / qualifier, where the qualifier is located, whether or not to use the "," operator etc. Things which could be somehow declared as a matter of personal taste.

Pro:

increased readability

better to understand for "externals" (who are new to the code or typically not dealing with it)

Con:

because it is somehow a matter of personal taste, what increases readability for one person, could decrease it for another (?)

might be hard to clearly define

style rule set would be enlarged a lot

a lot of work to always follow a large rule set

The main question is somehow: Does it really help to have a very specific rule set? We could not find a clear consensus about that. A style guide should increase the readability/maintainability of code, so there will be some optimum between size of the rule set and "beauty" of the code (?).

I would like to get your opinions about where this optimum is.


答:
I think first you should take a step back and think; what you are trying to communicate with the diagram, and to whom?

If you absolutely have to do it via a class diagram then Nick's answer is a reasonable approach. However Class diagrams are usually used for showing static structure, creates seems like a behavioural/lifetime relation so is better suited to a behavioural diagram such as an activity diagram

Lastly is the diagram even necessary? This seems like such a low level implementation detail the only person I can think who would be interested in it would be another developer? As a developer I think I'd rather just read the code, and maybe a brief comment stating that FooGenerator is a factory for Foo than wade through UML.

Title: How do I show 'creates' in a UML class diagram?

Body:
Let's say my code is going to look like this:

```<code>public class MyController
{
    private Foo foo;
    private FooGenerator fooGen;

    //setters

    public void work()
    {
        this.foo = this.fooGen.generateFoo();
        System.out.println(this.foo.toString());
    }
}

public class FooGenerator()
{
    public Foo generateFoo()
    {
        String[] names = ["Andy", "Bob", "Charlie"];
        String[] colors =  ["Red", "Green", "Blue"];

        int nameIndex = new Random().nextInt(names.length);
        int colorIndex = new Random().nextInt(colors.length);

        return new Foo(names[nameIndex], colors[colorIndex], new Random().nextInt(100));
    }
}

public class Foo()
{
    private String color;
    private String name;
    private int age;

    public Foo(String color, String name, int age)
    {
        this.color = color;
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString()
    {
         return "This Foo's name is " + this.name + ", color is " + this.color + ", and is " + this.age + " years old.";
    }
}
</code>```

So what we have is MyController HAS a FooGenerator, and is passed back a Foo from the FooGenerator, which MyController then utilises.

The question is, how do I represent this in a UML diagram?


问:
Why store flags/enums in a database as strings instead of integers?
I've been browsing SQL dumps of some famous CMSes, including Drupal 7, Wordpress (some quite very old version), and some custom application based on Python.

All of these dumps contained data with string flags instead of integer ones. For example, a post's status was represented as published, closed, or inherit rather than 1, 2, or 3.

I have quite limited experience in design of databases and I have never went past simple SQLs, but I was always taught that I should use numeric/integer flags for data like this. It's obvious that tinyint consumes much less space in a database than, for example, varchar(9).

So what am I missing? Isn't this a waste of data storage and a data redundancy? Wouldn't browsing, searching and indexing be a bit faster if these columns used integers instead of strings?

答:
Yes, storing strings instead of numbers can use more space. The reason that high-profile pltforms are doing it anyway is that they think the benefits of that solution are greater than the cost.

What are the benefits? You can easily read a database dump and understand what it's about without memorizing the enum tables, and even semi-official GUIs might simply use the values themeselves rather than transform the record they get. (This is a basic form of disk space/processing time tradeoff.)

What about the cost? Data storage capacity hasn't been the bottleneck in CMS for a long time, since disks have gotten so large and so cheap. Programmer time, on the other hand, usually becomes more expensive - so anything that trades development effort for disk space is also a good thing, from a business perspective.


答:
The main purpose of best practices like DRY is not to make things easier for you, but to make things easier for future you(and for other people who'll work on the project). They care less about the process of coding and more about the final result. Of course, that doesn't mean the process is not important - but that's a concern for development methodologies and business planning(which you also need to balance)

In that regard, rewriting the logic for the server doesn't violate DRY - what could violate DRY is keeping both client and server side implementations and using them both. The rewriting itself creates work for you, but if once it's done you end up with a better application it can be worthwhile. Keeping both versions means you have to keep them in sync(changes to one must be replicated in the other), which makes the project harder to maintain. It's a problem for future you.

Now, like ddyer said in the other answer - if you send that data from the client to the server the server needs to validate it. So the main question is:

How complex is the to validate the progress compared to the calculating the progress?

If validating the progress is as complex as calculating it, you'll have to keep them in sync, which makes the project harder to maintain. It's different than having to syncing the two calculation implementations(Easier because bugs will pop earlier when validation code rejects progress that should be OK. Harder because there is no longer almost-one-to-one mapping between the logics), but it's still a problem for future you.

On the other hand, if validating the progress is much simpler than calculating it, you might consider calculating in the client side and sending to the server side. You should, of course, take into account things like:

Is the progress calculation requires lots of client/user interaction? If the server for example, will have to contact the client to ask the users questions, and needs these answers before the user can proceed, all this ping-pong can be prevented if the calculations are done client-side.

If the progress calculation is mainly for displaying it to the user, and you only need it at server side for logging purposes, it can be easier and produce better results for the client to do the calculation itself instead of having the query the server each time it wants to update the progress display.

How much work is it to reimplement the logic compared to implementing the API for transferring it between client and server?

Do you want to allow third party clients? If so, if the logic is at client-side they'll need to reimplement it...

And so on - the point is these should be considered only if the validation logic is simple(which means you can probably do many changes to the calculation logic without having to change the validation logic), since having to sync complex validation logic with complex calculation logic will make the project harder to maintain.

Title: Is it worth to rewrite the logic that already done client side on the server side or vice versa?

Body:
Few month ago, we created a logic to calculate customer progress in JavaScript. We didn't thought that it will be useful if we store them in our database.

Now, i'm facing 2 options,

Rewrite calculation_pogress in ruby (our server side programming language).

Create an API endpoint that catch the value from clients.

Is it worth for me to rewrite it in ruby ? Are there any other options ?


问:
Should I use BLOB or Tables for storing large data?
Problem

Currently investigating a solution to improve the performance of a web application. The application works well for small projects, but faces performance issues in the UI when working with large projects.

The use case is the following:

A user has to submit an excel document that contains 10000 line items. Each line item contains approximately 50 terms and each term can have one or more attributes.
The system should support a project that can handle 200 users uploading such documents.
A max of 10 users can concurrently be active. There can be multiple such large projects.

The database currently used is Oracle. We also need to ensure that the chosen solution will work well with an in-memory columnar RDBMS.

The existing functionality works well for small projects that has both a web UI and an excel interface. But the web UI has performance issues with large projects and we will solely rely on an excel interface.

The operations on the data involve upload/import, download/export, editing and reports.

All actions have to be transactional, since there are other updates within the RDBMS that occur as part of the upload. So this cannot be put in a non-transactional data source. There is at least one main operation where we need to load all the data. This operation can be done asynchronously.

Existing solution

Our existing solution which runs on tomcat and Oracle uses wide tables. This solution works well upto 1000 line items and then has performance issues on the application server.
The performance issues relate to java object hydration and causes memory issues on the application server. This is because the wide table has a large number of null columns and the java objects that are created are large due to a large number of empty fields.

Options

In order to handle a larger number of line items we need to reduce the memory footprint of the existing solution.
We are trying to decide between the following approaches:

BLOB

Narrow table

Redesigned Java object (New)

BLOB solution

One way to avoid the null values is to transform the excel document into a concise key value format that can be compressed and stored in the database as a BLOB per user.
The advantage of this approach is:

Use considerably less space in the DB.

The drawbacks are:

We are limited in what we can do, since there are some operations
that will need to process data across all users.

A small edit will cause the whole BLOB to be rewritten and thus causing redo log
growth.

Will be difficult to retrofit the existing UI against this model in the future

Maintain a new model for large projects

Narrow table

This approach solves the null values by having a few fields with a row for each term. The number of null columns is reduced drastically. The java objects hydrated from these rows do not have empty fields and can be small in size. So the memory issue is alleviated.
  The advantages are:

A narrow table is well suited for an in-memory columnar approach

Keeps open the possibility of reworking the UI to work against the new table structure

The drawbacks are:

Order of magnitude increase in the number of rows. A single project will end up having 10000x50x200 rows, i.e., 100 millions rows.

Maintain a new model since the UI will not be touched and that will go off the old model.

Redesigned java class

Had not initially considered this approach but it looks like a good option.
We use the existing data model, but revamp our java class backed by a map. Only the populated fields are held in this map. This avoids having a class with a large number of fields and hence reduces the memory footprint for a sparsely populated object.

The advantage

Solves the application memory issue with the least impact of all 3 options

Uses the existing data model

Drawbacks

Does not get rid of empty columns in the DB. But I think we can live with this for now.

May not be the best format for a columnar in-memory RDBMS

Question

What is the best approach to take?

Update
As I was clearing up the description, a potential third option (Redesigned java class) dawned on me. So I am going to investigate it further as it looks promising with no model impact. Let me know if this is not a good option based on the use case and if you see any issues with it.

答:
The challenge is how should this information be stored efficiently in an RDBMS?

The question should be why should this information be stored in a RDBMS at all?

What are you going to do with it once it's there?

If all you're going to do is "save" a spreadsheet into the database and then pull it back out again, then I'd suggest you're wasting your time.  It's a file; put it in a file system where it belongs and from where you can [far more] easily retrieve it.

However ...

If you want to interrogate the "uploaded" data and "slice and dice" it, drawing summaries across the data uploaded by many users, then the database is most definitely the way to go.

OK, 100M rows is a lot but with proper indexing (and partitioning, if you have the option), your database will cope with it.


问:
Should I use abstract or virtual methods?
If we assume that it is not desirable for the base class to be a pure interface class, and using the 2 examples from below, which is a better approach, using the abstract or virtual method class definition?

The advantage of "abstract" version is that it is probably looks cleaner and forces the derived class to give a hopefully meaningful implementation.

The advantage of the "virtual" version is that it can be easily pulled in by other modules and used for testing without adding a bunch of underlying framework like the abstract version requires.

Abstract Version:

```<code>public abstract class AbstractVersion
{
    public abstract ReturnType Method1();
    public abstract ReturnType Method2();
             .
             .
    public abstract ReturnType MethodN();

    //////////////////////////////////////////////
    // Other class implementation stuff is here
    //////////////////////////////////////////////
}
</code>```

Virtual Version:

```<code>public class VirtualVersion
{
    public virtual ReturnType Method1()
    {
        return ReturnType.NotImplemented;
    }

    public virtual ReturnType Method2()
    {
        return ReturnType.NotImplemented;
    }
             .
             .
    public virtual ReturnType MethodN()
    {
        return ReturnType.NotImplemented;
    }

    //////////////////////////////////////////////
    // Other class implementation stuff is here
    //////////////////////////////////////////////
}
</code>```

答: Dependency injection relies on interfaces. Heres a short example. Class Student has a function called CreateStudent that requires a parameter that implements interface "IReporting" (with a ReportAction method). After it creates a student it calls ReportAction on the concrete class parameter.  If the system is set up to send an email after creating a student we send in a concrete class that sends an email in its ReportAction implementation, or we could send in another concrete class that sends output to a printer in its ReportAction implementation. Great for code re-use.</p>


问:
Selecting members to add to a github project
My team recently added a software project to github. We are getting requests from some of the users that they would like to become part of the team. What would be a good way to choose whom to add ?</p>

答:
Don't add any outside contributor right away - make them send pull requests instead. As you review these pull requests, you can:

Assess the skill of the contributor from the quality of their code

Verify that what they want to add to the project is compatible with your vision

See how willing they are to conform to your rules and style

Learn to know them a bit by talking to them over the comments in the review-fix cycle

After accepting a few of their pull requests, you'll be in a much better position to decide if you want to give to a contributor push access to the project's repository.

Note that the contributors also learn during the course of the pull requests, so these parameters can and will improve over time. A contributor can learn from your code review how code submitted to the project should be and match their code to it. So it's not a matter of who but a matter of when (where the answer can be "never", but you don't know that yet) - any contributor might one day be a full member.


答:
As far as I can see, when you write:

I made some changes which affected the expected array element position

The advantage that you're focusing on is that in Method #1, the start position is only found in one place (the initial value of i) whereas in Method #2, that information is implicitly contained in all four array indexes.

Seen this way, the second method is a violation of the Don't Repeat Yourself (DRY) principle, as you're repeating knowledge. What you described is exactly the expected outcome when you violate DRY: when the repeated knowledge changes, you have a tricky, error-prone process of updating it in all the places it's contained.

However, I think we can do better than the first method too. Instead of the needlessly tricky increment operator, why not say directly what we mean:

```<code>Object[] objects = getArrayOfKnownLength();
int start = 0;
someMethod(objects[start], objects[start+1], objects[start+2], objects[start+3]);
</code>```

Now we have all the benefits of the first method, without the ugliness and difficulty to read.

Body:
As far as I can see, when you write:

I made some changes which affected the expected array element position

The advantage that you're focusing on is that in Method #1, the start position is only found in one place (the initial value of i) whereas in Method #2, that information is implicitly contained in all four array indexes.

Seen this way, the second method is a violation of the Don't Repeat Yourself (DRY) principle, as you're repeating knowledge. What you described is exactly the expected outcome when you violate DRY: when the repeated knowledge changes, you have a tricky, error-prone process of updating it in all the places it's contained.

However, I think we can do better than the first method too. Instead of the needlessly tricky increment operator, why not say directly what we mean:

Object[] objects = getArrayOfKnownLength();
int start = 0;
someMethod(objects[start], objects[start+1], objects[start+2], objects[start+3]);

Now we have all the benefits of the first method, without the ugliness and difficulty to read.


问:
What is the functional programming answer to type-based invariants?
I am aware that the concept of invariants exists across multiple programming paradigms. For example, loop invariants are relevant in OO, functional and procedural programming.

However, one very useful kind found in OOP is an invariant of the data of a particular type. This is what I'm calling "type-based invariants" in the title. For example, a Fraction type might have a numerator and denominator, with the invariant that their gcd is always 1 (i.e. the fraction is in a reduced form). I can only guarantee this by having some kind of encapsulation of the type, not letting its data be set freely. In return, I never have to check whether it's reduced, so I can simplify algorithms like equality checks.

On the other hand, if I simply declare a Fraction type without providing this guarantee through encapsulation, I can't safely write any functions on this type which assume that the fraction is reduced, because in the future somebody else could come along and add a way of getting hold of a non-reduced fraction.

Generally, the lack of this kind of invariant might lead to:

More complex algorithms as pre-conditions need to be checked/ensured in multiple places

DRY violations as these repeated pre-conditions represent the same underlying knowledge (that the invariant should be true)

Having to enforce pre-conditions through runtime failures rather than compile-time guarantees

So my question is what the functional programming answer to this kind of invariant is. Is there a functional-idiomatic way of achieving more or less the same thing? Or is there some aspect of functional programming which makes the benefits less relevant?

答:
You do it the same way:  make a constructor that enforces the constraint, and agree to use that constructor whenever you create a new value.

```<code>multiply lhs rhs = ReducedFraction (lhs.num * rhs.num) (lhs.denom * rhs.denom)
</code>```

But Karl, in OOP you don't have to agree to use the constructor.  Oh really?

```<code>class Fraction:
  ...
  Fraction multiply(Fraction lhs, Fraction rhs):
    Fraction result = lhs.clone()
    result.num *= rhs.num
    result.denom *= rhs.denom
    return result
</code>```

In fact, opportunities for this kind of abuse are fewer in FP.  You have to put the constructor last, because of immutability.  I wish people would stop thinking of encapsulation as some sort of protection against incompetent colleagues, or as obviating the need for communicating constraints.  It doesn't do that.  It just limits the places you have to check.  Good FP programmers also use encapsulation.  It just comes in the form of communicating a few preferred functions to make certain kinds of modifications.

Body:
You do it the same way:  make a constructor that enforces the constraint, and agree to use that constructor whenever you create a new value.

multiply lhs rhs = ReducedFraction (lhs.num * rhs.num) (lhs.denom * rhs.denom)

But Karl, in OOP you don't have to agree to use the constructor.  Oh really?

class Fraction:
  ...
  Fraction multiply(Fraction lhs, Fraction rhs):
    Fraction result = lhs.clone()
    result.num *= rhs.num
    result.denom *= rhs.denom
    return result

In fact, opportunities for this kind of abuse are fewer in FP.  You have to put the constructor last, because of immutability.  I wish people would stop thinking of encapsulation as some sort of protection against incompetent colleagues, or as obviating the need for communicating constraints.  It doesn't do that.  It just limits the places you have to check.  Good FP programmers also use encapsulation.  It just comes in the form of communicating a few preferred functions to make certain kinds of modifications.


答:
Encapsulation is not a feature that came with OOP. Any language that supports proper modularization has it.

Here's roughly how you do it in Haskell:

```<code>-- Rational.hs
module Rational (
    -- This is the export list. Functions not in this list aren't visible to importers.
    Rational, -- Exports the data type, but not its constructor.
    ratio,
    numerator,
    denominator
    ) where

data Rational = Rational Int Int

-- This is the function we provide for users to create rationals
ratio :: Int -> Int -> Rational
ratio num den = let (num', den') = reduce num den
                 in Rational num' den'

-- These are the member accessors
numerator :: Rational -> Int
numerator (Rational num _) = num

denominator :: Rational -> Int
denominator (Rational _ den) = den

reduce :: Int -> Int -> (Int, Int)
reduce a b = let g = gcd a b
             in (a `div` g, b `div` g)
</code>```

Now, to create a Rational, you use the ratio function, which enforces the invariant. Because data is immutable, you cannot later violate the invariant.

This does cost you something, though: it's no longer possible for the user to use the same deconstructing declaration as denominator and numerator use.

Title: What is the functional programming answer to type-based invariants?

Body:
I am aware that the concept of invariants exists across multiple programming paradigms. For example, loop invariants are relevant in OO, functional and procedural programming.

However, one very useful kind found in OOP is an invariant of the data of a particular type. This is what I'm calling "type-based invariants" in the title. For example, a Fraction type might have a numerator and denominator, with the invariant that their gcd is always 1 (i.e. the fraction is in a reduced form). I can only guarantee this by having some kind of encapsulation of the type, not letting its data be set freely. In return, I never have to check whether it's reduced, so I can simplify algorithms like equality checks.

On the other hand, if I simply declare a Fraction type without providing this guarantee through encapsulation, I can't safely write any functions on this type which assume that the fraction is reduced, because in the future somebody else could come along and add a way of getting hold of a non-reduced fraction.

Generally, the lack of this kind of invariant might lead to:

More complex algorithms as pre-conditions need to be checked/ensured in multiple places

DRY violations as these repeated pre-conditions represent the same underlying knowledge (that the invariant should be true)

Having to enforce pre-conditions through runtime failures rather than compile-time guarantees

So my question is what the functional programming answer to this kind of invariant is. Is there a functional-idiomatic way of achieving more or less the same thing? Or is there some aspect of functional programming which makes the benefits less relevant?

Body:
Encapsulation is not a feature that came with OOP. Any language that supports proper modularization has it.

Here's roughly how you do it in Haskell:

-- Rational.hs
module Rational (
    -- This is the export list. Functions not in this list aren't visible to importers.
    Rational, -- Exports the data type, but not its constructor.
    ratio,
    numerator,
    denominator
    ) where

data Rational = Rational Int Int

-- This is the function we provide for users to create rationals
ratio :: Int -> Int -> Rational
ratio num den = let (num', den') = reduce num den
                 in Rational num' den'

-- These are the member accessors
numerator :: Rational -> Int
numerator (Rational num _) = num

denominator :: Rational -> Int
denominator (Rational _ den) = den

reduce :: Int -> Int -> (Int, Int)
reduce a b = let g = gcd a b
             in (a `div` g, b `div` g)

Now, to create a Rational, you use the ratio function, which enforces the invariant. Because data is immutable, you cannot later violate the invariant.

This does cost you something, though: it's no longer possible for the user to use the same deconstructing declaration as denominator and numerator use.


问:
Blocking function call with asynchronous content
I am sure that this is a common design pattern, but I seem to have a blind-spot.

I have a requirement that the function call from Application to Service be blocking, but service needs to do something asynchronous.

Well, I can handle that, BUT, there can be multiple applications and there is only one service.

While the service should block the application, it should also be able to handle multiple parallel requests from different applications.

I suppose I need to spawn a new thread for each application’s request, but how then can I return from the thread to application at the end?

I don't mind an assembler insert to store the application’s return address & pop it later, but is that really necessary?

Do I need to do anything special in C++ to mark the service function as re-entrant?

Can someone please lift my blindfold?

[Update] There is no correlation between the applications.
Also, only the service would be allowed to spawn a thread, not the applications. So, I am thinking of a service  main spawning a bunch of service threads, but I don't see how I can can handle the function call blocking & return.

答:
Since the application is blocked while it is in the service call, every application already must have its own thread if there are to be multiple service calls at the same time.

Assuming the service call simply starts the async operation, maybe does something else, and then blocks until the operation finishes, I don't see the problem. Since every call to service is on its own thread, you can simply block that thread.