问:
How to design error monitoring for batch jobs with multiple and diverse steps?
I'm working on a software to integrate an e-commerce store and the company ERP. All the integration steps are run as a unique batch job, every 5 minutes. Things are working under production, but I'm struggling with how to better design the subsystem that alert us (developers and IT folks) that an integration error is occurring.

Essentially, the software integrates all the new customers, the new invoices, stock, products, and so on. To simplify things, the process was developed to be run as a serial sequence. Example: integrate first the customers and after the invoices, so we can't have invoices without customer data. When something fails (our e-commerce provider try to integrate some invalid data on a specific customer or it is simply down), the entire process fails and it signals to Nagios (a monitoring and alerting system) alert us.

I'm changing the design so a specific problem with a single unit don't stop the process, but the problem is the design of alerting that the integration of that specific unit failed, and make not necessary fiddling with logs to finding what is happening.

I've thought about some ways, but I would like to know if there is some better way to tackle this:

Send an e-mail to IT with a simplified description of the problem, with some data (example: invoice number), so they can open an issue ticket with the e-commerce provider. The problem is that the job is tried at every batch processing, so we should have a way to send only one e-mail per problem.

Send the problem to a centralized system (to be developed) via API, and show a web page listing the current problems. There should be a way to, again, deal with duplicated errors, and remove the resolved errors from the listing after they are resolved and the units integrated.

I am really stuck at this design, and would like to know if there is any other better way to deal with those batch jobs data inconsistencies, even if it demands developing some "infra structure" to make it easier for us.

答:
The problem is the design of alerting that the integration of that specific unit failed.

Take a step back from the technical solution and think about it from a business perspective first.

What types of errors are expected to happen? Go through each integration step and list the things that can go wrong. Group these into different severity levels. For example, these may be minor (e.g. the system reconnected after a short disconnect), moderate (e.g. a record was dumped because it was invalid) or major (e.g. the system is down).

You may need some intelligence here. For example, lower severity (e.g. data conversion) issues may indicate a more severe error (e.g. a whole input file being invalid).

Who needs to respond? Different errors may be fixed by different people at different times of the day. You will also need backups in case the designated person is unavailable (e.g. sick) or busy (e.g. dealing with another issue).

The answers to these questions determine the best medium. Less important errors can probably be stored for later review. Data conversion errors are probably best collated and addressed in bulk. Serious errors need to be communicated to users urgently but you may want to use a different message over night (e.g. SMS) than during the day (e.g. E-mail or Slack message).

You do not want lower severity issues flooding users. This just trains them to ignore errors. It makes it harder to find the root cause.

Also consider other requirements. Do you have service level agreements? The number of failures may contribute to this. Do management what reports the types of failures and time taken to fix them? Do people get paid extra to deal with issues out of hours?

I've thought about some ways, but I would like to know if there is some better way to tackle this.

The good news is many systems out there that do exactly this sort to thing already. For example, most syslog servers have a rules engine that can take the various messages from different sources (each integration step) then run a script if conditions are met (e.g. send an E-mail or SMS). This separates how systems report errors (e.g. via syslog), the rules that determine whether actions are taken (using the rules engine) and what those actions are (the scripts).


问:
Swift functions vs computed properties
Say I have have a class Event as follows:

```<code>class Event {
    private var attendees: [Person] = []

    // Case 1
    //*******
    // Should I use a func…
    func countOfAttendees() -> Int {
        return attendees.count
    }

    // …or a var
    var countOfAttendees: Int {
        return attendees.count
    }

    // Case 2
    //*******
    // Should I use a func…
    func countOfPaidAttendees() -> Int {
        return attendees.filter({$0.hasPaid}).count
    }

    // …or a var
    var countOfPaidAttendees: Int {
        return attendees.filter({$0.hasPaid}).count
    }
}
</code>```

Is it best practice to use functions or computed properties in the 2 cases indicated above?

答:
Follow the Uniform Access Principle,

All services offered by a module should be available through a
  uniform notation, which does not betray whether they are implemented
  through storage or through computation

To me, this means that I don't write funcs that take no arguments and return a value. I always use computed properties. That way, if I later decide to change the computed property into a stored property, I can do so without having the urge to remove the parens everywhere in my app and without having a separate "getter" method that just returns the value of a stored property, which seems pretty wasteful IMHO.

And if I change a stored property into a computed one, I don't have to add parens to the end of it, and everywhere that it is used in the app.

ContentLicense: CC BY-SA 3.0

Title: Swift functions vs computed properties


问:
Using a user defined equation to compute an entire list of numbers and store the list in an array
A example of what I want to do:

User puts numbers into a list on a GUI that looks like this

```<code> | L1 | L2 | l3 |
 ----------------
 | 3  |  6 |    |
 | 5  |  3 |    |
  ...
</code>```

He writes in a text area an equation like this; L1+L2[STO>]L3 and hits calculate and it updates the list to this

```<code> | L1 | L2 | L3 |
 ----------------
 | 3  |  6 |  9 |
 | 5  |  3 |  8 |
</code>```

In row one, 3 + 6 = 9 so the corresponding row of the numbers match the box, the corresponding row in list 3 is updated. We selected list one by typing L1, and we used a store opperator ([STO>]) to store the numbers into list 3. This is not new, I found this idea on ti-83, ti-84, and stat programs which use this.

This seemed simple at first; I could solve it knowing the equation beforehand with a simple algorithm. For example, with an equation such as L1+L2[STO>]L3 I could loop through the 2d array using the index value and store the numbers into an array to process the numbers from there. To illustrate I wrote a simple code snippet

```<code>for(int i = 0; i <= 100; i++) {
    data[i][0] + data[i][1] = data[i][2];
}
//Then update GUI

//So the data array was how I store user data from the gui,
//index 1 grabs the value from the row and index 2 is the corresponding list item.
</code>```

The problem:

I don't know how to proceed when I don't know the user defined equation. The user should be able to put in anything that you could put into a calculator. For example, valid inputs could also be L1*3455[STO>]L2, L1/L2[STO>]L3 ect.

答:
You have two options.

Option 1: Write your own parser. Start by reading up on "parse tree" and "recursive descent parser". So, the user enters the expression as text in whatever syntax you decide, you parse the text, you build an expression tree, some of the nodes in the tree will be line identifiers, some will be constants, some will be operators, you substitute identifiers with values, run the tree to evaluate the operators, you receive a result. It is not easy, but it is not rocket science, either.

Option 2: Embed a javascript engine like Rhino into your java application. Again, the user enters an expression as text in javascript syntax, the javascript engine parses it, you supply the engine with a value for each variable, you run the engine to evaluate the expression, you obtain a result. It is not easy either, but you are far more likely to find tutorials that will do most of the work for you.  You don't get to choose the syntax of the expressions, but if I compare javascript syntax with the odd thing you described above, I think it's a good thing that you don't get to choose the syntax.


问:
Check in which region is a geoposition Marker located in
I use Mapbox to display some moving markers over a map with several polygons drew over it (feature layers).

I have serveral markers (~1000) and multiple feature layers (polygons ~200) that represent different regions. The markers are moving (once per second with a constant velocity) and I have to know at any moment on which polygon does the marker position reside on.

First idea is the one to iterate for each marker every second over all polygons and check with a point in polygon formula (for geopositions?) if it is inside or not. This solution will not work as the complexity of O(NrMarkers * NrPolygons) seems too mcuh to run every second (might work but will put too mcuh stress on the server that moves the marker positions).

Now, I think maybe there is some way I could use more complex structures like k-d trees but it will be pretty hard to implement them and still there would be pretty much computation to be done each second.

I was also thinking of pre-processing because the polygons are known from the start and never change. Maybe I could somehow store for some known coordinates what polygons intersect those coordinates and use this info to exclude some polygons (or directly know the correct one) for any given position query.

So, to formulate it as an algorithmic question:

Given a list of P polygons (~200) (each having between 3 and 20 vertexes) and a list of N points (~1000) (which can either be still or have a constant speed that might change direction from time to time) find an algorithm that tells for each of the N points with which polygon shape does it intersect (inside which polygon is it, if any). This query will be asked every second for the same list of polygons and the same list of points (points which have their position updated after each second).

答:
Hopefully not too late... I'm having a similar problem, except that the number of my points are in billions, and the number of polygons could be in thousands.

While this idea did not help me, it might help you: if the area covered by the polygons is relatively small and/or the resolution is not too fine, you can create a grid of all points in the area, and for each point pre-compute the polygon it belongs in. Then, at run time, you'd find the 4 grid points that span a square/rectangle that contains your query point, and for each grid point you'll figure out its polygon, then you'll have 4 polygons to work with.

On the whole, I agree with @MichaelIT that spatial databases are pretty fast, and PostGIS (PostgreSQL extension) can easily cache in memory the shapes of polygons you work with, so, it won't be any slower than your custom code.


问:
How to become a "faster" programmer?
My last job evaluation included just one weak point: timeliness. I'm already aware of some things I can do to improve this but what I'm looking for are some more.

Does anyone have tips or advice on what they do to increase the speed of their output without sacrificing its quality?

How do you estimate timelines and stick to them? What do you do to get more done in shorter time periods?

Any feedback is greatly appreciated, thanks,

答:
"Timeliness" is not the same thing as "fast". If that was the problem your evaluation should have just said "slow". So be before you take the path you propose, make sure you know what is expected of you.

It could mean anything; it might even mean that you don't get into the office until 20 minutes after your colleagues, or that you have poor time management. That may be nothing to do with your 'programming speed'.

I probably spend most time designing and planning; it is easier to plan tasks from a good analysis and design, and you will then give better estimates that will be believed. Moreover from a good design, coding becomes a lot simpler and more directed process. It also makes it possible to divide up a task and distribute it amongst other developers.

