答:
It depends.

Obviously, the more layers in your code the slower it goes.  But... there comes a point where direct end-to-end performance doesn't matter as much as scalability. If you have 1 user accessing your database on a local PC, it can go fast. If you have a thousand users accessing the same DB on the same PC, chances are you're going to see them all get frustrated. The solution is to move the DB to another box, add a layer in the middle and although for 1 user, it will perform slower, when the thousand access it, it will go faster. (that's a simplistic answer but true in principle).

There are other reasons to hide your DB behind a middle tier layer, such as security.


问:
Knowing when a SQL table is done updating before Querying
I am using a self hosted windows service WEB API as the layer between my Windows 8 App and a SQL Server Database because Windows 8 apps can't directly connect to SQL server like win forms or pretty much anything else .net.

The way the process is going to work is that when I make a call to web api from the application, it will insert a row in a specified table. When that happen's, a SQL function is triggered which populates tables with the most current data that I'll need to send to the application.

How can I know when this operation is complete from WEB API's perspective so I know I can query the table and send the data to the client?

答:
You can take a look at the tran_locks for an exclusive lock (due to the write access). If the lock is done, everything should be fine.

```<code>select * from sys.dm_tran_locks
where resource_database_id = DB_ID(N'YOUR DB')
AND request_session_id = [YOUR SESSION]
</code>```

Another idea is to take a look at the requests and check if your last request is still running or already sleeping. But that way you need to wait till everything is done. The first idea is better if you just want to ensure that the write process is done.

ContentLicense: CC BY-SA 3.0

Body:
You can take a look at the tran_locks for an exclusive lock (due to the write access). If the lock is done, everything should be fine.

select * from sys.dm_tran_locks
where resource_database_id = DB_ID(N'YOUR DB')
AND request_session_id = [YOUR SESSION]

Another idea is to take a look at the requests and check if your last request is still running or already sleeping. But that way you need to wait till everything is done. The first idea is better if you just want to ensure that the write process is done.


问:
Is anything in programming truly evil?
So, there are a bunch of questions appearing asking is X evil, is Y evil.

My view is that there are no language constructs, algorithms or whatever which are evil, just ones which are badly used.  Hell, if you look hard enough there are even valid uses of goto.

So does absolute evil, that is something which is utterly incompatible with best practice in all instances, exist in programming?  And if so what is it?  Or is it just bad programmers not knowing when something is appropriate?

Edit: To be clear, I'm not talking about things programmers do (such as not checking return codes or not using version control - they're choices made by bad programmers), I mean tools, languages, statements, whatever which are just bad...

答:
Is anything in programming truly evil?
  I mean tools, languages, statements, whatever

Nothing on that list is evil. Here is why.

Humans were created as beings with a free will. They may use their powers to go into the dark side or embrace the light side. This choice defines what comes out of their hands.

Now, those tools and frameworks were created by benevolent people who were genuinely trying to make things somehow better with a good will. Consequently, their creations (either successful or not is irrelevant) do not bear the imprint of evil. At the least they are neutral, but not malicious.

Then those tools come into the hands of other individuals. And whatever they do with them depends on those people. Even a debugger could be turned into an evil instrument in the hands of a hacker trying to remove the serial number check in some software.

But it does not redefine in any way the characteristic of the tool. They are all good. Some are more useful, some less. Some dangerous, some less. But still, they are all good and quite useful in certain scenarios.

And if a programmer by a mistake misuses a tool and causes damage, the tool does not become evil. It's just a programmer mistake, lack of knowledge, ignorance, whatever. But without any evil intent.

Bottom line is, all tools are good. We must love them all.

ContentLicense: CC BY-SA 2.5

Title: Is anything in programming truly evil?


问:
What are the most used pattern to manage a lot of interconnected parameters?
I recently start to work on an application that drive different measurement device.

Before the user start a measure, she sets the parameters of it.

Actually, considering all measurements type there are 50+ parameters.

The difficulty here is that every settings depends on others for:

Being available

List of available values

so on..

Moreover, some measurement settings depend on previous measure results and settings.

To make it short : we have a lot of stuff that are interconnected.

The actual pattern is to validate everything as soon as a values is changed. It cost a lot (in time) and we are going to add a lot of more parameters : it will break.

We try to implement a pattern where we use ObservableValues and where all parameters register on all values it depend on.

It became hard when the parameters depend on an other reference measure. If the reference change, we have to stop listening on the previous reference and start to listen on the new reference.

Etc...

An other issue is that when we work on our pattern and we had more capabilities (like serialization), or when we had some helper class (like factory), we build big files with 50+ parameters or functions.

Is there any other good pattern or library to do it ?

答:
You could consider using a mediator to decouple parameter event dependencies. This is sometimes known as an Event Aggregator. Your individual parameter classes are wired up to publish and subscribe to events directly through the EA.

The following example uses Reactive Extensions but you could implement a simple version yourself using callbacks for subscription and publishing.

```<code>public class EventAggregator
{
    private Subject<Event> _events;
    public IObservable<Event> Events { get { return _events; } }

    public void Publish(Event evt)
    {
        _events.OnNext(evt);
    }
}

public class Parameter<T> : IDisposable
{
    private readonly EventAggregator _ea;
    public Parameter(EventAggregator ea, string name, T initialValue)
    {
        this._ea = ea;
        this._name = name;
        this._value = initialValue;
        this._subscription = ea.Events.Subscribe(evt => HandleEvent(evt));
    }

    private readonly string _name;
    public string Name { get { return this._name; } }

    private T _value;
    public T Value
    {
        get { return this._value; }
        set
        {
            this._value = value;
            this._ea.Publish(new ValueChangedEvent(this._name, this._value);
        }
    }

    private void HandleEvent(Event evt)
    {
        // decide what to do with an event here - change availability of the parameter etc
    }

    public void Dispose()
    {
        this._subscription.Dispose();
    }
}
</code>```

You create a single EventAggregator and pass it as a dependency to your Parameter objects:

```<code>var eventAggregator = new EventAggregator();
var length = new Parameter<int>(eventAggregator, "Length", 50);
var width = new Parameter<int>(eventAggregator, "Width", 50);

// this will fire an event to the event aggregator which can be handled by other parameters
width.Value = 20;

// this unsubscribes from the event aggregator
length.Dispose();
</code>```

Body:
You could consider using a mediator to decouple parameter event dependencies. This is sometimes known as an Event Aggregator. Your individual parameter classes are wired up to publish and subscribe to events directly through the EA.

The following example uses Reactive Extensions but you could implement a simple version yourself using callbacks for subscription and publishing.

public class EventAggregator
{
    private Subject<Event> _events;
    public IObservable<Event> Events { get { return _events; } }

    public void Publish(Event evt)
    {
        _events.OnNext(evt);
    }
}

public class Parameter<T> : IDisposable
{
    private readonly EventAggregator _ea;
    public Parameter(EventAggregator ea, string name, T initialValue)
    {
        this._ea = ea;
        this._name = name;
        this._value = initialValue;
        this._subscription = ea.Events.Subscribe(evt => HandleEvent(evt));
    }

    private readonly string _name;
    public string Name { get { return this._name; } }

    private T _value;
    public T Value
    {
        get { return this._value; }
        set
        {
            this._value = value;
            this._ea.Publish(new ValueChangedEvent(this._name, this._value);
        }
    }

    private void HandleEvent(Event evt)
    {
        // decide what to do with an event here - change availability of the parameter etc
    }

    public void Dispose()
    {
        this._subscription.Dispose();
    }
}

You create a single EventAggregator and pass it as a dependency to your Parameter objects:

var eventAggregator = new EventAggregator();
var length = new Parameter<int>(eventAggregator, "Length", 50);
var width = new Parameter<int>(eventAggregator, "Width", 50);

// this will fire an event to the event aggregator which can be handled by other parameters
width.Value = 20;

// this unsubscribes from the event aggregator
length.Dispose();


问:
How to translate from a programming language to another?
I want to write a small DSL that will need to compile to another high level programming language (similar to C, but a lot more limited).

What is the high level overview of what the compiler should do?

To clarify, I'm especially interested into a conversion from an high level programming language to another relatively high level programming language, not in compilation in general.

答:
To clarify, I'm especially interested into a conversion from an high level programming language to another relatively high level programming language, not in compilation in general.

Well, I have some bad news for you - translating from one language to another is compilation in general. Thinking that you can skip steps because you're working with high level languages, or because your language is very limited is only going to bring you trouble.

Compilers are a well researched, well known, and well documented sort of software. The standard approach to building them is not as complicated as it may seem, and the general approach will save a pile of headaches and complexity down the road by dealing with the boatloads of corner cases that comprise even the simplest of languages.


问:
Can I write a cross-platform application (Mac and Windows) using C#?
I see lots of old information on this question, and lots of articles flying around the Interwebs, but I can't tell quite where things are at.

Basically, I want to write C# code that I can then compile into a Windows application, and also compile into a Mac application (no Parallels, no Wine, etc.).  Can this be done?

Are products like Xamarin the way to go?  Xamarin's web site says "Use the same language, APIs and data structures to share an average of 75% of app code across all mobile development platforms." but I can't tell what that 25% of app code that is platform-specific might entail ... I also can't tell if they target OSX and Windows, their web site is very mobile-device-centric.

[added later]

Summary: Xamarin is not designed to make cross-platform applications - what they give you is a way to write Mac applications in C#, using the Mac's GUI operations.  What they recommend is to write all your "business logic" cross-platform, and then write 2 completely separate GUIs - one for Windows, one for Mac.

C#/Qt seems to be in its very early phases (though if anyone is stumbling onto this article in 2016 or later, go check it out).

It appears that mono will do cross-platform including GUI, but only if you use Windows Forms.

答:
Xamarin doesn't "target" Windows, because there is no point - whatever you write in C#, is ready to run on Windows in and of itself. That's why they're only selling licences for Xamarin.Android, Xamarin.iOS and Xamarin.Mac. But what would a Xamarin.Windows abstraction layer be supposed to do? That would be a textbook snake oil product :)

"Native Windows application" - native is a tricky word when it comes to C#. C# apps typically require either .NET or Mono runtime, whether created with the use of Xamarin or not. If you'd like to create native binaries rather than IL, there's solutions such as .NET Native or ngen, but it's a matter independent of using Xamarin. I don't suppose you meant "native" in this strict sense, but it's good to be aware of this distinction. With C#, even on Windows you typically use a virtual machine of sorts.

Xamarin.Mac requires Mono.OSX runtime so that C# code can be built at all. And it bridges Objective-C runtime with Mono.OSX as described in Xamarin.Mac Internals:

There is the Objective-C based runtime containing instances of native classes (NSString, NSApplication, etc) and there is the C# runtime containing instances of managed classes (System.String, HttpClient, etc). In between these two works Xamarin.Mac creates a two way bridge so you can call methods (selectors) in Objective-C (such as NSApplication.Init) and Objective-C can call you back (like methods on your app delegate)

So yes, the resulting application is "native" in the sense that it's not - say - a piece of HTML wrapped in some WebView component. You do have access to native UI elements and APIs. It's not native in the sense that it requires Mono.OSX to be installed. In other words, it is as native as C# on Windows gets under default circumstances.

You might also want to familiarize yourself with http://blog.xamarin.com/introduction-to-xamarin.mac-seminar/ - it could be dated (2013), but the core thruths hold I believe. Take a glimpse at slide #7.

"I can't tell what that 25% of app code that is platform-specific might entail" - by definition, everything that isn't platform-agnostic, so, pretty much "pure" business logic, and whatever you abstract away from concrete implementations (like some "IRepository" interface etc.). Whatever is platform-specific though: user interface, database persistance, 3rd party libraries for Windows or Mac, cannot be shared. The 75/25 proportion is very rule of thumbish, your mileage may vary a lot depending on the nature of your application.

Note that I've only had some non-commercial experience with Xamarin for mobile platforms, and it didn't cover iOS (just Windows Phone and Android). If I am inaccurate on any of this, please correct me.

ContentLicense: CC BY-SA 4.0


问:
Estimating scrum points for non cross-functional team
We are working to transition our development team to be more Agile. I have a question about story point estimation and their relationship to an individual's skill.

The scenario is that our team members do not have cross-functional skills. One might focus entirely on front-end, another only does back-end.

The problem we fail to plan accordingly to our team composition. We figure, if our average velocity is let's say 200 points per sprint, then that number should not be exceeded. However, tasks vary each sprint and sometimes there are too few back-end tasks or too few front-end tasks.

My question is - should we divide and plan back-end and front-end tasks separately, or is there a better way?

答:
If you have zero or very little backend development (or vice-versa), for all practical purposes, you treat that person like you would if she went on vacation or worked on another project. A particular sprint gets the total story points adjusted.

From a management perspective, you could treat this as an opportunity to cross-train people with pair-programming. If cross-training is one of the goals of this project, then count both developer's time/points. You'll have a more accurate way to plan a project that involves cross-training.


答:
If you are translating some DSL to some generated C code (or something similar), look first at this and that answers.

I actually am doing the same within MELT, a Lispy domain specific language to extend the GCC compiler

You did not explain what kind of DSL are you thinking of, and how would the generated C code be used. Do you generate a standalone program, or some "plugin" or "library"?

A significant issue is memory management. You definitely should read about garbage collection (e.g. read the GC handbook).

Of course, translating some high-level domain specific language to C (or some other C-like language, or even any programming language which is lower-level than your DSL) is some form of compilation.

If you translated from a lower-level DSL to a higher-level language, you could read about decompilation and perhaps static binary analysis.

BTW, even for a "small DSL" and a simple C-like (or Pascal-like) target language, it is a lot of work (probably a year at least).

What generating some C-like language (with a good compiler implementation) brings you is that you might work less on optimization techniques inside your own compiler, with the realistic hope that the C (or Pascal, etc...) compiler you are generating code for would be able to optimize your generated code. You could also consider generating Gimple (with the new libgccjit from recent GCC) or LLVM or use some other JIT library (e.g. libjit).

Title: How to translate from a programming language to another?

Body:
I want to write a small DSL that will need to compile to another high level programming language (similar to C, but a lot more limited).

What is the high level overview of what the compiler should do?

To clarify, I'm especially interested into a conversion from an high level programming language to another relatively high level programming language, not in compilation in general.


答:
Null is the root of Evil!

https://softwareengineering.stackexchange.com/questions/22912/null-references-the-billion-dollar-mistake-closed

The Billion Dollar Mistake: I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years. In recent years, a number of program analysers like PREfix and PREfast in Microsoft have been used to check references, and give warnings if there is a risk they may be non-null. More recent programming languages like Spec# have introduced declarations for non-null references. This is the solution, which I rejected in 1965. C.A.R. Hoare, 2009

Title: Is anything in programming truly evil?

Body:
So, there are a bunch of questions appearing asking is X evil, is Y evil.

My view is that there are no language constructs, algorithms or whatever which are evil, just ones which are badly used.  Hell, if you look hard enough there are even valid uses of goto.

So does absolute evil, that is something which is utterly incompatible with best practice in all instances, exist in programming?  And if so what is it?  Or is it just bad programmers not knowing when something is appropriate?

Edit: To be clear, I'm not talking about things programmers do (such as not checking return codes or not using version control - they're choices made by bad programmers), I mean tools, languages, statements, whatever which are just bad...


问:
Should I check parameter before using it in methods?
I'm going to build some public PHP packages, Following standards is a priority for me.

PHP lets users call methods even if they don't pass required parameters to it.

My question. Should I check parameter before using it in methods? (like following example)

```<code>public function setVariable($value)
{
    if(isset($value))
        throw new \InvalidArgumentException("Value must be an int value!");
    $this->variable = (int) $value;
}
</code>```

Or just do my job!

```<code>public function setVariable($value)
{
    $this->variable = (int) $value;
}
</code>```

答:
I'm going to build some public  public PHP packages.

Public as in "Open Source", "public to the whole world"? Then do yourself a favor and do as much validation as you can as long as you do not suffer serious performance problems. Even when you provide some documentation saying "this function does not handle left out parameters", expect a lot of people completely ignoring your package's documentation not reading all the gory details of your documentation in full. And if a problem occurs, the error message should users give a clear description of what they did wrong, otherwise they will blame your package, might blame you, and send you dozens of support mails.

So I recommend not only to throw an exception, but to make your the error messages in those exceptions are as clear as possible.


问:
Multiple threads making call to precompiled library
I have been asked this question in an interview and I thought to take advice from you guys.

Say I have a pre-compiled library which references to some function say "strtok".

There is no source code for this library.

```<code>$ nm lib.a | grep strtok
      U _strtok
</code>```

Your target system uses a RTOS and you have multiple threads which
make calls into the library.

Q) Why might this be a problem?

To my understanding, this shouldn't be a problem . I mean  there is nothing wrong in calling same function from different threads. If you want to ensure that your variables are consistent it is advisable to provide thread synchronization mechanisms to prevent crashes and race conditions

答:
If you want to ensure that your variables are consistent it is advisable to provide thread synchronization mechanisms to prevent crashes and race conditions

I think you just hit what they were after.  In any language you will have parts of it that are not thread safe and need synchronization mechanisms to prevent issues (look at the .net List for a specific example that I am familiar with).  This is even more true with third party libraries.

Calling the same function from 2 threads is only problematic when synchronization is not considered and the library is not thread safe*.

*of course there may be specifics I am unaware of.


答:
When the team draws stories during the sprint planning, they should take their available skills into account. That means they might draw a backend-heavy story and leave a frontend-heavy story on the board if they have free backend capacity, or vice versa.

If nothing else, some programmers might find time for necessary refactoring.

If the imbalance persists for a long time, re-train or change the team.

Title: Estimating scrum points for non cross-functional team

Body:
We are working to transition our development team to be more Agile. I have a question about story point estimation and their relationship to an individual's skill.

The scenario is that our team members do not have cross-functional skills. One might focus entirely on front-end, another only does back-end.

The problem we fail to plan accordingly to our team composition. We figure, if our average velocity is let's say 200 points per sprint, then that number should not be exceeded. However, tasks vary each sprint and sometimes there are too few back-end tasks or too few front-end tasks.

My question is - should we divide and plan back-end and front-end tasks separately, or is there a better way?


问:
Isolating test data in acceptance tests
I'm looking for guidance on how to keep my acceptance tests isolated. Right now the issue I'm having with being able to run the tests in parallel is the database records that are manipulated in the tests. I've written helpers that take care of doing inserts and deletes before tests are executed, to make sure the state is correct. But now I can't run them in parallel against the same database without uniquely generating the test data fields for each test. For example.