OwnerDisplayName: Nick Gotch

ContentLicense: CC BY-SA 2.5

Title: How to become a "faster" programmer?

OwnerDisplayName: Clifford


问:
Enforce uniqueness of personal contact records in a system or not?
I have a contact table which stores all the contact information of a person such as name, address, phone number.

The system would be more user friendly if it can prevent users from accidentally entering duplicate records.

If it is unique it will be more user friendly, but it costs performance to check, plus the added code increases the maintenance cost in the future.

If it's not necessary from a business requirement standpoint, have you found person/contact records to be worth maintaining uniqueness in? Can you think of any problems I might run into enforcing uniqueness on them?

答:
For those who skip the comments, but read the answers, here is a copy/paste of a comment thread above, which seems to satisfy the OP. Please note, I am not asking to be awarded the answer, just to make this exchange more visible to future readers.

Can you make some fields unique - such as the phone number, if you are sure that no two customers will share the same 'phone number? Otherwise, SELECT before INSERT and, if a matching record already exists, ask the user who is inserting it what to do – Mawg

@Mawg Having two customers share a phone number is a very common scenario, although mobile phone numbers are of course, less likely to be shared... – Robbie Dee

Yup, I thought so - which is why I said "such as ..". Maybe social security number would be better .. – Mawg

@Mawg social security numbers are not guaranteed to be unique, at least not in every country. I don't think there's enough context in this question to provide a meaningful answer. What data is the OP storing? Why does it matter for users to be unique? The answers to these questions are use case specific. – toniedzwiedz

@Mawg that would be a great solution, a group of field would be unique such as Name+email+phone number so that I can prevent the employee from accidentally entering duplicated record. – Mai Hữu Lợi