Testing creating a row i'll delete everything where column A = foo and column B = bar
Then I'll navigate through the UI in the test and create a record with column A = foo and column B = bar.

Testing that a duplicate row is not allowed to be created. I'll insert a row with column A = foo and column B = bar and then use the UI to try and do the exact same thing. This will display an error message in the UI as expected.

These tests work perfectly when ran separately and serially. But I can't run them at the same time for fear that one will create or delete a record the other is expecting. Any tips on how to structure them better so they can be run in parallel?

答:
At Intelliware, we use "home state switching" to address this problem.  We have an internally developed framework called Runway that abstracts away the complexity, but here's how it works in a nutshell:

When a tests starts, it makes a remote call to a home state controller get a database to use for the test.

The home state controller creates a new database from scripts, or uses one from the pool of available homestates (created, but not currently in use)

The unique UUID is returned to the test

The test includes the UUID as a header on all requests

The system-under-test detects the UUID header and routes all database calls to the test db. This is how you get parallelization - different requests get routed to different databases depending on the header.

At its completion, the test returns the database to the 'available pool'.

This approach depends on being able to spin up new database schemas quickly and reliably. We typically use Flyway for this, but eventually you may need to export a snapshot if the migration starts taking a long time.

The aforementioned home state controller is a standalone, lightweight web application that never gets deployed outside a test environment.

See also

Intelliware Runway (video)

Title: Isolating test data in acceptance tests


答:
There's no objective absolute evil.

The problem with things like GOTO is that it's awfully hard to USE it in a way that's not nasty. Enough so that it's hard for me sitting right here now to think of an example of GOTO that isn't code smell. But I'm willing to accept that there might be such uses, and that the problem isn't the tool itself but rather the likelihood of its abuse.

Title: Is anything in programming truly evil?


答:
strtok is not thread-safe because it saves its state in static memory between calls. If two threads make concurrent or interleaved calls to strtok, they will get erroneous results. The strtok_r variant returns a pointer to the caller to use for future invocations on the same string. There are many other C and POSIX functions that are not safe to use from multiple threads.[1,2]

In modern software, with multithreading being commonplace, it is less likely that library implementers will build unsafe functions like this. However, you cannot assume that is the case by default.

Thread-safety and POSIX.1
http://www.unix.org/whitepapers/reentrant.html

The GNU C Library: Nonreentrancy
http://www.gnu.org/software/libc/manual/html_node/Nonreentrancy.html

Title: Multiple threads making call to precompiled library

Body:
I have been asked this question in an interview and I thought to take advice from you guys.

Say I have a pre-compiled library which references to some function say "strtok".

There is no source code for this library.

```<code>$ nm lib.a | grep strtok
      U _strtok
</code>```

Your target system uses a RTOS and you have multiple threads which
make calls into the library.

Q) Why might this be a problem?

To my understanding, this shouldn't be a problem . I mean  there is nothing wrong in calling same function from different threads. If you want to ensure that your variables are consistent it is advisable to provide thread synchronization mechanisms to prevent crashes and race conditions


答:
As of PHP5, type hinting is a thing. The most important distinctions are the following:

You cannot type hint scalar/numerical, string, Resource, or Trait data.

You can force parameters that are objects, arrays, or anonymous functions.

You can type hint using interface names, class names, object keyword, array keyword, or callable keyword (for anonymous functions/callbacks).

When a type hinted function or method is called with mismatched parameters, a catchable fatal error will occur (not an Exception).

If you specify null as a default value, type hinting is somewhat useless.

I would suggest at least checking isset and is_null before running your algorithm, and I highly suggest further type checking with things like is_string, is_int, is_float, etc. Type casting is also valid. Edit: this only applies for when you cannot use type hinting for a function, e.g. scalar parameters.

Returning false or throwing an Exception are all context specific questions -- you have to determine which fits each particular function/algorithm in your package, which will include considering how someone using your code should interact with it. My favorite example of contrived Exception throwing is Sentry. I mean, look at all of those catch blocks. Maybe you'll want your code to throw that many things to provide different information, or perhaps you can return a boolean, or perhaps you can throw a new Exception("Message text"). It's a personal choice for each piece of code. I recommend looking at potential applications where your package will be useful and see how well your package would integrate with those coding styles.

tl;dr version

The only primitives you can type hint are array and object. You can type cast or type check other primitives. Function/method calls with parameters that fail to satisfy type hints results in a catchable fatal error. What to do when parameters sent to your function/method are mismatched is a personal, context specific choice that nobody can definitively answer without seeing your code.

Edit: see also https://stackoverflow.com/a/5724696/2103394

Body:
I'm going to build some public PHP packages, Following standards is a priority for me.

PHP lets users call methods even if they don't pass required parameters to it.

My question. Should I check parameter before using it in methods? (like following example)

```<code>public function setVariable($value)
{
    if(isset($value))
        throw new \InvalidArgumentException("Value must be an int value!");
    $this->variable = (int) $value;
}
</code>```

Or just do my job!

```<code>public function setVariable($value)
{
    $this->variable = (int) $value;
}
</code>```


答:
Yes, one should divide the tasks by work area expertise.  This will allow for accurate resource and project/sprint planning.  Let's say after story point estimation you have 100 front end points and 50 back end points over the course of the next few months of 2 week interations.

If this were the case, at a very high level one would need to finish equally:

2 front end developers and 1 back end developer OR

1 front end developer and 1 Generalist (Front/Back) OR

1 front end developer and 1 back end developer (part time - 50%)

Story point estimation is the first step.  The second is planning out resource assignments, work streams + dependencies.

Every project is different and stocked with people with different skill sets.  There are few "full stack"  developers out there that are good doing the UI front end all the way back to the back to the database.  Even within a general techlogy like .Net there are specializations like ASP.NET MVC, WCF services, Entity Framework, etc.  So, take in the developers skill sets into account when planning out the sprints from the backlog and there will be efficient utilitzation of resources across the board.


答:
When planning in Scrum, the first thing to realize is that you should not try to plan the work to keep everybody busy, but rather you should try to plan a certain amount of functionality that your team will try to deliver at the end of the sprint.

In Scrum, the input to the planning is the product backlog, which should contain the desired features in the order that they provide the most value to the business. During the planning, you take items from the top of the product backlog until you have enough work to fill a sprint.

If the top of the product backlog happens to contain a lot a features that involve mostly front-end work, then it can happen that the sprint is "full" before you reach the 200 story-point mark. Full here means that the front-end developers are fully loaded.

Within the Scrum framework, there are a few possibilities to deal with this situation:

If you foresee a lot of back-end work coming up in the backlog, which would result in the same situation in the next sprint, you can negotiate with the Product Owner to exchange some front-end heavy stories for back-end heavy stories to create a more even workload over the team.

If there is nothing suitable to exchange, or if the Product Owner doesn't agree with a swap, then it is good to realize that there are always tasks that can be picked up by any team member. If there are no back-end related tasks that the back-end developers can work on, they should try to assist the front-end developers to the best of their capabilities so that the team can finish the work the planned.

Examples of work that the back-end developers could pick up are:

Write test specifications

Execute manual tests

Write unit tests

If they have some familiarity with the front-end technology, perform reviews

etc.

In Scrum, the goal should not be that you as individual work at maximum capacity/productivity, but that the team produces that what benefits the business the most.


问:
Git clone to memory
I'm making an app that is required to clone git repositories by a link to it and analyze the codebase.

Pulling to the disk using the git clone command wouldn't scale well. Is there any way to clone to memory, or at least to get a stream of file contents, instead of intermediary disk I/O.

答:
It is operating system specific.

If your application runs on Linux (so perhaps also on Android), you can use a memory based file system like tmpfs. So git pull  (or git clone, etc...)  would put it in that FS which sits in virtual memory, and it will run quite fast. However, the bottleneck is probably the network (unless your application is running in some datacenter and can use a big lot of bandwidth).

BTW, on Linux, the page cache is quite effective. So even with ordinary (disk-based) file system, performance can be quite good in practice.

Then your application would access these files thru usual file-related syscalls or library functions (e.g. <stdio.h> in C), and that would only use memory (without any real disk IO) and should scale quite well.

Of course, you'll need to have enough RAM for that to work well.

Title: Git clone to memory


问:
Is it poor practice to return different data types from alternative Web API endpoints?
Consider a Web API with an endpoint api\LargeItem

A LargeItem is a pretty big object with lots of properties and long strings.

A user can GET any one like so api\LargeItem\1 which returns the LargeItem having Id 1

However we often need to populate lists of these items for user selection and when doing that we never need the big rich hydrated object.

On one hand the user can expect to just call api\LargeItem and to retrieve all of them... but I never intend my user to consume the big collection of rich objects like that. When they want a list I'd rather give them SmallerVersionOfLargeItem which maybe just has Id and Name, for example.

Is there a guideline or standard for whether it's ok to return differently shaped objects from the same names endpoint given different URL inclusions?

Is it preferred to have an endpoint like api\SmallerVersionOfLargeItems\?

EDIT This is equivalent to the ShouldSerialize concept in JSON.Net except without using a ?fields property in the API call.

To clarify: My Api api doesn't use the terms "Large" and "Small" in reality. I'm just using them here to illustrate that these are just different fieldsets of the same object.

答:
This endpoint:

```<code>api\Items
</code>```

Is not the same as this one:

```<code>api\Items\1
</code>```

Therefore, they can return different responses.  As you have indicated, getting items may just return a list with just a few properties while getting an item by ID may return a heavier object.  Avoid decriptive terms such as "small" and "large" in your api/endpoints if the endpoints are named in that fashion.

Also, putting a version on the endpoint would be adviseable as well.  Bottom line, the simpler the API is, the easier it will be for others to use.

Body:
This endpoint:

api\Items

Is not the same as this one:

api\Items\1

Therefore, they can return different responses.  As you have indicated, getting items may just return a list with just a few properties while getting an item by ID may return a heavier object.  Avoid decriptive terms such as "small" and "large" in your api/endpoints if the endpoints are named in that fashion.

Also, putting a version on the endpoint would be adviseable as well.  Bottom line, the simpler the API is, the easier it will be for others to use.


答:
I try to follow the following:

For externally-called code, do as much explicit error handling as possible, to provide the best experience for the caller and highest level of protection for your own code against inbound data that might partially succeed in unintended ways.

For private calls, I'd explicitly handle bad data if it was possible for that data to somehow get through the call without failing violently on its own.  So, I guess I'd call this defensive trapping.

It seems to me that API writers should try to handle invalid data and provide great error messages on all external calls, both to help the user and to help yourself when you invariable have to support some production issue.

Title: Should I check parameter before using it in methods?


答:
There is a fundamental conflict between with the git model and what you are trying to do here.  git clone makes a full copy of the repository on your local machine. The idea of git is to keep a complete copy of the repository locally.   There is nearly no communication with the server with most commands.  The only time there is communication is when you git fetch (pull down all branch changes from the server) or git push (push all local branch changes to the remote repository) or of course when you make a complete copy with git clone.

So really, I think you are stuck.  What I gather is that you want to pull down lots of different projects, run analysis on each, then delete the results.  That's not something git was designed for as it is fundamentally about keeping and modifying a local copy of a repository for a long period of time.

I do question your assumption that pulling to memory would even make a difference.  Network operations are a couple orders of magnitude slower than disk operations.  A decent non-SSD drive is going to give you around 150 MB/s.  And SSD is going to triple that.  Unless you have a much better connection than I do, pulling to memory would not speed things up at all as your OS is spending all its time waiting for network requests to the git server.

If you are working with github, you may be better off with the "Download ZIP" method on each project page.  This will download a branch without all the extraneous branch/history information.  That should be faster than doing a git pull for cases where you only need the latest version of one branch.

Title: Git clone to memory

Body:
I'm making an app that is required to clone git repositories by a link to it and analyze the codebase.

Pulling to the disk using the git clone command wouldn't scale well. Is there any way to clone to memory, or at least to get a stream of file contents, instead of intermediary disk I/O.


问:
Testing strategy for coupled systems
We have 2 coupled applications: a ReadOnly view and an Admin management view.

The ReadOnly view is updated by changes from the Admin view.

Each application is owned by different teams. Each application should be deployed separately.

Basically, we're not clear on how to perform integration tests without needing to test both all the time.

Right now, we open the ReadOnly view, check some assumptions, open the Admin view and modify data, then reload the ReadOnly view to verify things have updated as expected.

Can anyone give some feedback on whether there's a better way?

In the current approach, we'll need to run 2 sets of integration tests for each change to the ReadOnly view or the Admin view. Indeed, we've got a few ReadOnly views (different products) that would be run in this matter.

Any feedback / thoughts is appreciated!

答:
If I understand your question correctly, these two views both access some kind of state (presumably in a database), the AdminView is capable of modifying it as well as reading it, and the ReadOnly view can only read it. In which case...

Most of your tests (in particular, most of your automated tests) should only test one of these views.

The ReadOnly view tests should take the form "here's some data, give it to the view, do the right widgets appear on the screen?"

The AdminView tests should take the form "here's some data, perform an operation on it, does the modified data look right?" or perhaps "here's some data, do the right widgets appear, click this widget, does the modified data look right?".

The "data" I'm nebulously referring to should have some kind of schema (be it a database schema, an XML schema, a JSON schema, whatever) that thoroughly, unambiguously and precisely defines the possible forms this data can take. As long as this schema never changes in a backwards-incompatible way, testing each view in isolation remains a valid strategy.

Of course you'll still want to test the two views together (including the glue code that talks to the real database), but those don't need to be super-thorough test suites. Indeed, trying to make a super-thorough integration test suite usually causes a hopeless combinatorial explosion in practice. It's the "unit tests" (or at least, the single-view tests) where it's worth spending time to get a tight feedback loop for red-green-refactor-style TDD and/or perfect code coverage to increase confidence in your tests' results.

Title: Testing strategy for coupled systems


问:
Divide time available by task type
We’re a team of a few programmers working on different projects, enhancing current features adding new features and of course fixing bugs as they come to our attention.
Problem is that we need a way to efficiently divide the time available between the various tasks. we can’t work a few weeks on bug fixes and let all new features waiting, and the same is true for bugs it can’t wait for too long before getting fixed, that’s why we need to find a tool to divide the time.

Our plan is to add an estimate for each task and categorize it accordingly, this part is available in FogBugz and other similar task management tools I tried.

The other part I can’t find in the tools available is how to assign developers time to the task, and get a report on how much time is left for each category.
For instance I want to divide the available time as;
25% Bug Fixes
50% New Features
25% Enhance current features

So the tool should calculate all tickets estimates, and tell me how much time I have available.

We’re open to hear other suggestions on how to manage time available against different various task types, if that’s not the standard way of doing it.

答:
You will find that it is nearly impossible to plan the work down to the granularity that you are proposing.

If you want to have the flexibility to respond quickly to changing requests from the business, while still giving the developers the stability that they can finish what they are working on before being asked to switch to a different task, then I would advice to take a look at Agile methods and Scrum in particular.

In Scrum, the team works in short iterations (between 1 and 4 weeks in length).

You would put all your bugs, new features and feature improvements in one big list and order that list in the order that gives the most value to the business. Then at the start of an iteration, the team takes as much work from the top of the list as they can handle in one iteration.

During an iteration, you can't change what the team is working on, so if an important bug comes in you can put it on top of the list and it will stay there at most for the duration of one iteration (half an iteration on average).

Title: Divide time available by task type


问:
URI construction for REST API and use of verbs as resource?
I have two questions.

1) I have a issue of understanding the correct URL construction for a REST API. For example,

```<code>https://api.context.io/lite/users/id/email_accounts/label/folders/folder/messages/message_id
</code>```

Here as you can see the folder comes under folders resource and message_id comes under messages. Is it necessary to show the parent resource or collection. Will it be wrong to have a URL such as this?

```<code>https://api.context.io/lite/userid/folderid/message_id
</code>```

Here, the userid, folderid and message_id maybe would indicate as integers or some other value. Of course with the values the URL doesn't make sense, but does it matter in m2m transaction? What is the best way to construct the URL?

2) Is it ok to have 'verbs' in the URL? I have seen the following urls which has verbs such as 'move', 'send' in the URL. Is that correct? If it is wrong, should we do everything such as 'search' using parameters only?

```<code>POST https://outlook.office365.com/api/v1.0/me/messages/{message_id}/move
POST https://www.googleapis.com/gmail/v1/users/userId/messages/send
</code>```

Please share your thoughts. I read lots of articles on these. Yet it seems to be more of a personal choose rather than an Industry standard. What would Dr. Fielding would recommend?

Thanks in advance.

答:
1) It's typically best to be explicit about the display of your rested resources. Thus, it makes sense to follow the collection/:id/sub-collection/:id paradigm with your API.

2) It's considered an anti-pattern in the REST world to use anything other than nouns for your collections in your URIs. That's because REST best practices expect your "verbiage" to come in the form of HTTP verbs, and not something you've decided to declare on your own. This isn't a REST constraint by any means, but it's considered the best possible implementation, and allows developers to keep things consistent.

The HTTP verbs GET POST PUT and DELETE are suitable for any need your program may have. Oftentimes, if you feel you "need more verbs" in order to successfully implement a feature, it really means you need to expand your thinking of the resource on which that call is being made.

For example: if I wanted to login a user, my first thought may be to put that action on the User class via a User#login method. That'd get pretty weird, though, right? We're using a verb, and we now know that's a REST anti-pattern.

Instead, what if we created an associated Session class? Now we can conveniently call Session#create on a new instance of the model (that's associated with User) and stay RESTful as hell!

I really hope this helps. Remember: when in doubt, keep in mind that no matter what you're doing, the community has found a way to keep things RESTful, as there are some BIG OL' applications out there that adhere pretty strictly to the rules of REST.

UPDATE

Why not use a class method? I haven't seen your code, but I'm assuming you're using something like rails for your api.

You could do the following:

```<code>///items/index.html.erb
<% form_tag items_path, method: :get do %>
<p>
  <%= text_field_tag :search, params[:search] %>
  <%= submit_tag "Search", name: nil %>
</p>
<% end %>

///items_controller.rb
def index
  @items = Item.search(params[:search])
end

///models/item.rb
def self.search(search)
  if search
    find(:all, conditions: ['name LIKE ?', "%#{search}%"])
  else
    find(:all)
  end
end
</code>```

This is obviously fairly vague object naming, but I think you'll get the idea. That way, you're always using a GET HTTP verb, and doing it in a restful manner. The other option, yes, is to use query params. This helps you avoid doing that.

Body:
1) It's typically best to be explicit about the display of your rested resources. Thus, it makes sense to follow the collection/:id/sub-collection/:id paradigm with your API.