答:
In Swift, functions without parameters and computed properties have almost the same capabilities (there may be a difference that a function without parameter is also a closure, while a computed property isn't).

The difference is semantically. If your code performs an action and returns for example a description of the outcome of that action, then I would use a function. If your code calculates a property but from the user's point of view this could have been a stored property, or maybe a stored property that requires updating some cached value first, then I would use a calculated property.

A big difference: What happens if you call the function or the calculated property twice? For a calculated property I expect that x = property; y = property has exactly the same behaviour as x = property; y = x except it might run a tiny bit slower. For functions, I wouldn't be surprised if the behaviour was different.

Title: Swift functions vs computed properties

Body:
Say I have have a class Event as follows:

```<code>class Event {
    private var attendees: [Person] = []

    // Case 1
    //*******
    // Should I use a func…
    func countOfAttendees() -> Int {
        return attendees.count
    }

    // …or a var
    var countOfAttendees: Int {
        return attendees.count
    }

    // Case 2
    //*******
    // Should I use a func…
    func countOfPaidAttendees() -> Int {
        return attendees.filter({$0.hasPaid}).count
    }

    // …or a var
    var countOfPaidAttendees: Int {
        return attendees.filter({$0.hasPaid}).count
    }
}
</code>```

Is it best practice to use functions or computed properties in the 2 cases indicated above?


问:
Separating Issues across multiple products (JIRA)
My R&D team has been working on a major product for over a year and we're using JIRA for issue management.

Now got more minor, distinct products (not related to the previous one), and would like to separate the issues, versions, etc.. related to these products.

We thought of the following options:

Different JIRA Projects, one for each product

Single JIRA Project for all products + custom attribute (named product)

Which option will be more clear, easier to understand and to manage?

答:
Best practice for me may not be best practice for you, but I can relate my experience, which is that building several commercial products in a single Jira system as separate projects has been really painless for me.

These products are separate and don't all go to all customers but are also quite integrated and related and share dependencies.

Jira's project prefix has become really convenient for writing and reading git commit comments and for quickly finding and filtering issues.

Separating projects makes it simple if release cycles and version numbers differ.

Generally it makes use of Jira's capabilities, in my experience. Yet we can still see issues from all products/sub-products in the dashboard and search results.

For my work it has been a no-brainer, separate projects works and using a custom attribute would be a pain for us.

If I had new sub-products every few weeks, I would consider changing my story, just due to the admin setup and maintenance and wondering whether the proliferation of product-projects will become as confusing as accounting codes, and the UI too cluttered. So if it means quickly going from 1 to 20 projects, I'm going to stop and think it over. 5 or 6, no problem.

If the products / sub-products are always released and versioned together then having them in the same project would not hurt as much, and at some point you really are talking about components or modules. I prefer to do those with tags, because I don't want to pay someone to keep the terms up to date in a formal mutually exclusive component list. Component lists tend to need periodic revamping as teams and products changes. Customers don't pay for that. Git commits are more definitive anyway after a change is done. Before it's done what's important to me is which person is it assigned to.

Disclaimer: I really am not interested in fancy metrics, in aggregate graphs across products/sub-products and I don't need to track or bill time. So I never looked to see how those things are affected.


答: Emacs</p>

Title: How to become a "faster" programmer?

Body:
My last job evaluation included just one weak point: timeliness. I'm already aware of some things I can do to improve this but what I'm looking for are some more.

Does anyone have tips or advice on what they do to increase the speed of their output without sacrificing its quality?

How do you estimate timelines and stick to them? What do you do to get more done in shorter time periods?

Any feedback is greatly appreciated, thanks,

Body: Emacs

OwnerDisplayName: ZeroCool


答:
Jira tends to be project centric, not product centric. So as you have realized, products do not have a very rich model (in Jira). The is a standard field "component" which might express something similar to what you are looking for. The approach of mapping a Jira "project" to each of your product also works, but makes it a bit harder to have a consolidated view of your products.

In the end, it depends a lot on how you work.

Title: Separating Issues across multiple products (JIRA)

Body:
My R&D team has been working on a major product for over a year and we're using JIRA for issue management.

Now got more minor, distinct products (not related to the previous one), and would like to separate the issues, versions, etc.. related to these products.

We thought of the following options:

Different JIRA Projects, one for each product

Single JIRA Project for all products + custom attribute (named product)

Which option will be more clear, easier to understand and to manage?


问:
Is this the correct approach to an OOP design structure in php?
I'm converting a procedural based site to an OOP design to allow more easily manageable code in the future and so far have created the following structure:

```<code>/classes
/templates
index.php
</code>```

With these classes:

```<code>ConnectDB
Games
System
User
User   -Moderator
User   -Administrator
</code>```

In the index.php file I have code that detects if any $_GET values are posted to determine on which page content to build (it's early so there's only one example and no default):

```<code>function __autoload($className) {
    require "classes/".strtolower($className).".class.php";
}

$db = new Connect;
$db->connect();

$user = new User();

if(isset($_GET['gameId']))
{
    System::buildGame($gameId);
}
</code>```

This then runs the BuildGame function in the system class which looks like the following and then uses gets in the Game Class to return values, such as $game->getTitle() in the template file template/play.php:

```<code>function buildGame($gameId){

    $game = new Game($gameId);
    $game->setRatio(900, 600);

    require 'templates/play.php';
}
</code>```

I also have .htaccess so that actual game page url works instead of passing the parameters to index.php

Are there any major errors of how I'm setting this up or do I have the general idea of OOP correct?

答:
So this is probably too old for me to help the OP in any way, and I'm not an expert.
However, I'd like to support @Rijk & their accepted answer with a little bit more structure & a few reference books (as outdated as books are starting to be these are, in my opinion, good ones).

So modern OOP, is mostly centered around the SOLID acronym.

Single responsibility, Open-closed principle, Liskov substitution, Interface segregation & Dependency inversion (almost an exact quote from the link).

They are defined better in the linked article than I could do them justice but here are the overall concepts that most of this fits under (but in , as much as I could pull off, less technical sounding terms):

Do one thing & do it well

Attempt to keep programming objects smaller & simpler rather than larger & complex (you can still build complex systems with less complex parts and it makes it much easier to understand each part of the complex system... and avoid and fix bugs)

Make your classes built so they follow the above AND so you can add functionality to them WITHOUT changing the code that makes up the class (when you change working code you are always risking the creation of bugs)

Design to concepts and ideas before you design your code

When a concept is implemented in code make an interface for it and anything that fits that concept should support the same interface & that interface to the concept should NOT change

Based on the above, make more interfaces & more concepts behind them if the interfaces you have created are not sufficient (rather than make the interface you have defined more complex)

Also avoid complex inheritance chains and instead utilize object composition

It's likely I missed a number of important points and/or failed to convey the proper ideas in what I said so, on to the reference books. These books were recommended to me by multiple programmers with more experience, expertise, & knowledge than I have. So with that recommendation:

Headfirst Design Patterns

Code Complete

The second is an excellent book on how to write good software & pull off software projects & plan & design & ... just lots of other valuable stuff.

The first is more accessible in general and is a really great resource for learning, well ... probably all of the principles covered in the SOLID acronym.

The overarching concept seems to be best described as:
writing relatively error free software through writing smaller pieces of code that are proven to work, that also don't need to be changed when you add more functionality.

Hope that's helpful to someone.


答:
I'd personally go this way (we are currently using this in my digital agency):

Have a distinct JIRA project for each project you are managing. This makes it easier to track issues and what's being done on each single worke you are doing.

Then have another JIRA project set up as a Kanban board to track the overall state of the projects.

Ours for example has the following columns:

Icebox

Estimate

Waiting for approval

Functional Requirements

Accounting

Design

Development

Rejected

Release

Done

Not necessarily every project goes through every of them in the given order. This allows even people not involved directly on each single project to get a quick picture of what's happening.

You should then have a custom field for projects in the Development column keeping track of the current sprint which is being developed right now.

I don't know if there are some better practises, but I can tell you from my experience that this is working pretty well for us!


答:
The trick is not to write code faster, but to write working code faster.

The sooner your spot a bug, the less effort it is to fix it - this is the primary thing which affects programmer performance.

It's not about how fast you type, or how fast your compiler is, it's about the speed at which you can identify bugs and fix them as you go.

Therefore, I'd suggest pair programming as a way to be faster - it really does avoid bugs. That and test-driven-development. Having two pairs of eyes makes it more than twice as hard for bugs to slip through (I think anyway).

Other tips would be

Try to reduce your code-test turnaround time to a minimum - this obviously depends on your platform an tools. If you're working on a silly embedded system with  lame tools, turnaround time could be quite signifcant (for example if you need to rebuild a system image and re-netboot the device), VMs or simulators can help here.

If not pair programming, ask others for informal code reviews occasionally, but only at logical breaks in your (and hopefully their) flow. Do this in addition to your formal code  review process you doubtless have.

Keep it simple - don't over engineer. You ain't going to need it.

Title: How to become a "faster" programmer?

OwnerDisplayName: MarkR


问:
Best practice for encapsulating a parameter that requires multiple interfaces to be implemented
I encounter this issue multiple times, but I'm never sure how best to deal with it.

Basically, some methods I write require the implementing object to support multiple interfaces. The example I have in mind is that a table structure needs to be able to IIterate over its records and IStreamable its content, where

```<code>IIterate = interface
  procedure First;
  procedure Next;
  property EOF: boolean;
end;

IStreamable = interface // outputs current record in table only
  function StreamOut: TArray<byte>;
  procedure StreamIn(const Input: TArray<byte>);
end;
</code>```

Now I don't think I want to combine these interfaces, as it's rare for methods to require IStreamable and not every IStreamable class can IIterate (my use case has single-record structures too).

The method I am trying to write is

```<code>CopyTable(InIterate: IIterate; InStreamable: IStreamable; OutIterate:IIterate; OutStreamable:IStreamable);
</code>```

Where the In and Out interfaces must be implemented by the same object. At the moment I'm just asking for one of the interfaces and using a Supports call to get the second, though this doesn't give you any compile-time protection. The obvious alternative is to use the above structure and check that both interfaces have the same implementing class, but that looks ugly.

Is there a better way to work with this kind of problem and get some compile-time safety?

The implementing language is Delphi (XE2), though I think this is a fairly language agnostic question for languages that don't have multiple inheritance.

答:
Java solved it by simply allowing multiple inheritance for interfaces. What is more interesting it also has some support for intersection types (but only for generics). So it is possible to simply write:

```<code><T extends InIterate & IStreamable > void something(T argument) {
    InIterate i = argument;
    IStreamable s = argument;
}
</code>```

It is type safe and no cast are required. In Java 8 it is also possible to use such syntax as type for lambda expressions but there still isn't any way to simply declare variable as InIterate & IStreamable

Body:
Java solved it by simply allowing multiple inheritance for interfaces. What is more interesting it also has some support for intersection types (but only for generics). So it is possible to simply write:

<T extends InIterate & IStreamable > void something(T argument) {
    InIterate i = argument;
    IStreamable s = argument;
}

It is type safe and no cast are required. In Java 8 it is also possible to use such syntax as type for lambda expressions but there still isn't any way to simply declare variable as InIterate & IStreamable


问:
Referencing database values in business logic
I guess this is another question about hard coding and best practices.  Say I have a list of values, lets say fruit, stored in the database (it needs to be in the database as the table is used for other purposes such as SSRS reports), with an ID:

```<code>1 Apple
2 Banana
3 Grapes
</code>```

I may present them to the user, he selects one, it gets stored in his profile as FavouriteFruit and the ID stored in his record in the database.

When it comes to business rules / domain logic, what are the recommendations for assigning logic to specific values.  Say if the user has selected Grapes I want to perform some extra task, what's the best way to reference the Grapes value:

```<code>// Hard coded name
if (user.FavouriteFruit.Name == "Grapes")

// Hard coded ID
if (user.FavoriteFruit.ID == 3) // Grapes

// Duplicate the list of fruits in an enum
if (user.FavouriteFruit.ID == (int)Fruits.Grapes)
</code>```

or something else?

Because of course the FavouriteFruit will be used throughout the application, the list may be added to, or edited.

Someone may decide that they want 'Grapes' renamed to 'Grape' and this would of course break the hardcoded string option.

The hardcoded ID isn't completely clear although, as shown you could just add a comment to quickly identify which item it is.

The enum option involves duplicating data from the database which seems wrong as it may get out of sync.

Anyway, thanks in advance for any comments or suggestions.

答:
A very common problem. While duplicating the data client side may seem to violate DRY principles, it is really due to the difference in paradigm between the layers.

Having the enum (or whatever) out of step with the database isn't actually that uncommon either. You may have pushed out another value to a metadata table to support a new reports feature which isn't yet used in the client side code.

It sometimes happens the other way too. A new enum value gets added to the client side but the DB update can't happen until such time as the DBA can apply the changes.


答:
I have a table contact which store all contact information of a person name, address, phone number. The contact records will be entered by some employees.

I want to know that should I check for duplication when a contact record is saved?

Define "unique".

Each and every "Contact" [record] should have a unique identifier which. IMO, cannot be any of the things you mention (they can all change over time).  This identifier should be application-generated, probably meaningless, and should not change from the moment the record is created to the moment that it is finally destroyed (systems like this are unlikely to physically delete records very often; keeping long-term records of people is a very common legal requirement these days).

So yes, your records should be uniquely identified, so that you only have one record per Person (without this constraint, your table will be of little practical use).

Given this, your question then becomes one of finding out whether or not you've seen this particular "Person" before - this is a De-Duplication issue and, again, is very common to systems like this.

One way to tackle it might be to provide a "Search" facility that people can use to locate a Person before creating a new record for them.

Alternatively, you'll need to run numerous database queries looking for data items that are "like" the ones entered; addresses are poorly standardised in the way that they are entered and so will be problematical to match.  Phone numbers are easier so long as you have a standardised format for storing them.

If you find your queries containing lots of "like" clauses, especially those with leading wildcards, then you're probably heading in the general direction of poor application performance.

And, even after all of this, remember that you will probably have to create the "sledgehammer" process that "merges" two Contact records - and every other record in every other table that relates to them - into one.

Title: Enforce uniqueness of personal contact records in a system or not?

Body:
I have a contact table which stores all the contact information of a person such as name, address, phone number.

The system would be more user friendly if it can prevent users from accidentally entering duplicate records.

If it is unique it will be more user friendly, but it costs performance to check, plus the added code increases the maintenance cost in the future.

If it's not necessary from a business requirement standpoint, have you found person/contact records to be worth maintaining uniqueness in? Can you think of any problems I might run into enforcing uniqueness on them?


问:
Spring Batch + Web Scraping
I need to develop a batch processing that will be executed every day. These are the steps:

Read each line of a database table that contains a URL (SQLite)

Extract some data, say Users, from that website by scraping it. Each website may contain 1..n users.

Persist each User in a local NoSQL database.

Send each User(one by one) through third-party REST API.

I'm going to implement this process using Spring Batch and I have thought this design:

Item Reader: Read each URL of SQLite database using JdbcCursorItemReader.

Item Processor: Scrap and deserialize users from website. (Url -> List<User>)

Item Writer: for each User, persist it in database and send it through REST API.

Is this approach right? Should I change any step? I have never worked with Spring Batch so I'm willing to change the technology if needed. I need some advice before start developing since I need this process to be very robust.

答:
This is generally a good application for Spring Batch, and you seem to understand the logical separation of Reader, Processor and Writer fairly well.

There are certain things that you should consider and think about when it comes to an application like this.  Spring Batch gives you the concept of chunking where rather than read/process/write each record one at a time you could read in several items as a chunk, process them as a single transaction, and write them as a single transaction.  Something that isn't clear to me based on your question is what your domain model will look like in your application to where this is possible.  It sounds as if there is a one to many relationship from URL to Users.  You would likely read in a single URL and build a collection of User objects that are ready to be processed and written out as a single transaction.

The second thing I would consider in your design and generally a good practice to get into when designing software is to document what your system constraints are.

Are there alternative means to retrieve required data about a User apart from screen scraping?  If not document the constraints that exist.

What software system or component requires the User data to be provided by your software (REST API).  Does this third party software have the ability to take a batch file for input as opposed to REST API?  Are there other potential interfaces that might be more reliable?

Also good to document Risks:

Screen scraping presents tight coupling between the web design and application and your batch job

In light of this information I would design like such:

Reader

Retrieve the URL from database

Screen scrape for user data

Create a List<User> objects for the Processor step

Processor

Integration of data from multiple Readers if applicable?

Special processing rules or calculation of derived data?

Preparation of User object for Writers

Writer

One unique writer for persisting to your database

Second unique writer for POST to REST API

Each chunk will be composed of users in a single URL.  Each chunk in process should be transacted so that in the event of an exception or failure, any persisted changes can be rolled back.  In the event of an exception, is it possible to define custom rollback behavior for the REST API?

Your final considerations should be the supportability and maintainability of the batch job.  You might want to consider Spring Batch Admin for this.  Any time where your business process depends on URL resources for internal or external network, screen scraping, and the availability and proper functioning of a REST API I would consider this sufficiently high risk.  There are many potential points of failure in this job so not only are Transactions and good exception handling a must, you will also want the ability to administer this easily and with minimal manual intervention.

Spring Batch Admin maintains a database of historical jobs as well as currently running jobs and paused and failed jobs.  You can configure a Spring Batch job managed with Spring Batch Admin to pick up where the failed job left off.  Perhaps your job got through 350 URL's of 400 to scan.  No need to clean up and start over if you can restart the failed job instance, it will pick up at record 351 and try again.  You may even be able to have it wait a few minutes and try several times before sending notifications out.

I hope this gives you things to consider.

Title: Spring Batch + Web Scraping


答:
You're right to ask this question, it's a nice question actually as you're attempting to defend against the evaluation of inaccurate conditions.

That said, the evaluation (your if conditions) don't necessarily need to be the focus of how you get around it. Instead, pay attention to the way you propagate the changes that would cause an 'out of sync' issue.

String Approach

If you must use strings, why not expose the functionality of changing the list through the UI? Design the system so that upon changing Grape to Grapes, for example, you update all of the records currently referencing Grape.

ID Approach

I'd always prefer to reference an ID, despite the compromise of some readability. The list may be added to can again be something you would be notified of if you exposed such a UI feature. If you're concerned with the reordering of items changing the id, propagate such a change to all dependent records again. Similarly to above. Another option (following proper normalisation convention, would be to have a your enum/id column - and reference a more detailed FruitDetail table, which has an 'Order' column you can lookup).

Either way, you can see I'm suggesting to control the amendment or updating of your list. Whether you do this through the use of an ORM or some other data access is determined by the specifics of your technology. What you're, essentially, doing is requiring that people that away from the DB for such changes - which I think is fine. Most main CRMs will make the same requirement.

Title: Referencing database values in business logic

Body:
I guess this is another question about hard coding and best practices.  Say I have a list of values, lets say fruit, stored in the database (it needs to be in the database as the table is used for other purposes such as SSRS reports), with an ID:

```<code>1 Apple
2 Banana
3 Grapes
</code>```

I may present them to the user, he selects one, it gets stored in his profile as FavouriteFruit and the ID stored in his record in the database.

When it comes to business rules / domain logic, what are the recommendations for assigning logic to specific values.  Say if the user has selected Grapes I want to perform some extra task, what's the best way to reference the Grapes value:

```<code>// Hard coded name
if (user.FavouriteFruit.Name == "Grapes")

// Hard coded ID
if (user.FavoriteFruit.ID == 3) // Grapes

// Duplicate the list of fruits in an enum
if (user.FavouriteFruit.ID == (int)Fruits.Grapes)
</code>```

or something else?

Because of course the FavouriteFruit will be used throughout the application, the list may be added to, or edited.

Someone may decide that they want 'Grapes' renamed to 'Grape' and this would of course break the hardcoded string option.

The hardcoded ID isn't completely clear although, as shown you could just add a comment to quickly identify which item it is.

The enum option involves duplicating data from the database which seems wrong as it may get out of sync.

Anyway, thanks in advance for any comments or suggestions.


答:
Assuming that we're talking about what is essentially a static lookup then the third option - the enum - is basically the only sane choice. Its what you'd do if the database wasn't involved so it makes sense.

The question then becomes the one about how to keep enums and static / lookup tables in the database in sync and unfortunately that's not a problem I yet have a complete answer to.

By choice I do all my schema maintenance in code and therefore I can keep a relationship between a build of the application and an expected schema version so it is straightforward to keep the lookup and the enum in sync but its something that one has to remember to do. It would be better if it were more automated (and also an automated integration test to ensure that the enums and the lookups match would help) but that's not something I've ever implemented.


问:
How to avoid the pitfalls of static analysis
I'm working at a company that would score 11 on Joel Test - at least on paper.

In practice, however, nothing works quite as well as expected, and the project has been on DEFCON 1 for half a year. Now, most of my peers are happy if they can go back home at 6pm - on Sunday.

One of the apparently good practices that struck me as not working is the use of static analysis tools. The project both tracks gcc -Wall warnings and a proprietary and very expensive "C/C++" tool.

Gcc warnings do more often than not point to real(if most of the time inoffensive) bugs.

The proprietary tools, however, list things such as implicit casts and sizeof'ing a string literal. Implicit casts are also blacklisted on their stylebook.

The standard practice is that people is pressed to make every single warning shut up. Note that this does exclude warnings that are predominantly false positives, this is not the problem.

The result is:

People add type casts to every rvalue and to every argument hiding real problematic type mismatches in the process.

People introduce off by one bugs, or use a different problematic language feature.(strlen instead of sizeof, strncpy instead of strcpy, etc.)

The warnings are silenced.

The bug reports start rolling in.

The main point is the original code was working and written by people who were playing safe within their language abilities whereas the fixes were not.

Now, I don't really think this company can be saved. However, I would like to know if there is a better, preferably working, way to use the "pro" tools or if I should just avoid using them altogether in case I am the one making the decision in the future.

A solution which doesn't assume all programmers are geniuses that can't err. Because well, if they are, then there is no need to use the tools in the first place.

答:
I'm working at a company that would score 11 on Joel Test - at least on paper.

Validation by the Joel Test is not a measure of good practices; Invalidation by the Joel Test is a measure of bad/missing practices though. In other words, it can be seen as necessary, but not sufficient.

Now, most of my peers are happy if they can go back home at 6pm - on Sunday.

It looks like you have a management problem. Systematic overtime is not a solution to getting things done; if anything, it is a solution to making things look like they are working and accumulating technical debt.

Now, I don't really think this company can be saved. However, I would like to know if there is a better, preferably working, way to use the "pro" tools or if I should just avoid using them altogether in case I am the one making the decision in the future.

Yes, there is a way to use static code analysis in a sane manner.

They can, and usually do provide a lot of value. You just have to remember that, (like compiler warnings) a static analysis tool can (at most) provide hints of something going wrong - not bug reports, and no certainty whatsoever.

It sounds like your decisions makers confuse static analysis flags with application defects.

Implicit casts are also blacklisted on their stylebook.

This sounds like a management competence problem, not a static analysis problem.

The result is:

People add type casts to every rvalue and to every argument hiding real problematic type mismatches in the process.

People introduce off by one bugs, or use a different problematic language feature.(strlen instead of sizeof, strncpy instead of strcpy, etc.)

The warnings are silenced.

The bug reports start rolling in.

All these are ways to make the periodic reports look good, not ways to fix the problems in the code (it looks like you have a management competence problem, again).


问:
Interception vs Injection: a framework architecture decision
There's this framework that I'm helping to design. There are some common tasks that should be done using some common components : Logging, Caching and raising events in particular.

I am not sure if it's better to use dependency injection and introduce all of these components to each service (as properties for example) or should I have some kind of meta data placed over each method of my services and use interception to do these common tasks?

Here's an example of both:

Injection:

```<code>public class MyService
{
    public ILoggingService Logger { get; set; }

    public IEventBroker EventBroker { get; set; }

    public ICacheService Cache { get; set; }

    public void DoSomething()
    {
        Logger.Log(myMessage);
        EventBroker.Publish<EventType>();
        Cache.Add(myObject);
    }
}
</code>```

and here's the other version:

Interception:

```<code>public class MyService
{
    [Log("My message")]
    [PublishEvent(typeof(EventType))]
    public void DoSomething()
    {

    }
}
</code>```

Here are my questions:

Which solution is best for a complicated framework?

If interception wins, what are my options to interact with internal values of a method (to use with cache service for example?)? Can I use other ways rather than attributes to implement this behavior?

Or maybe there can be other solutions to solve the problem?

答:
I've faced this problem lots of times and I think that I have come up with a simple solution.

Initially I went with the decorator pattern and manually implemented each method, when you have hundreds of methods this becomes very tedious.

I then decided to use PostSharp but I didn't like the idea of including an entire library just to do something that I could accomplish with (a lot of) simple code.

I then went down the transparent proxy route which was fun but involved dynamically emitting IL at run time and wouldn't be something that I'd want to do in a production environment.

I recently decided to use T4 templates to automatically implement the decorator pattern at design time, it turns out that T4 templates are actually quite hard to work with and I needed this done quickly so I created the code bellow. It's quick and dirty (and it doesn't support properties) but hopefully someone will find it useful.