2) It's considered an anti-pattern in the REST world to use anything other than nouns for your collections in your URIs. That's because REST best practices expect your "verbiage" to come in the form of HTTP verbs, and not something you've decided to declare on your own. This isn't a REST constraint by any means, but it's considered the best possible implementation, and allows developers to keep things consistent.

The HTTP verbs GET POST PUT and DELETE are suitable for any need your program may have. Oftentimes, if you feel you "need more verbs" in order to successfully implement a feature, it really means you need to expand your thinking of the resource on which that call is being made.

For example: if I wanted to login a user, my first thought may be to put that action on the User class via a User#login method. That'd get pretty weird, though, right? We're using a verb, and we now know that's a REST anti-pattern.

Instead, what if we created an associated Session class? Now we can conveniently call Session#create on a new instance of the model (that's associated with User) and stay RESTful as hell!

I really hope this helps. Remember: when in doubt, keep in mind that no matter what you're doing, the community has found a way to keep things RESTful, as there are some BIG OL' applications out there that adhere pretty strictly to the rules of REST.

UPDATE

Why not use a class method? I haven't seen your code, but I'm assuming you're using something like rails for your api.

You could do the following:

///items/index.html.erb
<% form_tag items_path, method: :get do %>
<p>
  <%= text_field_tag :search, params[:search] %>
  <%= submit_tag "Search", name: nil %>
</p>
<% end %>

///items_controller.rb
def index
  @items = Item.search(params[:search])
end

///models/item.rb
def self.search(search)
  if search
    find(:all, conditions: ['name LIKE ?', "%#{search}%"])
  else
    find(:all)
  end
end

This is obviously fairly vague object naming, but I think you'll get the idea. That way, you're always using a GET HTTP verb, and doing it in a restful manner. The other option, yes, is to use query params. This helps you avoid doing that.


问:
What were the "core" API packages of Java 1.0?
Reading about the Google v Oracle case, I came across these questions (apparently from the presiding Judge)

...

  Is it agreed that the following is true, at least as of 1996?

The following were the core Java Application Programming Interface: java.lang, java.util and java.io.

Does the Java programming language refer to or require any method, class or package outside the above three?

  ...

source: Groklaw

There are obviously lots of legal ramifications, Google and Oracle probably disagree on some points, and I don't care.  Leave law to the lawyers.

However, I suspect there's an interesting bit of history in here.

My question is (as someone who first did any Java coding around 2001 in version 1.3), in version 1.0 of Java was anything required outside of java.lang, java.util, and java.io to compile a valid Java program?