Here's the code:

```<code>        var linesToUse = code.Split(Environment.NewLine.ToCharArray()).Where(l => !string.IsNullOrWhiteSpace(l));
        string classLine = linesToUse.First();

        // Remove the first line this is just the class declaration, also remove its closing brace
        linesToUse = linesToUse.Skip(1).Take(linesToUse.Count() - 2);
        code = string.Join(Environment.NewLine, linesToUse).Trim()
            .TrimStart("{".ToCharArray()); // Depending on the formatting this may be left over from removing the class

        code = Regex.Replace(
            code,
            @"public\s+?(?'Type'[\w<>]+?)\s(?'Name'\w+?)\s*\((?'Args'[^\)]*?)\)\s*?\{\s*?(throw new NotImplementedException\(\);)",
            new MatchEvaluator(
                match =>
                    {
                        string start = string.Format(
                            "public {0} {1}({2})\r\n{{",
                            match.Groups["Type"].Value,
                            match.Groups["Name"].Value,
                            match.Groups["Args"].Value);

                        var args =
                            match.Groups["Args"].Value.Split(",".ToCharArray())
                                .Select(s => s.Trim().Split(" ".ToCharArray()))
                                .ToDictionary(s => s.Last(), s => s.First());

                        string call = "_decorated." + match.Groups["Name"].Value + "(" + string.Join(",", args.Keys) + ");";
                        if (match.Groups["Type"].Value != "void")
                        {
                            call = "return " + call;
                        }

                        string argsStr = args.Keys.Any(s => s.Length > 0) ? ("," + string.Join(",", args.Keys)) : string.Empty;
                        string loggedCall = string.Format(
                            "using (BuildLogger(\"{0}\"{1})){{\r\n{2}\r\n}}",
                            match.Groups["Name"].Value,
                            argsStr,
                            call);
                        return start + "\r\n" + loggedCall;
                    }));
        code = classLine.Trim().TrimEnd("{".ToCharArray()) + "\n{\n" + code + "\n}\n";
</code>```