As an example (using C# 5.0), the await keyword is dependent upon Task<T> GetAwaiter() (amongst other things).  The compiler couldn't function to spec without that class.

Equivalently, were there any core runtime features (like ClassLoader*) that were dependent on other packages?

I'll admit I ask out of curiosity, exactly what is necessary for minimum-viable Java (the language, ignoring all the legal bits around it) is interesting.

*I am assuming that ClassLoader was even a feature in Java 1.0, it's part of the spec in 7.0 and presumably many earlier versions.

答: Java 1.0 had exactly six top-level packages: java.applet, java.awt (with subpackages java.awt.image and java.awt.peer), java.io, java.lang, java.net, and java.util. That's it.  Source: Patrick Chan, Java Developers Almanac 1998</p>

OwnerDisplayName: Kevin Montrose


问:
When is optimization not premature and therefore not evil?
"Premature optimization is root of all evil" is something almost all of us have heard/read. What I am curious what kind of optimization not premature, i.e. at every stage of software development (high level design, detailed design, high level implementation, detailed implementation etc) what is extent of optimization we can consider without it crossing over to dark side.</p>

答:
Most people would call optimization premature, if you're optimizing something that isn't resulting in a "soft failure" (it works but it's still useless) of the system due to performance.

Real world examples.

If my bubble sort takes 20ms to run, optimizing it to 1ms quicksort
is not going to enhance the overall utility in any meaningful way
despite being a 2000% performance increase.

If a web page takes 20s to load and we decrease it to 1s, this
can increase the utility of the website from 0 to near infinity.
Basically something that was broken because it was too slow, is now
useful.


问:
How to quantify the work perfomed by a developer/programmer?
I know the best four naive ways of achieving this.

Commit count: In the code repository count the number of commits done by a user. However this is just what the name says, counting the commits. It cannot separate near empty commits from commits having hours of thinking and sweat.

Line count: Counting the number of lines of code produced by a developer. However developers can practice verbose programming, use inefficient algorithms which take lots of code to be made, or just create ugly and large code. So it is hard to use this as a good metric of the work performed.

Time count: I could count the time that a developer spent working. But no one can work one hour straight. Moreover, some programmers can preform 2h of work in just 1h, and vice-versa. And in the end is not the time that counts but the progress the project made. I don't care if a developer spends 5 years or 5 minutes coding, in the end the project is to be completed in its due date. This leads to our final bullet:

Feature count: Counting the number of features closed by a developer will keep track of the overall project's progress. However I cannot tell if those features were toy-features or they were insanely hard to accomplish. Moreover, I cannot tell one developer from another in order to see who performed most of the work. I could estimate the difficulty of each feature, however most of the times that is unfeasible because (i) it is frequent that unexpected problems arise during a feature, (ii) we tend to over simplify things, (iii) for some significant amount of features the work of estimating their difficulty greatly overlaps with the task of doing it, i.e., we can only know how long it takes to close that feature after closing it.

So, in the end, how can one measure, quantify and compare the work performed by developers in a project?

答:
The purpose of developers is not to "write code". The purpose of ANY engineer, be they software engineer, civil engineer, or whatever is to SOLVE PROBLEMS.

Therefore, it IS possible to judge a developer on their work, but not in any of the ways that you listed (sort of...).

Number 4 is on the right track, but not quite complete. Judge developers on how well they:

Interact with their business users/team members (if you are a big enough company to have BA's, then substitute business users with BA's)

Solve the issues presented to them. Do they take an inordinate amount of time compared to other developers to solve similar problems?

Abstract complex issues down to smaller chunks of work and create meaningful subtasks, and then execute on those subtasks.

Add value to the business.

As you can see, it isn't easily quantifiable. Just like many other professions, it is about concepts far more nebulous than "how much code they write".


问:
Ternary operator considered harmful?
For example, would you prefer this one-liner

```<code>int median(int a, int b, int c) {
    return (a<b) ? (b<c) ? b : (a<c) ? c : a : (a<c) ? a : (b<c) ? c : b;
}
</code>```

or an if/else solution involving multiple return statements?

When is ?: appropriate, and when is it not? Should it be taught to or hidden from beginners?

答:
Can I say it? I can't manage to find this particular application of the ternary operation evil:

the operation it performs is pretty trivial, and once you went trough it once it's hardly possible some bugs will come out;

what it does is clearly stated in the function name;

taking >1 line for something so obvious and that so clearly wouldn't be improved in the future (unless a magic median algorithm has lived undetected until now).

Please have mercy, my reputation is pretty pitiful already.

Title: Ternary operator considered harmful?


问:
Advantage of non-deterministic staistical analysis?
I am working with a Java Library used for identifying the language of a given text.  It relies on an n-gram analysis of the text to return a set of languages it might be and the "confidence" that it is that language.

I wrote a wrapper class to run it, however, it is non-deterministic (results are generally incorrect with <200 chars input, don't worry about that):

```<code>C:\wamp\www\langdetect [master]> java Detect viking rowboat
af:0.8571411945873898 lt:0.1428569473433962
C:\wamp\www\langdetect [master]> java Detect viking rowboat
af:0.5714268373011915 sw:0.2857137271014559 lt:0.14285898741269984
C:\wamp\www\langdetect [master]> java Detect viking rowboat
af:0.9999958253321346
</code>```

The analysis is non-deterministic by default (can be made deterministic like so: https://code.google.com/p/language-detection/issues/detail?id=64) but my question is this:

Is there any advantage to this analysis being non-deterministic?

The only upside I can think of is that a deterministic algorithm will, statistically, be incorrect a percentage of the time (even with valid input), while non-deterministic removes this guarantee (but still is incorrect sometimes).  Does this even make sense?

答: It sounds like it is returning results before the results have really converged. I'd find the varying results troubling and untrustworthy. The fix should be to run more samples, not use a "deterministic" algorithm that consistently returns the same misleading result. Also, if the results are unreliable with short input strings, you should not trust highly confident percentages like 0.99999: I would discount the confidence numbers, or assume error bars like 0.99999 ± 0.9.</p>

OwnerDisplayName: Will

OwnerDisplayName: John Kugelman


答:
Committed line count is the measure I use. I find it is reasonably reliable. There is a big difference between a guy who is committing 15,000 lines a year and another who is committing 3,000 lines a year, and another who is committing 60,000 lines a year. Back in the day when I wrote a lot of code I maxed out at about 50,000 lines a year. The best programmers I have had personal experience with do about 200,000 lines a year. Guys that do that, the real burners, have to be very heads down and are hard to find. One guy I knew who was that level is a VP now and makes $500,000 a year.

You also have to take into account whitespace. There are some guys who put every brace on its own line, so you have to multiply by 0.8 to adjust for all the white space. In general, though, its just tweaking. Usually the good programmers will be producing double or triple or quadruple what the weakies do, so the whitespace doesn't really matter much in the long run.

The very first year I worked full time as a programmer I wrote an MS Access DB application. It took me the whole year to write it. After the whole thing was written and fully debugged and working, I did a line count. It was 400 lines of code. Nowadays, I could write that same application in a single day. Gives you some idea of the difference in skill. Nowadays, if I go all out, like its Goole Code Jam or something I can write about 1000 working/debugged lines a day, so in theory I could do 250,000 lines a year if I did nothing but program. Of course, I spend all my time now going to meetings, writing specs, winning new business and answering questions on SO, so I do a lot less than that.

Because of this last factor, you have to weigh what the person is actually doing. Do you have them spending weeks writing specs or help files? That is going to hit their line count. Are they talking to clients or bidding for business. Ditto. The line count is mainly an accurate measure for guys who are strictly coding.

Note that you have to take into account what phase the project is in. If the project is new, the line count will be a lot higher than an old project that is getting debugged. Also, as projects get big and hairy, the line count decreases somewhat.

You will find some "manglers" out there who have the idea that measuring by commit count will encourage people to commit junk code, but in practice that is not what happens. Anyone who commits a lot of junk ends up wasting a lot of time debugging, so they don't really gain anything by committing junk. In general, the more productive a coder is, the higher quality their code is, and in fact, it has to be. If you are writing 1000 lines a day, they better be GOOD or you will spend a lot of time debugging.

------Postscript

I think it is pretty comical that the number one answer says there is no way to quantify programming skill/productivity and then recommends that you have a lot of meetings. LOL. The icing on the cake is that the guy is from London, the epicenter of the program-by-having-meetings world.

I guess I should modify my answer somewhat. I was assuming that what you were trying to do is write working computer programs. If you work for a big, rich, entitled company where the goal is to maximize employment, make your government happy, be green, and have a wholesome work environment where everybody is social and nice to everybody, then by all means have lots of the meetings, that's the secret to success. Measuring people is bad, might make them feel inferior, can't have that.

Title: How to quantify the work perfomed by a developer/programmer?

Body:
I know the best four naive ways of achieving this.

Commit count: In the code repository count the number of commits done by a user. However this is just what the name says, counting the commits. It cannot separate near empty commits from commits having hours of thinking and sweat.

Line count: Counting the number of lines of code produced by a developer. However developers can practice verbose programming, use inefficient algorithms which take lots of code to be made, or just create ugly and large code. So it is hard to use this as a good metric of the work performed.

Time count: I could count the time that a developer spent working. But no one can work one hour straight. Moreover, some programmers can preform 2h of work in just 1h, and vice-versa. And in the end is not the time that counts but the progress the project made. I don't care if a developer spends 5 years or 5 minutes coding, in the end the project is to be completed in its due date. This leads to our final bullet:

Feature count: Counting the number of features closed by a developer will keep track of the overall project's progress. However I cannot tell if those features were toy-features or they were insanely hard to accomplish. Moreover, I cannot tell one developer from another in order to see who performed most of the work. I could estimate the difficulty of each feature, however most of the times that is unfeasible because (i) it is frequent that unexpected problems arise during a feature, (ii) we tend to over simplify things, (iii) for some significant amount of features the work of estimating their difficulty greatly overlaps with the task of doing it, i.e., we can only know how long it takes to close that feature after closing it.

So, in the end, how can one measure, quantify and compare the work performed by developers in a project?


问:
Cleanest way to write logically procedural software in an OO language
I'm an electrical engineer and I don't know what the hell I'm doing. Please save the future maintainers of my code.

Recently I've been working on a number of smaller programs (in C#) whose functionality is logically "procedural". For example, one of them is a program which collects information from different databases, uses that information to generate a sort of summary page, prints it, and then exits.

The logic required for all of that is around 2000 lines. I certainly don't want to stuff all of that in a single main() and then "clean it up" with #regions as some previous developers have been doing (shudder).

Here are some things I have already tried without much satisfaction:

Make static utilities for each coarse bit of functionality, e.g. a DatabaseInfoGetter, SummaryPageGenerator, and a PrintUtility. Making the main function look like:

```<code>int main()
{
    var thisThing = DatabaseInfoGetter.GetThis();
    var thatThing = DatabaseInfoGetter.GetThat();
    var summary = SummaryPageGenerator.GeneratePage(thisThing, thatThing);

    PrintUtility.Print(summary);
}
</code>```

For one program I even went with interfaces

```<code>int main()
{
    /* pardon the psuedocode */

    List<Function> toDoList = new List<Function>();
    toDoList.Add(new DatabaseInfoGetter(serverUrl));
    toDoList.Add(new SummaryPageGenerator());
    toDoList.Add(new PrintUtility());

    foreach (Function f in toDoList)
        f.Do();
}
</code>```

None of this feels right. As the code gets longer, both of these approaches start to get ugly.

What's a good way to structure stuff like this?

答:
Since you are not a professional programmer, I would recommend sticking to simplicity. It will be a LOT easier for programmer to take your modularized, procedural code and make it OO later, than it will be for them to fix a badly written OO program. If you are not experienced, it is possible to create OO programs that can turn into an unholy mess which will neither help you or whoever comes after you.

I think your first instinct, the "this thing-that thing" code in the first example is the right track. It is clear and obvious what you want to do. Don't worry too much about code efficiency, clarity is much more important.

If a code segment is too long, break it into bite-sized chunks, each having its own function. If it is too short, consider using less modules and putting more in line.

---- Postscript: OO Design Traps

Working successfully with OO programming can be tricky. There are even some people who consider the whole model flawed. There is a very good book I used when first learning OO programming called Thinking in Java (now in its 4th edition). The same author has a corresponding book for C++. There is actually another question on Programmers dealing with common pitfalls in object-oriented programming.

Some pitfalls are sophisticated, but there are plenty of ways to create problems in very basic ways. For example, a couple of years ago there was an intern at my company who wrote the first version of some software I inherited and he made interfaces for everything that might someday have multiple implementations. Of course, in 98% of the cases there was only a single implementation ever, so the code was loaded with unused interfaces which made debugging very annoying because you can't step back through an interface call, so you end up having to do a text search for the implementation (although now I use IntelliJ was has a "Show All Implementations" feature, but back in the day I didn't have that). The rule here is the same as for procedural programming: always hardcode one thing. Only when you have two or more things, create an abstraction.

A similar kind of design blunder can be found in the Java Swing API. They use a publish-subscribe model for the Swing menu system. This makes creating and debugging menus in Swing a complete nightmare. The irony is that it is completely pointless. There is virtually never a situation in which multiple functions would need to "subscribe" to a menu click. Also, publish-subscribe was a complete misfire there because a menu system is normally always in use. It's not like functions are subscribing and then unsubscribing randomly. The fact that "professional" developers at Sun made a blunder like this just shows how easy it is even for pros to make monumental screw ups in OO design.

I am a very expert developer with decades of experience in OO programming, but even I would be the first to admit there is tons I don't know and even now I am very cautious about using a lot of OO. I used to listen to long lectures from a co-worker who was an OO zealot about how to do particular designs. He really knew what he was he was doing, but in all honesty I had a hard time understanding his programs because they had such sophisticated design models.


问:
Fast indexing of k-combinations
I'm revisiting an old problem that I was working on some time ago.

A typical scenario is "3 bits are set within an 8 bit integer", i.e. 00000111.

All unique combinations with 3 set bits can easily be generated (in order) by nested loops. What I'm interested in is the mapping index <-> combination, i.e. "00001011" would be the second combination (or value "1" in a zero-based index).

So far, I ran through all combinations and stored them in a table, making the look up index -> conversation a O(1) operation. The other direction is O(ln(n)) with bisect search.

The downside, however, is that this obviously is heavy on the memory if we increase the domain, up to a point where it's not feasible.

What would be a simple way to calculate the n.th combination or the index for a given combination? Order of combinations would be nice, but is not mandatory.

答:
Generating the n-th combination is called a "unranking" algorithm. Note that permutations and combinations can often equated by the way the problem is parameterized. Without knowing exactly what the problem is, it is difficult to recommend the exact right approach, and in fact, for most combinatoric problems there are usually several different ranking/unranking algorithms possible.

One good resource is "Combinatorial Algorithms" by Kreher and Stinson. This book has many good ranking and unranking algorithms clearly explained. There are more advanced resources, but I would recommend Kreher as a starting point. As an example of an unranking algorithm consider the following:

```<code>/** PKSUL : permutation given its rank, the slots and the total number of items
 *  A combinatorial ranking is number of the permutation when sorted in lexicographical order
 *  Example:  given the set { 1, 2, 3, 4 } the ctItems is 4, if the slot count is 3 we have:
 *     1: 123    7: 213   13: 312   19: 412
 *     2: 124    8: 214   14: 314   20: 413
 *     3: 132    9: 231   15: 321   21: 421
 *     4: 134   10: 234   16: 324   22: 423
 *     5: 142   11: 241   17: 341   23: 431
 *     6: 143   12: 243   18: 342   24: 432
 *  From this we can see that the rank of { 2, 4, 1 } is 11, for example. To unrank the value of 11:
 *       unrank( 11 ) = { 11 % (slots - digit_place)!, unrank( remainder ) }
 * @param rank           the one-based rank of the permutation
 * @param ctItems        the total number of items in the set
 * @param ctSlots        the number of slots into which the permuations are generated
 * @param zOneBased      whether the permutation array is one-based or zero-based
 * @return               zero- or one-based array containing the permutation out of the set { ctItems, 1,...,ctItems }
 */
public static int[] pksul( final int rank, final int ctItems, final int ctSlots, boolean zOneBased ){
    if( ctSlots <= 0 || ctItems <= 0 || rank <= 0 ) return null;
    long iFactorial = factorial_long( ctItems - 1 ) / factorial_long( ctItems - ctSlots );
    int lenPermutation = zOneBased ? ctSlots + 1 : ctSlots;
    int[] permutation = new int[ lenPermutation ];
    int[] listItemsRemaining = new int[ ctItems + 1 ];
    for( int xItem = 1; xItem <= ctItems; xItem++ ) listItemsRemaining[xItem] = xItem;
    int iRemainder = rank - 1;
    int xSlot = 1;
    while( true ){
        int iOrder = (int)( iRemainder / iFactorial ) + 1;
        iRemainder = (int)( iRemainder % iFactorial );
        int iPlaceValue = listItemsRemaining[ iOrder ];
        if( zOneBased ){
            permutation[xSlot] = iPlaceValue;
        } else {
            permutation[xSlot - 1] = iPlaceValue;
        }
        for( int xItem = iOrder; xItem < ctItems; xItem++ ) listItemsRemaining[xItem] = listItemsRemaining[xItem + 1]; // shift remaining items to the left
        if( xSlot == ctSlots ) break;
        iFactorial /= ( ctItems - xSlot );
        xSlot++;
    }
    if( zOneBased ) permutation[0] = ctSlots;
    return permutation;
}
</code>```

This is permutation unranking, but as mentioned above, in many cases you can convert a combination unranking into an equivalent permutation problem.

Title: Fast indexing of k-combinations

Body:
Generating the n-th combination is called a "unranking" algorithm. Note that permutations and combinations can often equated by the way the problem is parameterized. Without knowing exactly what the problem is, it is difficult to recommend the exact right approach, and in fact, for most combinatoric problems there are usually several different ranking/unranking algorithms possible.

One good resource is "Combinatorial Algorithms" by Kreher and Stinson. This book has many good ranking and unranking algorithms clearly explained. There are more advanced resources, but I would recommend Kreher as a starting point. As an example of an unranking algorithm consider the following:

/** PKSUL : permutation given its rank, the slots and the total number of items
 *  A combinatorial ranking is number of the permutation when sorted in lexicographical order
 *  Example:  given the set { 1, 2, 3, 4 } the ctItems is 4, if the slot count is 3 we have:
 *     1: 123    7: 213   13: 312   19: 412
 *     2: 124    8: 214   14: 314   20: 413
 *     3: 132    9: 231   15: 321   21: 421
 *     4: 134   10: 234   16: 324   22: 423
 *     5: 142   11: 241   17: 341   23: 431
 *     6: 143   12: 243   18: 342   24: 432
 *  From this we can see that the rank of { 2, 4, 1 } is 11, for example. To unrank the value of 11:
 *       unrank( 11 ) = { 11 % (slots - digit_place)!, unrank( remainder ) }
 * @param rank           the one-based rank of the permutation
 * @param ctItems        the total number of items in the set
 * @param ctSlots        the number of slots into which the permuations are generated
 * @param zOneBased      whether the permutation array is one-based or zero-based
 * @return               zero- or one-based array containing the permutation out of the set { ctItems, 1,...,ctItems }
 */
public static int[] pksul( final int rank, final int ctItems, final int ctSlots, boolean zOneBased ){
    if( ctSlots <= 0 || ctItems <= 0 || rank <= 0 ) return null;
    long iFactorial = factorial_long( ctItems - 1 ) / factorial_long( ctItems - ctSlots );
    int lenPermutation = zOneBased ? ctSlots + 1 : ctSlots;
    int[] permutation = new int[ lenPermutation ];
    int[] listItemsRemaining = new int[ ctItems + 1 ];
    for( int xItem = 1; xItem <= ctItems; xItem++ ) listItemsRemaining[xItem] = xItem;
    int iRemainder = rank - 1;
    int xSlot = 1;
    while( true ){
        int iOrder = (int)( iRemainder / iFactorial ) + 1;
        iRemainder = (int)( iRemainder % iFactorial );
        int iPlaceValue = listItemsRemaining[ iOrder ];
        if( zOneBased ){
            permutation[xSlot] = iPlaceValue;
        } else {
            permutation[xSlot - 1] = iPlaceValue;
        }
        for( int xItem = iOrder; xItem < ctItems; xItem++ ) listItemsRemaining[xItem] = listItemsRemaining[xItem + 1]; // shift remaining items to the left
        if( xSlot == ctSlots ) break;
        iFactorial /= ( ctItems - xSlot );
        xSlot++;
    }
    if( zOneBased ) permutation[0] = ctSlots;
    return permutation;
}

This is permutation unranking, but as mentioned above, in many cases you can convert a combination unranking into an equivalent permutation problem.


问:
Split program to smaller programs
I have for planned to build my own drone from scratch for learning purpose and will program all controls for the drone. My question is if it's a good idea to split the code that doesn't interact with each other to another program or just have them in different threads.

For example I have one program for controlling the drone and then one program for taking images.

The pros I have found of this is:

If on of the program crashes the other will be just fine.

Which also leads to that I could have backup programs if one crash.

Some negative I have found is that

All the signals I send back and forth to it would be checked multiple
times.

Total file size will be larger.

答:
I would advise against multi-threading, especially for a real-time application. The main questions are how to use the cores, if it is a multi-core processor, and how to manage multiple processes (as you have already noted).

Firstly, I would recommend putting sensor processing into its own process in all cases for each sensor. For example, if you have an imager and a IMU, each one should have its own process. Any specialized high-speed code for processing the sensor data stream should be in the sensor handler. In general, if the sensor is critical, like the IMU, it should be given its own dedicated core. Non-critical processes should share whatever cores are left over. Obviously you either need an RTOS to do this (recommended), or a specially configured version of Linux.

Other than the sensor processing, everything else should usually be in the same monolithic process unless you have some very specific and specialized need for it to be in a separate process. This monolithic process should be designed so that it allocates appropriate amounts of processing time to the different tasks and should have a top-level exception handler that guarantees that any problem will be resolved in a useful way.

It is also a good idea to have two heartbeat monitors. These processes check the other processes and restart them if they have crashed. There are two of them because if one dies, you need the other to restart it.

You can communicate between processes at high speed using shared memory. Doing this is usually much faster and simpler than messing around with IPC or any OS-moderated communication.

Title: Split program to smaller programs


答:
The first approach is fine. In procedural languages, like C, the standard approach is to divide functionality into namespaces - using static classes like DatabaseInfoGetter is basically the same thing. Obviously this approach leads to simpler, more modular and more readable/maintainable code than shoving everything into one class, even if everything is broken down into methods.

Speaking of which, try to limit methods to performing as few actions as possible. Some programmers prefer a little less granularity, but huge methods are always considered harmful.

If you're still struggling with complexity, you most likely need to break down your program even further. Create more levels in the hierarchy - maybe DatabaseInfoGetter needs to reference some other classes like ProductTableEntry or something. You're writing procedural code but remember, you ARE using C#, and OOP gives you a bunch of tools to reduce complexity, e.g.:

int main()
{
    var thisthat = Database.GetThisThat();
}

public class ThisThatClass
{
    public String This;
    public String That;
}

public static class Database
{
    public ThisThatClass GetThisThat()
    {
        return new ThisThatClass
        {
            This = GetThis(),
            That = GetThat()
        };
    }

    public static String GetThis()
    {
        //stuff
    }
    public static String GetThat()
    {
        //stuff
    }

}
```

Also, be careful with static classes. Database classes are good candidates.. as long as you usually have one database.. you get the idea.

Ultimately if you think in mathematical functions and C# doesn't suit your style, try something like Scala, Haskell, etc. They're cool too.

Title: Cleanest way to write logically procedural software in an OO language

Body:
I'm an electrical engineer and I don't know what the hell I'm doing. Please save the future maintainers of my code.

Recently I've been working on a number of smaller programs (in C#) whose functionality is logically "procedural". For example, one of them is a program which collects information from different databases, uses that information to generate a sort of summary page, prints it, and then exits.

The logic required for all of that is around 2000 lines. I certainly don't want to stuff all of that in a single main() and then "clean it up" with #regions as some previous developers have been doing (shudder).

Here are some things I have already tried without much satisfaction:

Make static utilities for each coarse bit of functionality, e.g. a DatabaseInfoGetter, SummaryPageGenerator, and a PrintUtility. Making the main function look like:

```<code>int main()
{
    var thisThing = DatabaseInfoGetter.GetThis();
    var thatThing = DatabaseInfoGetter.GetThat();
    var summary = SummaryPageGenerator.GeneratePage(thisThing, thatThing);

    PrintUtility.Print(summary);
}
</code>```

For one program I even went with interfaces

```<code>int main()
{
    /* pardon the psuedocode */

    List<Function> toDoList = new List<Function>();
    toDoList.Add(new DatabaseInfoGetter(serverUrl));
    toDoList.Add(new SummaryPageGenerator());
    toDoList.Add(new PrintUtility());

    foreach (Function f in toDoList)
        f.Do();
}
</code>```

None of this feels right. As the code gets longer, both of these approaches start to get ugly.

What's a good way to structure stuff like this?

Body:
The first approach is fine. In procedural languages, like C, the standard approach is to divide functionality into namespaces - using static classes like DatabaseInfoGetter is basically the same thing. Obviously this approach leads to simpler, more modular and more readable/maintainable code than shoving everything into one class, even if everything is broken down into methods.

Speaking of which, try to limit methods to performing as few actions as possible. Some programmers prefer a little less granularity, but huge methods are always considered harmful.

If you're still struggling with complexity, you most likely need to break down your program even further. Create more levels in the hierarchy - maybe DatabaseInfoGetter needs to reference some other classes like ProductTableEntry or something. You're writing procedural code but remember, you ARE using C#, and OOP gives you a bunch of tools to reduce complexity, e.g.:

int main()
{
    var thisthat = Database.GetThisThat();
}

public class ThisThatClass
{
    public String This;
    public String That;
}

public static class Database
{
    public ThisThatClass GetThisThat()
    {
        return new ThisThatClass
        {
            This = GetThis(),
            That = GetThat()
        };
    }

    public static String GetThis()
    {
        //stuff
    }
    public static String GetThat()
    {
        //stuff
    }

}

Also, be careful with static classes. Database classes are good candidates.. as long as you usually have one database.. you get the idea.

Ultimately if you think in mathematical functions and C# doesn't suit your style, try something like Scala, Haskell, etc. They're cool too.


问:
Career guidance books for programmers
I am currently reading the recently published Being Geek by Michael "Rands" Lopp and I can't get enough of it.

Is there any other career guidance books aimed directly or indirectly at programmers that are worth reading?

答: Joel on Software blog contains tons of invaluable information which can help you to be successful programmer. A lot of the things can be easily reflected to non-software development professions. If you prefer to read books, there are several books which compile all the wisdom of Joel's articles.</p>

Title: Career guidance books for programmers


问:
Are there any problems with defining a single-source "module" in C using conditional compilation to separate the "header" from "source"
I'm writing a project that I want to keep small in the sense of being very densely-coded and a single source file.

But it's growing large enough that navigating the file is becoming tiresome, so I want to break it up into several parts. But I think I can keep the number of files small by not having a separate header file but instead using preprocessor directives to designate sections:

```<code>#ifdef EXPORT
#define blah Blah Blah Blah
void prototype(void);
#else /* IMPLEMENTATION */

void prototype(void){
     //blah
}

#endif /* IMPLEMENTATION */
</code>```

and to include the "header":

```<code>#define EXPORT
#include "module.c"
#undef EXPORT
</code>```

Are there any drawbacks to this that I'm just not seeing? Aside from the fact that I've never heard of anybody doing this?

答: Compilation dependencies are from file to file. By separating the declarations into a separate header file, you are free to change the source without causing a recompilation of other code that depends on the header.</p>


问:
Use cases, User Stories, CRC cards and Conceptual Object Model
i'm currently in the middle of watching the Lynda.com course on the foundations of Object Oriented Design and one question keeps popping into my mind.

When should I use Use cases and when should I use User Stories?

And the same question goes for the other part

When should I use CRC cards and when should I use a Conceptual Object Model?

Can I use one and "skip" the other? Are they compliments of each other?

答:
You can use any one that you are comfortable with. "Fully dressed" use cases tend to be more verbose and detailed. User stories, on the other hand, involve input that comes directly from users, and is usually a very short description which is less detailed. That said they push more of the design work into coding phase, which most developers tend to like.

The things about requirements is they change very quickly. User stories are a mainstay of agile and/or test driven development. If you have frequent communication with the stakeholder of the application, and can update the application you are working on every few days to every two weeks, and you can get immediate feedback from your users, then user stories are probably the way to go.

On the other hand, if you are working with some offshore guys who read english better than they speak it, then use cases might be the right fit for that project. Also, if you are working on a more critical app, that could blow something up if it went down the wrong path, then you might need more detailed specs up front.

As far CRC vs COM is concerned, they are both used for design. I personally like CRC and responsibility driven design myself. That said, you can do the same thing with use cases and a UML COM. In the use case + COM case, more design is done outside of coding, which is often end up changing during the coding phase.

If you are doing Test Driven Development (TDD), you will design your code through a series of tests that  guarantee your code meets it's requirements. That way, you are not writing any extra code, just what is needed to meet the requirements.

The most important thing is to get the principles off OOD down. Once you understand that, you can use any method that is comfortable for you.

Scrum is a very popular agile project process used by many teams these days, you might want to look into that:
https://en.wikipedia.org/wiki/Scrum_(software_development)

Another thing you might want to look into is BDD
https://en.wikipedia.org/wiki/Behavior-driven_development

Here is an example from the same page of a behavioral specification user story.

```
Story: Returns go to stock

In order to keep track of stock
As a store owner
I want to add items back to stock when they're returned

Scenario 1: Refunded items should be returned to stock
Given a customer previously bought a black sweater from me
And I currently have three black sweaters left in stock
When he returns the sweater for a refund
Then I should have four black sweaters in stock

Scenario 2: Replaced items should be returned to stock
Given that a customer buys a blue garment
And I have two blue garments in stock
And three black garments in stock.
When he returns the garment for a replacement in black,
Then I should have three blue garments in stock
And two black garments in stock
```

Body:
You can use any one that you are comfortable with. "Fully dressed" use cases tend to be more verbose and detailed. User stories, on the other hand, involve input that comes directly from users, and is usually a very short description which is less detailed. That said they push more of the design work into coding phase, which most developers tend to like.

The things about requirements is they change very quickly. User stories are a mainstay of agile and/or test driven development. If you have frequent communication with the stakeholder of the application, and can update the application you are working on every few days to every two weeks, and you can get immediate feedback from your users, then user stories are probably the way to go.

On the other hand, if you are working with some offshore guys who read english better than they speak it, then use cases might be the right fit for that project. Also, if you are working on a more critical app, that could blow something up if it went down the wrong path, then you might need more detailed specs up front.

As far CRC vs COM is concerned, they are both used for design. I personally like CRC and responsibility driven design myself. That said, you can do the same thing with use cases and a UML COM. In the use case + COM case, more design is done outside of coding, which is often end up changing during the coding phase.

If you are doing Test Driven Development (TDD), you will design your code through a series of tests that  guarantee your code meets it's requirements. That way, you are not writing any extra code, just what is needed to meet the requirements.

The most important thing is to get the principles off OOD down. Once you understand that, you can use any method that is comfortable for you.

Scrum is a very popular agile project process used by many teams these days, you might want to look into that:
https://en.wikipedia.org/wiki/Scrum_(software_development)

Another thing you might want to look into is BDD
https://en.wikipedia.org/wiki/Behavior-driven_development

Here is an example from the same page of a behavioral specification user story.

Story: Returns go to stock

In order to keep track of stock
As a store owner
I want to add items back to stock when they're returned

Scenario 1: Refunded items should be returned to stock
Given a customer previously bought a black sweater from me
And I currently have three black sweaters left in stock
When he returns the sweater for a refund
Then I should have four black sweaters in stock

Scenario 2: Replaced items should be returned to stock
Given that a customer buys a blue garment
And I have two blue garments in stock
And three black garments in stock.
When he returns the garment for a replacement in black,
Then I should have three blue garments in stock
And two black garments in stock


答:
When the requirements or the market specifically asks for it.

For example performance is a requirement in most financial applications because low latency is crucial. Depending on the nature of the traded instrument, optimization can go from using non-locking algorithms in a high-level language to using a low-level language and the even the extreme - implementing the order matching algorithms in hardware itself (using FPGA for example).

Other example would be some types of embedded devices. Take for example the ABS brake; firstly there is the safety, when you hit the break the car should slow down. But there is also performance, you would not want any delays when you hit the break.

Title: When is optimization not premature and therefore not evil?

Body: "Premature optimization is root of all evil" is something almost all of us have heard/read. What I am curious what kind of optimization not premature, i.e. at every stage of software development (high level design, detailed design, high level implementation, detailed implementation etc) what is extent of optimization we can consider without it crossing over to dark side.</p>


问:
Web apps for enterprise
What are the potential drawbacks of taking a 'web approach' to providing an enterprise-level app (e.g. an ERP system or an app for field staff to complete tasks) and how can these drawbacks be tackled?

What I've Thought of So Far

Offline capability - This doesn't seem to be a problem anymore, as we can use the HTML5 app cache.

Storage - HTML5 localstorage offers us a way of storing data locally whilst checking for a connection to 'sync' it to server.

The reason I ask is because I'm trying to choose the most appropriate route for meeting requirements I have, and I want to make sure I'm not missing something that would push me in the 'native' direction.

答: Keep in mind that syncing "local" content from many, potentially conflicting sources is not a trivial task. Is really offline capability important? Does the risk of being offline really matters in environment you are targeting?</p>

Title: Web apps for enterprise


答:
From a user perspective I find web apps for field staff (i.e. without permanent internet access or slow internet access) often difficult to work with.

The offline capability of such an web app has to be tested thoroughly and syncing is a difficult task.

Title: Web apps for enterprise

Body:
What are the potential drawbacks of taking a 'web approach' to providing an enterprise-level app (e.g. an ERP system or an app for field staff to complete tasks) and how can these drawbacks be tackled?

What I've Thought of So Far

Offline capability - This doesn't seem to be a problem anymore, as we can use the HTML5 app cache.

Storage - HTML5 localstorage offers us a way of storing data locally whilst checking for a connection to 'sync' it to server.

The reason I ask is because I'm trying to choose the most appropriate route for meeting requirements I have, and I want to make sure I'm not missing something that would push me in the 'native' direction.