Here is an example:

```<code>public interface ITestAdapter : IDisposable
{
    string TestMethod1();

    IEnumerable<string> TestMethod2(int a);

    void TestMethod3(List<string[]>  a, Object b);
}
</code>```

Then create a class called LoggingTestAdapter which implements ITestAdapter, get visual studio to auto implement all of the methods and then run it through the code above. You should then have something like this:

```<code>public class LoggingTestAdapter : ITestAdapter
{

    public void Dispose()
    {
        using (BuildLogger("Dispose"))
        {
            _decorated.Dispose();
        }
    }
    public string TestMethod1()
    {
        using (BuildLogger("TestMethod1"))
        {
            return _decorated.TestMethod1();
        }
    }
    public IEnumerable<string> TestMethod2(int a)
    {
        using (BuildLogger("TestMethod2", a))
        {
            return _decorated.TestMethod2(a);
        }
    }
    public void TestMethod3(List<string[]> a, object b)
    {
        using (BuildLogger("TestMethod3", a, b))
        {
            _decorated.TestMethod3(a, b);
        }
    }
}
</code>```

This is it with the supporting code:

```<code>public class DebugLogger : ILogger
{
    private Stopwatch _stopwatch;
    public DebugLogger()
    {
        _stopwatch = new Stopwatch();
        _stopwatch.Start();
    }
    public void Dispose()
    {
        _stopwatch.Stop();
        string argsStr = string.Empty;
        if (Args.FirstOrDefault() != null)
        {
            argsStr = string.Join(",",Args.Select(a => (a ?? (object)"null").ToString()));
        }

        System.Diagnostics.Debug.WriteLine(string.Format("{0}({1}) @ {2}ms", Name, argsStr, _stopwatch.ElapsedMilliseconds));
    }

    public string Name { get; set; }

    public object[] Args { get; set; }
}

public interface ILogger : IDisposable
{
    string Name { get; set; }
    object[] Args { get; set; }
}

public class LoggingTestAdapter<TLogger> : ITestAdapter where TLogger : ILogger,new()
{
    private readonly ITestAdapter _decorated;

    public LoggingTestAdapter(ITestAdapter toDecorate)
    {
        _decorated = toDecorate;
    }

    private ILogger BuildLogger(string name, params object[] args)
    {
        return new TLogger { Name = name, Args = args };
    }

    public void Dispose()
    {
        _decorated.Dispose();
    }

    public string TestMethod1()
    {
        using (BuildLogger("TestMethod1"))
        {
            return _decorated.TestMethod1();
        }
    }
    public IEnumerable<string> TestMethod2(int a)
    {
        using (BuildLogger("TestMethod2", a))
        {
            return _decorated.TestMethod2(a);
        }
    }
    public void TestMethod3(List<string[]> a, object b)
    {
        using (BuildLogger("TestMethod3", a, b))
        {
            _decorated.TestMethod3(a, b);
        }
    }
}
</code>```

Body:
I've faced this problem lots of times and I think that I have come up with a simple solution.

Initially I went with the decorator pattern and manually implemented each method, when you have hundreds of methods this becomes very tedious.

I then decided to use PostSharp but I didn't like the idea of including an entire library just to do something that I could accomplish with (a lot of) simple code.

I then went down the transparent proxy route which was fun but involved dynamically emitting IL at run time and wouldn't be something that I'd want to do in a production environment.

I recently decided to use T4 templates to automatically implement the decorator pattern at design time, it turns out that T4 templates are actually quite hard to work with and I needed this done quickly so I created the code bellow. It's quick and dirty (and it doesn't support properties) but hopefully someone will find it useful.

Here's the code:

        var linesToUse = code.Split(Environment.NewLine.ToCharArray()).Where(l => !string.IsNullOrWhiteSpace(l));
        string classLine = linesToUse.First();

        // Remove the first line this is just the class declaration, also remove its closing brace
        linesToUse = linesToUse.Skip(1).Take(linesToUse.Count() - 2);
        code = string.Join(Environment.NewLine, linesToUse).Trim()
            .TrimStart("{".ToCharArray()); // Depending on the formatting this may be left over from removing the class

        code = Regex.Replace(
            code,
            @"public\s+?(?'Type'[\w<>]+?)\s(?'Name'\w+?)\s*\((?'Args'[^\)]*?)\)\s*?\{\s*?(throw new NotImplementedException\(\);)",
            new MatchEvaluator(
                match =>
                    {
                        string start = string.Format(
                            "public {0} {1}({2})\r\n{{",
                            match.Groups["Type"].Value,
                            match.Groups["Name"].Value,
                            match.Groups["Args"].Value);

                        var args =
                            match.Groups["Args"].Value.Split(",".ToCharArray())
                                .Select(s => s.Trim().Split(" ".ToCharArray()))
                                .ToDictionary(s => s.Last(), s => s.First());

                        string call = "_decorated." + match.Groups["Name"].Value + "(" + string.Join(",", args.Keys) + ");";
                        if (match.Groups["Type"].Value != "void")
                        {
                            call = "return " + call;
                        }

                        string argsStr = args.Keys.Any(s => s.Length > 0) ? ("," + string.Join(",", args.Keys)) : string.Empty;
                        string loggedCall = string.Format(
                            "using (BuildLogger(\"{0}\"{1})){{\r\n{2}\r\n}}",
                            match.Groups["Name"].Value,
                            argsStr,
                            call);
                        return start + "\r\n" + loggedCall;
                    }));
        code = classLine.Trim().TrimEnd("{".ToCharArray()) + "\n{\n" + code + "\n}\n";

Here is an example:

public interface ITestAdapter : IDisposable
{
    string TestMethod1();

    IEnumerable<string> TestMethod2(int a);

    void TestMethod3(List<string[]>  a, Object b);
}

Then create a class called LoggingTestAdapter which implements ITestAdapter, get visual studio to auto implement all of the methods and then run it through the code above. You should then have something like this:

public class LoggingTestAdapter : ITestAdapter
{

    public void Dispose()
    {
        using (BuildLogger("Dispose"))
        {
            _decorated.Dispose();
        }
    }
    public string TestMethod1()
    {
        using (BuildLogger("TestMethod1"))
        {
            return _decorated.TestMethod1();
        }
    }
    public IEnumerable<string> TestMethod2(int a)
    {
        using (BuildLogger("TestMethod2", a))
        {
            return _decorated.TestMethod2(a);
        }
    }
    public void TestMethod3(List<string[]> a, object b)
    {
        using (BuildLogger("TestMethod3", a, b))
        {
            _decorated.TestMethod3(a, b);
        }
    }
}

This is it with the supporting code:

public class DebugLogger : ILogger
{
    private Stopwatch _stopwatch;
    public DebugLogger()
    {
        _stopwatch = new Stopwatch();
        _stopwatch.Start();
    }
    public void Dispose()
    {
        _stopwatch.Stop();
        string argsStr = string.Empty;
        if (Args.FirstOrDefault() != null)
        {
            argsStr = string.Join(",",Args.Select(a => (a ?? (object)"null").ToString()));
        }

        System.Diagnostics.Debug.WriteLine(string.Format("{0}({1}) @ {2}ms", Name, argsStr, _stopwatch.ElapsedMilliseconds));
    }

    public string Name { get; set; }

    public object[] Args { get; set; }
}

public interface ILogger : IDisposable
{
    string Name { get; set; }
    object[] Args { get; set; }
}

public class LoggingTestAdapter<TLogger> : ITestAdapter where TLogger : ILogger,new()
{
    private readonly ITestAdapter _decorated;

    public LoggingTestAdapter(ITestAdapter toDecorate)
    {
        _decorated = toDecorate;
    }

    private ILogger BuildLogger(string name, params object[] args)
    {
        return new TLogger { Name = name, Args = args };
    }

    public void Dispose()
    {
        _decorated.Dispose();
    }

    public string TestMethod1()
    {
        using (BuildLogger("TestMethod1"))
        {
            return _decorated.TestMethod1();
        }
    }
    public IEnumerable<string> TestMethod2(int a)
    {
        using (BuildLogger("TestMethod2", a))
        {
            return _decorated.TestMethod2(a);
        }
    }
    public void TestMethod3(List<string[]> a, object b)
    {
        using (BuildLogger("TestMethod3", a, b))
        {
            _decorated.TestMethod3(a, b);
        }
    }
}


答: I've practically tripled my C coding speed with VIM.</p>

Title: How to become a "faster" programmer?

OwnerDisplayName: Liran Orevi


答:
Checking against the string is the most readable, but it does double duty: it's used both as an identifier and a description (that may change for unrelated reasons).

I usually split both duties into separate fields:

```<code>id  code    description
 1  grape   Grapes
 2  apple   Apple
</code>```

Where description may change (but not "Grapes" to "Banana"), but the code is not allowed to change, ever.

Although this is mostly because our ids are almost always auto-generated, and thus not a good fit. If you can choose ids freely, maybe you can guarantee that they are always correct and use those.

Also, how often does someone really edit "Grapes" to "Grape"? Maybe none of this is necessary.

Body:
Checking against the string is the most readable, but it does double duty: it's used both as an identifier and a description (that may change for unrelated reasons).

I usually split both duties into separate fields:

id  code    description
 1  grape   Grapes
 2  apple   Apple

Where description may change (but not "Grapes" to "Banana"), but the code is not allowed to change, ever.

Although this is mostly because our ids are almost always auto-generated, and thus not a good fit. If you can choose ids freely, maybe you can guarantee that they are always correct and use those.

Also, how often does someone really edit "Grapes" to "Grape"? Maybe none of this is necessary.


答:
What you are expecting here is the programming logic to be automatically adaptable to changing data. Simple static options like Enum don't work here because you cannot feasibly add extra enums in the runtime.

A few patterns I have seen:

Enums + default to protect against a brand new database entry ruining your program's day.

Encoding of actions to be done (biz logic) in the database itself. In many cases this is very much possible because many logics get reused. The implementation of the logic should be in the program.

Extra attributes/columns in the database to mark the brand new value as 'to-be-ignored' in the program until program is deployed right.

Fail fast mechanisms around the code path that loads/reloads the values from the database. (If the corresponding action is not in the program AND it is not marked to be ignored, don't take the refresh).

In general I like data being complete in terms of referring to actions that it implies - even though the actions themselves might be implemented elsewhere. Any code that is determining actions independent of the data has just sharded your data representation which most probably will diverge and lead to bugs.


答:
First of all, Iterate is a lame name for that interface, it should be called Iterator, so I will be calling it Iterator in this answer, because I am allergic to bad naming.

You say that you encounter this issue often, and I have to believe you, but I also ought to mention my experience, which is that I have rarely, if ever, encountered this need in the last couple of decades that I have been doing OOP.

Usually, when such a need appears to arise, it is an indication that some other aspect of the design needs refactoring.

The first suspect to examine is the CopyTable() function: why does it need both an Iterator and a Streamable for both the source and the destination table?  Why can't it just stream-out the source table to bytes and then stream-in the bytes into the destination table?  And why is it even called CopyTable when it is probably just CopyStreamable() or CopyStreamableIterator() at worse?

If there is nothing that can be done about the function, then the best approach in my opinion is to combine the two interfaces into one.  The name CopyStreamableIterator() which describes what the function does immediately tells you that you need a StreamableIterator interface which combines Streamable and Iterator.  There is nothing wrong with that, and you do not even have to make your Table class implement that new interface, you can simply implement a StreamableIteratorDecorator (see decorator pattern) which accepts a Streamable and an Iterator as constructor parameters and implements the StreamableIterator interface by delegating to them.  (Though you will probably just declare your Table class as implementing this interface to be done real quick, because your Table already implements these methods, so it is good to go.)

Note: I am presuming that in Delphi, just as in Java and C#, a class may extend only one class but multiple interfaces, and that an interface may also extend multiple interfaces, but even if by any chance this is not true, this functionality can be emulated by aggregating interface implementations into classes and adding to classes and interfaces methods that return sub-interfaces.

Amendment

Thinking about it a bit more, I am realizing that your problem may lie in that ill-named IIterate interface.  This interface is trying to cut corners, so it is built in such a way as to force manipulation of the internal state of the underlying (implementing) object.  This causes problems, because it is forcing you to have two separate interfaces, one for altering the internal state of your table to select a row, and another interface for reading/writing the currently selected row.

In a language like C# and Java your table would implement IIterable<IStreamable>, and IIterable<T> would have just one method, newIterator() : IIterator<T>.  In turn, IIterator<T> would be defined as follows:

```<code>IIterator<T> = interface //javaesque
  function Next : T;
  property EOF: boolean;
end;
</code>```

or as follows:

```<code>IIterator<T> = interface //csharpesque
  function Current : T;
  function HasNext : boolean;
  procedure MoveNext;
end;
</code>```

So, instead of selecting rows, your iteration would be yielding each row which can then be streamed.  So, you only need to pass an iterator (or better yet an iterable) to your CopyTable() function.

Adding rows to the target table can be easily accomplished by implementing a IConsumer<IStreamable> interface which simply appends the contents of the IStreamable as a new row at the end of the table.

Title: Best practice for encapsulating a parameter that requires multiple interfaces to be implemented

Body:
I encounter this issue multiple times, but I'm never sure how best to deal with it.

Basically, some methods I write require the implementing object to support multiple interfaces. The example I have in mind is that a table structure needs to be able to IIterate over its records and IStreamable its content, where

```<code>IIterate = interface
  procedure First;
  procedure Next;
  property EOF: boolean;
end;

IStreamable = interface // outputs current record in table only
  function StreamOut: TArray<byte>;
  procedure StreamIn(const Input: TArray<byte>);
end;
</code>```

Now I don't think I want to combine these interfaces, as it's rare for methods to require IStreamable and not every IStreamable class can IIterate (my use case has single-record structures too).

The method I am trying to write is

```<code>CopyTable(InIterate: IIterate; InStreamable: IStreamable; OutIterate:IIterate; OutStreamable:IStreamable);
</code>```

Where the In and Out interfaces must be implemented by the same object. At the moment I'm just asking for one of the interfaces and using a Supports call to get the second, though this doesn't give you any compile-time protection. The obvious alternative is to use the above structure and check that both interfaces have the same implementing class, but that looks ugly.

Is there a better way to work with this kind of problem and get some compile-time safety?

The implementing language is Delphi (XE2), though I think this is a fairly language agnostic question for languages that don't have multiple inheritance.

Body:
First of all, Iterate is a lame name for that interface, it should be called Iterator, so I will be calling it Iterator in this answer, because I am allergic to bad naming.

You say that you encounter this issue often, and I have to believe you, but I also ought to mention my experience, which is that I have rarely, if ever, encountered this need in the last couple of decades that I have been doing OOP.

Usually, when such a need appears to arise, it is an indication that some other aspect of the design needs refactoring.

The first suspect to examine is the CopyTable() function: why does it need both an Iterator and a Streamable for both the source and the destination table?  Why can't it just stream-out the source table to bytes and then stream-in the bytes into the destination table?  And why is it even called CopyTable when it is probably just CopyStreamable() or CopyStreamableIterator() at worse?

If there is nothing that can be done about the function, then the best approach in my opinion is to combine the two interfaces into one.  The name CopyStreamableIterator() which describes what the function does immediately tells you that you need a StreamableIterator interface which combines Streamable and Iterator.  There is nothing wrong with that, and you do not even have to make your Table class implement that new interface, you can simply implement a StreamableIteratorDecorator (see decorator pattern) which accepts a Streamable and an Iterator as constructor parameters and implements the StreamableIterator interface by delegating to them.  (Though you will probably just declare your Table class as implementing this interface to be done real quick, because your Table already implements these methods, so it is good to go.)

Note: I am presuming that in Delphi, just as in Java and C#, a class may extend only one class but multiple interfaces, and that an interface may also extend multiple interfaces, but even if by any chance this is not true, this functionality can be emulated by aggregating interface implementations into classes and adding to classes and interfaces methods that return sub-interfaces.

Amendment

Thinking about it a bit more, I am realizing that your problem may lie in that ill-named IIterate interface.  This interface is trying to cut corners, so it is built in such a way as to force manipulation of the internal state of the underlying (implementing) object.  This causes problems, because it is forcing you to have two separate interfaces, one for altering the internal state of your table to select a row, and another interface for reading/writing the currently selected row.

In a language like C# and Java your table would implement IIterable<IStreamable>, and IIterable<T> would have just one method, newIterator() : IIterator<T>.  In turn, IIterator<T> would be defined as follows:

IIterator<T> = interface //javaesque
  function Next : T;
  property EOF: boolean;
end;

or as follows:

IIterator<T> = interface //csharpesque
  function Current : T;
  function HasNext : boolean;
  procedure MoveNext;
end;

So, instead of selecting rows, your iteration would be yielding each row which can then be streamed.  So, you only need to pass an iterator (or better yet an iterable) to your CopyTable() function.

Adding rows to the target table can be easily accomplished by implementing a IConsumer<IStreamable> interface which simply appends the contents of the IStreamable as a new row at the end of the table.


问:
flatMap with if else vs combine with filter
When using reactive frameworks I have seen both solutions below to make a mutually exclusive selection of which stream will be forwarded (rxjava in this case)

```<code>Observable.merge(
  Observable.just(aBoolStatement).filter(aBoolean -> aBoolean).flatMap(aBoolean -> signal1),
  Observable.just(aBoolStatement).filter(aBoolean -> !aBoolean).flatMap(aBoolean -> signal2)
)
</code>```

-

```<code>Observable.just(aBoolStatement).flatMap(aBoolean -> {
  if(aBoolean)
    return signal1
  else
    return signal2
}
</code>```

The first code is all rx which is nice but it also evaluates the statement twice which is easy to miss if you would ever need to update the statement. Is there a preferred way of doing this or any other way to achieve the same result?

答:
This probably depends heavily on your input size, but the first example is definitely doing more unnecessary work. It has to filter the input twice, and then merge it all back together. The second example gets it all in one pass.

Also, the second can be written as a fairly elegant one-liner (assuming this is Scala, which it appears to be)

```<code>Observable.just(aBoolStatement).flatMap(b -> if(b) signal1 else signal2)
</code>```

This is due to the fact that if is an expression that returns a value in Scala, so including the return keyword on each branch is redundant. Using if like this is equivalent to using a ternary expression (b ? signal1 : signal2) in another language that supports that syntax.

Body:
This probably depends heavily on your input size, but the first example is definitely doing more unnecessary work. It has to filter the input twice, and then merge it all back together. The second example gets it all in one pass.

Also, the second can be written as a fairly elegant one-liner (assuming this is Scala, which it appears to be)

Observable.just(aBoolStatement).flatMap(b -> if(b) signal1 else signal2)

This is due to the fact that if is an expression that returns a value in Scala, so including the return keyword on each branch is redundant. Using if like this is equivalent to using a ternary expression (b ? signal1 : signal2) in another language that supports that syntax.


答:
Avoid strings and magic constants at all costs.  They are completely out of the question, they should not even be considered as options. This appears to leave you with only one viable option: identifiers, that is, enums.  However, there is also one more option, which in my opinion is the best. Let's call this option "Preloaded Objects". With Preloaded Objects, you can do the following:

```<code>if( user.FavouriteFruit.ID == MyApplication.Grape.ID )
</code>```

What has just happened here is that I have obviously loaded the entire row of Grape into memory, so I have its ID ready to use in comparisons.  If you happen to be using Object-Relational Mapping (ORM), it looks even better:

```<code>if( user.FavouriteFruit == MyApplication.Grape )
</code>```

(That's why I call it "Preloaded Objects".)

So, what I do is that during startup I load all of my "enumeration" tables (small tables like days of the week, months of the year, genders, etc.) into the main application domain class. I load them by name, because obviously, MyApplication.Grape must receive the row called "Grape", and I assert that each and every one of them is found.  If not, we have a guaranteed run-time error during startup, which is the least malignant of all run-time errors.

Body:
Avoid strings and magic constants at all costs.  They are completely out of the question, they should not even be considered as options. This appears to leave you with only one viable option: identifiers, that is, enums.  However, there is also one more option, which in my opinion is the best. Let's call this option "Preloaded Objects". With Preloaded Objects, you can do the following:

if( user.FavouriteFruit.ID == MyApplication.Grape.ID )

What has just happened here is that I have obviously loaded the entire row of Grape into memory, so I have its ID ready to use in comparisons.  If you happen to be using Object-Relational Mapping (ORM), it looks even better:

if( user.FavouriteFruit == MyApplication.Grape )

(That's why I call it "Preloaded Objects".)

So, what I do is that during startup I load all of my "enumeration" tables (small tables like days of the week, months of the year, genders, etc.) into the main application domain class. I load them by name, because obviously, MyApplication.Grape must receive the row called "Grape", and I assert that each and every one of them is found.  If not, we have a guaranteed run-time error during startup, which is the least malignant of all run-time errors.


答:
Storing them in both places (in a table and in an ENUM) is not that bad.  The reasoning is the following:

By storing them in a database table, we can enforce referential integrity in the database via foreign keys.  So when you associate a person or whatever entity to a fruit it is only a fruit that exists in the database table.

Storing them as an ENUM also makes sense because we can write code without magic strings and it makes the code more readable.  Yes, they do have to keep in sync, but really how hard would it be to add a line to the ENUM and a new insert statement to the database.

One thing, once an ENUM is defined, do not change it's value.  For example, if you had:

Apple

Grape

DO NOT rename Grape to Grapes. Simply add a new ENUM.

Apple

Grape

Grapes

If you need to migrate data, then apply an update to move all the Grape to Grapes.


问:
How to sort a list containing bounded set of values in linear time when length is unknown?
Given a list of integers whose length is unknown, and each of its elements lies between 1 to 1000, how does one sort this list in linear time?</p>

答:
You know that every element of your int arr[]; is in [1;1000].

So have an array of counters, int cnt[1001]; in C parlance. Clear it (all zeros).

Then, read the arr[] array sequentially. Suppose that you have read the value x  at index i  (so x==arr[i]). Then increment its counter, so cnt[x]++;

When you have reached the end of the input array arr, iterate on cnt so for (int i=0; i<=1000; i++) and output the number i exactly cnt[i] times.

This is O(n) (because the bound 1000 is a constant).

This sort is often known as the counting sort.


答: CodeRush! Get it! Love it!</p>

Title: How to become a "faster" programmer?

Body: CodeRush! Get it! Love it!

OwnerDisplayName: Bobby Ortiz


问:
Is it a good practice to have a pre-filled embedded database for development?
I'm currently working on a project which only uses its database for data storage. This means there are no triggers or stored procedure in it, just tables and data to put into it. In this scenario I'm building the application using Spring 4. The Spring container allows you to have profiles, which basically decide which beans get loaded in your project. You can make sure an embedded in-memory database or a full-fledged oracle database is used for data storage, without having to change a single thing in your code.

This is obviously using for unit and integration testing. Not having to rely on a database is great for your tests. I was wondering if the same doesn't count during development. In this specific project I'm not entirely sure about what my final data model will look like, and because I don't feel like writing SQL over and over to move columns I'd prefer to use some kind of in-memory database. Of course one with pre-filled development data, so I can mimic a system that's been filled with all kinds of data, and I can easily add more for small tests during development.

Is this a recommended practice or inherently bad in a way?

答:
Why not!

Personally, I prefer to have scripts that populate an empty database with new data so I can a) know exactly what is going in and b) to easily change them.

I do this even for our big production databases, keeping a script with the 'usual data' that I would otherwise have to enter by hand - things like a predefined user id for myself and standardised configuration.

This is probably more useful in development than it is for integration or especially QA, who should be creating their own DB using the tools provided in order to test that the tools still work with new versions.


问:
Read data from a device through usb port at high frequency
I need to know if I can get acceleration data from an IMU at 500Hz via USB.

I've been going through the code that the manufacturer provides and trying to improve it. Right now I cannot go faster than 166Hz. Things I've tried:

Switched from streaming mode to polling. The reason is that in streaming mode the SDK of the manufacturer uses the ioctl library to know if there is new data to be read. The faster I would go was 10Hz. In polling mode, every N milliseconds data gets read from the port using a standard read command. I went from 10Hz to 166Hz, that is, for N=6. But with N<6 I get no improvement. 166Hz is a ceiling right now.

Increased the process priority. This has no effect at all. Maybe because I'm only running the browser and the code I'm working on?

Yes, I know linux is not a real time system. At the end of the day I'll have to live with that. I may use xenomai later on the code. But right now I need to know how fast I can go. Not theoretically, but in a real application. Any ideas about how to increase the performance?

EDIT: This are the timestamps of the time I get the data, in millisencods. Note the bursts.

```<code> 1449258970519
 1449258970519
 1449258970525
 1449258970531
 1449258970531
 1449258970543
 1449258970543
 1449258970543
 1449258970549
 1449258970555
 1449258970555
 1449258970562
 1449258970567
 1449258970567
 1449258970573
 1449258970579
 1449258970579
 1449258970585
</code>```

EDIT: The amount of data that I have to transfer is very small... say at most 16 floats, plus some headers. I can safely assume 128 bytes is enough. So 128x1000 is still A LOT less than the 480 Mpbs that USB 2 offers.

答:
You can get 166Hz.. that's interesting as I thought the default polling rate for USB was 125Hz.

Still, you'll possibly need to modify your kernel, drivers/usb/input/hid-core.c - set the polling rate there. There's a fair bit of info on the internet about updating mouse polling rates to 1000Hz (those gamers....) either with a tweak to the configuration. It might have some info relevant to your device.

You'll probably have to post this question on a Linux board to get detailed info however.

The other alternative is to buffer - most cases don't matter if you poll at 100Hz if each read grabs 10 commands of data!


答:
Yes, this is a very good idea.

To make development easy, it is very useful to minimize the steps between "check out sources" and "system is running". Providing a pre-filled database helps with that. I have used this in the past and found it very helpful.

There are different ways of accomplishing that:

an SQL script that is run automatically (or manually) - often the simplest solution

you can write some code that populates the database using the same classes/framework that you use in production - lets you re-use code, and means that schema changes automatically apply to your test database

have some kind of DB dump that you load - less flexible to change, but useful if you want to use real data (of course, take care to anonymize/sanitize first!)

Ideally, the test data generation should run automatically during build or system startup.

Obviously, build in safeguards so all this will never run in production!

Title: Is it a good practice to have a pre-filled embedded database for development?

Body:
I'm currently working on a project which only uses its database for data storage. This means there are no triggers or stored procedure in it, just tables and data to put into it. In this scenario I'm building the application using Spring 4. The Spring container allows you to have profiles, which basically decide which beans get loaded in your project. You can make sure an embedded in-memory database or a full-fledged oracle database is used for data storage, without having to change a single thing in your code.

This is obviously using for unit and integration testing. Not having to rely on a database is great for your tests. I was wondering if the same doesn't count during development. In this specific project I'm not entirely sure about what my final data model will look like, and because I don't feel like writing SQL over and over to move columns I'd prefer to use some kind of in-memory database. Of course one with pre-filled development data, so I can mimic a system that's been filled with all kinds of data, and I can easily add more for small tests during development.

Is this a recommended practice or inherently bad in a way?


答:
This is a really good idea, and you should do this. I would go above and beyond, though, with preconfigured databases from which to choose as well as the ability to point to shared databases for the team.

You want to be able to reset your database. You may need it to be in some known, difficult to reproduce state to trigger a particular bug. Being able to test and fix those types of bugs is a huge help.

Being able to get up and running quickly is crucial. That is unproductive time at the beginning of a project, or during an iteration if you need to reset something.

Being able to switch between databases can be a huge help. What if QA reports a bug but you are unable to reproduce it? Sure, you can see it on the QA system, but that does not help you step through the code. Being able to reconfigure your development environment to use a different database quickly and easily can help reduce the feedback cycle.

I would have a set of scripts for quickly adding specific data to a new database for testing specific scenarios. These could be SQL scripts, or automated test scripts such as Selenium.

Being able to configure and use a database quickly and arbitrarily is a tremendous help in development with many upsides and only one downside that I can think of:

It makes it easy to have multiple people using the same database. If multiple developers and testers are modifying the same data, it may actually invalidate their tests as they step on each others' toes. While this might not be a big deal in production, it can be frustrating in development and testing where data may need to be in a specific state during testing and debugging.


问:
How many classes are too many?
I'm making a Book Manager App using Java Swing that allows me to do a variety of things such as opening a txt list of books, searching for books, adding/removing books.

There are different classes for the different types of books e.g. Fictional Books have their own genre field and history books have their own period field. All of these books extend an abstract book class which holds the base fields for these classes.

I'm using the MVC pattern and I'm happy enough with the model but my main concern lies with the view.

I currently have 22 classes with 14 of them focused on the View aspect of the model. I basically have classes for the majority of the components. Such as a file chooser which extends JFileChooser, a MenuBar class and classes for the different book panels. Each Sub book panel extends from a general book Panel by taking all of its textfields and labels and appending their own.

I've also implemented the Builder design pattern that lets me create a range of components so far a I have 2 builder classes both implementing an interface one for textfields and one for labels.

My question is, is all of this a little over the top? Is it better to have everything crammed into the main class or is it better to have a lot of classes?

答:
You're asking the wrong question. It's not "better to have everything crammed into the main class", nor is it "better to have a lot of classes".

When we distance ourselves from such attempts at generating rigid rules to cover all cases, we arrive at a much better approach: abide by widely-accepted design principles, such as those embodied by SOLID, and we will end up with an appropriate number of classes for your codebase.

Whatever that number is.

For GUIs in particular you will tend to end up with a lot of classes as you add more widgets; this is normal. The scenario you describe does not sound frightening to me.

Title: How many classes are too many?


答:
Write less code.

Banish Not-Invented-Here and make good use of existing libraries/frameworks/toolkits to provide common (and generally non-defining) functionality so that you only need to write what's new.  For the parts that you do need to write yourself, build them with re-use in mind so that you won't have to write them again for the next project.

Even if you don't increase the number of lines of working code you produce per day, you can still get more done in less time by making each line do more.

Title: How to become a "faster" programmer?

OwnerDisplayName: Dave Sherohman


答:
You question is three-fold:

1. How many classes are too many?

While there are some guides on some metrics like how long should a method be or how many parameters can a method have, there are no such metrics as to how many classes should a system have as a maximum.

IMHO it's not so much the amount of classes that create complexity as it is the fact of their not being cohesive and their being strongly coupled to each other. If the classes are too big, there's a problem and there are metrics for that. If the classes are tightly coupled there's another problem.

In the other hand, how many abstract classes or interfaces, while not being a direct indicator of complexity, it can give you a hint. There should be a relatively high ratio of implementing classes to interfaces. That mean that your abstractions are good and are serving a good purpose. If you have a relatively 50/50 interfaces to implementers ratio then you have bad abstractions or not good enough design. The latter doesn't apply at the early stages of development since there will obviously be roughly the same amount of interfaces than that of implementors.

2. Is all of this (all my classes) a little over the top?

See previous question. But also ask yourself whether you (your team) feel overworked of overwhelmed by the amount of things to take care of. In that case help should be asked. Splitting work between presentation-layer programmers and business rule programmers could help.

3. Is it better to have everything crammed into the main class or is it better to have a lot of classes?

You are answering yourself here by using the word "crammed". That's a known anti-pattern called God-class. You should avoid it.

Bottomline: it's not the amount of components but the relationships between them what make a system too complex to maintain.

Don't look at the amount of classes. Make an UML diagram leaving out all leaf classes. Does it look clean and simple, or at least manageable? If it does, then you are OK.

Title: How many classes are too many?

Body:
I'm making a Book Manager App using Java Swing that allows me to do a variety of things such as opening a txt list of books, searching for books, adding/removing books.

There are different classes for the different types of books e.g. Fictional Books have their own genre field and history books have their own period field. All of these books extend an abstract book class which holds the base fields for these classes.

I'm using the MVC pattern and I'm happy enough with the model but my main concern lies with the view.

I currently have 22 classes with 14 of them focused on the View aspect of the model. I basically have classes for the majority of the components. Such as a file chooser which extends JFileChooser, a MenuBar class and classes for the different book panels. Each Sub book panel extends from a general book Panel by taking all of its textfields and labels and appending their own.

I've also implemented the Builder design pattern that lets me create a range of components so far a I have 2 builder classes both implementing an interface one for textfields and one for labels.

My question is, is all of this a little over the top? Is it better to have everything crammed into the main class or is it better to have a lot of classes?

ContentLicense: CC BY-SA 4.0


答:
500Hz through USB, assuming 8 or 16 bit data, should be no problem. I've worked on an application that sampled 4 16 bit channels at 8kHz, and I didn't even have to give it any consideration to make it read that fast.

How are you hitting your 166Hz ceiling? Are you at 100% cpu usage on the client? Are you hitting 100% utilization on your USB device? What USB interface are you using? Is it custom by the manufacturer of your device, or does it use something common like an FTDI USB-UART chip?

For some devices and interfaces there is a way to specify how many bytes should be buffered before the OS indicates there is anything to be read. Set that to 1 byte and see if that solves your problems.

Without more specific details about what device, what interface, and how you're communicating over USB, we can't solve your problems, we can only speculate.

Edit: If you need your software to make a decision and respond back at a 500hz granularity, you're out of luck, as no desktop operating system is real-time enough to reliably do that. If you just need to read the data and make a decision 'every now and then' as in every few 10s of milliseconds or less frequently, then you'll be fine.

Title: Read data from a device through usb port at high frequency

Body:
I need to know if I can get acceleration data from an IMU at 500Hz via USB.

I've been going through the code that the manufacturer provides and trying to improve it. Right now I cannot go faster than 166Hz. Things I've tried:

Switched from streaming mode to polling. The reason is that in streaming mode the SDK of the manufacturer uses the ioctl library to know if there is new data to be read. The faster I would go was 10Hz. In polling mode, every N milliseconds data gets read from the port using a standard read command. I went from 10Hz to 166Hz, that is, for N=6. But with N<6 I get no improvement. 166Hz is a ceiling right now.

Increased the process priority. This has no effect at all. Maybe because I'm only running the browser and the code I'm working on?

Yes, I know linux is not a real time system. At the end of the day I'll have to live with that. I may use xenomai later on the code. But right now I need to know how fast I can go. Not theoretically, but in a real application. Any ideas about how to increase the performance?

EDIT: This are the timestamps of the time I get the data, in millisencods. Note the bursts.

```<code> 1449258970519
 1449258970519
 1449258970525
 1449258970531
 1449258970531
 1449258970543
 1449258970543
 1449258970543
 1449258970549
 1449258970555
 1449258970555
 1449258970562
 1449258970567
 1449258970567
 1449258970573
 1449258970579
 1449258970579
 1449258970585
</code>```

EDIT: The amount of data that I have to transfer is very small... say at most 16 floats, plus some headers. I can safely assume 128 bytes is enough. So 128x1000 is still A LOT less than the 480 Mpbs that USB 2 offers.


问:
Centralize permission evaluation
I am looking for a viable design/architecture pattern for securing several web applications. Here is the situation:

Multiple web applications must enforce roles and permissions at Java service method level

Roles and permissions have to be stored centrally, in a separate component

How the roles and permissions are enforced (i.e., the behavior to be applied to roles and permissions) are also to be enforced centrally. This is important because we would like to standardize how methods are secured and how roles and permissions are enforced in order to avoid the risk of clients messing it up.

The options I am considering are:

Write a java client which would apply the behavior using some annotations - then individual web applications just need to include this jar file and use the annotation (cons for this approach: jar file rollout, dependency management)

Provide a web service which the methods can utilize (cons: it may not scale as calling an external service for a cross cutting concern would not scale)

How can I best solve this problem?

答:
What you are looking for is called externalized authorization. There are different ways of doing it and like some of the commenters point out, Apache Shiro, Spring Security, Microsoft Claims are all ways of externalizing authorization.

If you look at the broader access control picture, you will see there are different acccess control models:

access control lists (ACL),

role-based access control (RBAC), and

attribute-based access control (ABAC)

All of these can be used to externalize authorization though typically ACLs are not.

Apache Shiro and Spring Security achieve role-based access control i.e. you define users, roles, and permissions. You assign users to roles and roles to permissions. The information is stored centrally e.g. in an LDAP and the applications consume those. This will let you achieve coarse-grained access control. How the apps consume the roles and permissions and what the apps do with these is all up to the app developer. So in a way it is not fully externalized.

ABAC goes further. ABAC defines:

an architecture

a policy-based approach

a request/response scheme to query for authorization

ABAC is typically implemented using a standard called XACML, the eXtensible Access Control Markup Language.

In ABAC's architecture you will find the following components:

the policy enforcement point (PEP): this is the piece inside your app or in front of your app that you will use to query for authorization. This is the piece that externalizes your authorization. In your app for instance, you will use the PEP to create a request of the type Can Alice view document #123? The PEP will send that to the central authorization engine

the policy decision point (PDP): this is the central authorization engine. It is configured with policies. Those policies are built using attributes. For instance doctors can view the medical record of a patient if they have a care relationship with that patient. Attributes can be retrieved from databases, web services, and LDAPs. The PDP receives the request from the PEP and evaluates it. It eventually reaches a decision e.g. Permit or Deny which it returns to the PEP.

the policy information point (PIP): the PIP is the source of additional attributes the PDP might need e.g. a user's role or department, a document's status...

the policy administration point (PAP)

The diagram below summarizes the architecture.

Have a look at either:

Apache Shiro (free and open source)

Axiomatics Policy Server (commercial)

Title: Centralize permission evaluation