问:
Why can't a compiler avoid importing a header file twice by its own?
New to C++! So I was reading this: http://www.learncpp.com/cpp-tutorial/110-a-first-look-at-the-preprocessor/

Header guards

Because header files can include other header files, it is possible to
  end up in the situation where a header file gets included multiple
  times.

So we make preprocessor directives to avoid this. But I'm not sure - why can't the compiler just... not import the same thing twice?

Given that header guards are optional (but apparently a good practice), it almost makes me think that there are scenarios when you do want to import something twice. Although I can't think of any such scenario at all. Any ideas?

答:
They can, as shown by new languages that do.

But a design decision was made all those years ago (when the C compiler was multiple independent stages) and now to maintain compatibility the pre-processor has to act in a certain way to make sure old code compiles as expected.

As C++ inherits the way it processes header files from C it maintained the same techniques. We are supporting a old design decision. But changing the way it works is too risky lots of code could potentially break. So now we have to teach new users of the language how to use include guards.

There are a couple of tricks with header files were you deliberately include it multiple times (this does actually provide a useful feature). Though if we redesigned the paradigm from scratch we could make this the non-default way to include files.


答:
It wouldn't be as expressive otherwise, given that they chose to maintain compatibility with C and thus keep on with a preprocessor rather than a traditional packing system.

One thing that comes to mind for me is I had a project that was an API. I had two header files x86lib.h and x86lib_internal.h. Because internal was huge, I segregated the "public" bits to x86lib.h so that users didn't have to set aside extra time for compiling.

This introduced a funny problem with dependencies though so I ended up having a flow that went like this in x86lib_internal

Set INTERNAL preprocessor define

Include x86lib.h (which was smart to act a certain way when internal was defined)

Do some stuff and introduce some things used in x86lib.h

Set AFTER preprocessor define

Include x86lib.h again (this time it'd ignore everything except for a segregated AFTER portion which depended on elements of x86lib_internal

I wouldn't say it was the best way to go about it, but it achieved what I wanted.

ContentLicense: CC BY-SA 3.0

Title: Why can't a compiler avoid importing a header file twice by its own?

Body:
New to C++! So I was reading this: http://www.learncpp.com/cpp-tutorial/110-a-first-look-at-the-preprocessor/

Header guards

Because header files can include other header files, it is possible to
  end up in the situation where a header file gets included multiple
  times.

So we make preprocessor directives to avoid this. But I'm not sure - why can't the compiler just... not import the same thing twice?

Given that header guards are optional (but apparently a good practice), it almost makes me think that there are scenarios when you do want to import something twice. Although I can't think of any such scenario at all. Any ideas?


问:
How to work out a complex, untested scope?
The scope: it is part of a big ERP system which provides specific calculations. One can think of it as a salary calculation module in a CRM. Yet this is way beyond that in both complexity and integration points. Thus, there are a lot of technological debt, but not unmaintanable one. There some some unit/integration tests written. It is testable, but it wasn't done much. It is an old part of that ERP system (3 years old), over the years it had mutated into what it is now, and there are willingness to try to complete it even if it costs some major efforts.

The problem: the scope which provides a lot of various calculations based on a specific list of objects and the outcomes are not right sometimes.

There are a lot of interesting (not so common) features about this scope:

Dynamic C# formullas (loaded to context and then included in calculcations)

A lot of reports doing the same calculations but on the database

The scope is unstable and period-intense (new features and bugfixes are shipped every month)

This scope has a lot of input parameters on all of the dependant objects. By multiplying all the possible parameter values we have 1,62*107 cases of possible outputs. Not all the parameters warry that much, but most of them do. So slicing half of that number is something around the practical possible otucomes.

This scope is eating up a lot of time and money and is far from done in its current form.

I would like to know what is the silver bullet to the most viable way to stabilize and finish the scope to the state where only minor bugs and nice to haves will occur?

First of all I was thinking, that establishing a DoD (Definition of Done) would be a relevant thing to do. Then I came up to the fact that having that huge number of outputs working is half of that - everything must also be flexible on the UI scope as well.

Will it be enoguh to establish a test plan, maybe do some bruteforcing tests? Should I consider any kind of design patterns? Validation options (like a solid third party validation service, etc.)? Will it be enough or maybe I have gaps in my modest knowledge that I should fill?

Note: the application is not what would one call monolithic. It is based on a lot of different services that can be easily extended, etc.

答:
You need to find the "guiding principles" behind the scope. ax + by = c has an infinite number of solutions, but it's still a bounded equation.  It's a matter of finding sufficient test cases where you and the customer are satisfied that ax + by = c is fulfilled.  Obviously, you don't need a test for every possible output value.  What you need are enough tests to achieve confidence that the software works as it should.

When you put a new condition into the software somewhere, a bifurcation is created (a point where the logic branches off in two different directions). To achieve adequate code coverage, create a test case for each branch in every bifurcation, and tests to cover any remaining edge cases (such as critical number precision boundaries).  See here for an example of how this works.

Have the customer agree to a set of test cases that, once fulfilled, will allow you to declare success. Then your scope will be bounded by your test cases.


问:
method to allow me ability to freely modify my classes, but make them immutable to others?
I am creating the model part of an MVC architecture.  My one class will provide all the accesses to allow one to fetch system state.  I want most of this state to be immutable as it shouldn't be changed and I don't want anyone accessing my model to be able to break the state by making a foolish change.

However, The objects representing the state have a bit of a tree structure.  object A contains a set of B which contains a set of objects C which contain a set of Object D etc etc.  Due to the nature of how I have to fetch the data I can't build the structure from the bottom up; the bottom most 'chid object' will be generated after all the others are completed.  This is making it rather inconvenient to build the immutable objects in an intuitive manner, I want to add a set as my last phase of building but I can't if I already built immutable objects.

I know of three approaches, but not sure I like any.  One is a builder patern, but keeping track of all the builders for a complicated structure until I'm done seems like it could be very ugly.  Second is to have a sort of 'clone' method that builds a new immutable object by cloning the old but adding a newly provided set to it (what is this pattern called again?).  This gets ugly when I try adding the child most element to the set since I need to pretty much rebuild the entire tree with 'new' immutable objects to add one value.

The third, and so far easiest, solution is to just get rid of the tree structure and make them make a call to my model to get the data they want.  so instead of calling myObject.getChildren they call Model.getObjectChildren (myObject).  This one will work, but darn it I wanted the pretty tree structure for my immutable state.

Is there some other way to conveniently build up my state in a manner where my Model can freely modify the state while being built; but still have in immutable when it's finally published to the Controller?

ps. I'm running in java if that matters.

答:
Sure it is possible. Some even coined a term for it: Popsicle immutable. The general idea is to have a mutable data structure on the inside, plus a flag that tells you whether the object is currently mutable or not. Note that in many cases (including this one), you only want the flag to go from "mutable" to "immutable" once, and never the other way. Then you simply add

Methods to mutate the state, which check the flag and raise an error if the object is not mutable.

A method to flip the flag and thus turn the object immutable. This is also a good opportunity to perform some optimizations (e.g. trim excess capacity of collections or build an index) as you now know the state won't change.

This does add some complexity to the implementation, and some potential for errors (forgetting to flip the switch, trying to mutate an immutable object). But that's inherent to this ability.
To regain a bit of simplicity, you can disallow operations not related to building the state while the object is mutable. This means you have fewer cases to support/worry about.

ContentLicense: CC BY-SA 4.0


问:
How does understanding computer architecture help a programmer?
It is said, by Mike P. Wittie, in the course curriculum of computer architecture that,

Students need to understand computer architecture in order to
  structure a program so that it runs more efficiently on a real machine

I'm asking to more experienced programmers or professionals that have a background in this topic:

How does learning computer architecture help you? What aspect benefits you most? How did learning computer architecture changed your programming structuring?

答:
How does understanding physics help people drive a car?

They understand phenomena like brake fade, and will compensate for it.

They understand center of gravity and how tires grip the road.

They understand hydroplaning, and how to avoid it.

They know how to best enter and exit a curve.

They are far less likely to tailgate.

And so on.  You can drive a car without knowing much about physics, but understanding physics does make you a better driver.

Two example of how understanding computer architecture can affect the way you code:

branch prediction

Cache size and access patterns


问:
What are the benefits of using Dependency Injection and IoC Containers?
I'm planning to do a talk on Dependency Injection and IoC Containers, and I'm looking for some good arguments for using it.

What are the most important benefits of using this technique, and these tools?

答:
Most important, for me, is making it easy to follow the Single Responsibility Principle.

DI/IoC makes it simple for me to manage dependencies between objects.  In turn, that makes it easier for me to break coherent functionality off into it's own contract (interface).  As a result, my code has been far more modularized since I learned of DI/IoC.

Another result of this is that I can much more easily see my way through to a design that supports the Open-Closed Principle. This is one of the most confidence inspiring techniques (second only to automated testing). I doubt I could espouse the virtues of Open-Closed Principle enough.

DI/IoC is one of the few things in my programming career that has been a "game changer."  There is a huge gap in quality between code I wrote before & after learning DI/IoC.  Let me emphasize that some more.  HUGE improvement in code quality.

ContentLicense: CC BY-SA 2.5


答:
I'd go so far as to say that anyone who doesn't understand computer organization is doomed to be a lousy programmer. You'll know:

how to organize your data structures and algorithms to be more cache efficient

how a function call works, and the implications for calling convention

the segments of memory, and their implications for variable declarations

how to read assembly, and thus interpret the output of a compiler

the effects of instruction-level parallelism and out-of-order instruction scheduling, and what that means for branching

Basically, you'll learn how a computer actually works, and thus you'll be able to map your code to it more effectively.

Title: How does understanding computer architecture help a programmer?

Body:
It is said, by Mike P. Wittie, in the course curriculum of computer architecture that,

Students need to understand computer architecture in order to
  structure a program so that it runs more efficiently on a real machine

I'm asking to more experienced programmers or professionals that have a background in this topic:

How does learning computer architecture help you? What aspect benefits you most? How did learning computer architecture changed your programming structuring?


答:
It is basically the same reason as for understanding C and pointers or maybe even algorithms; the only difference is that if you know computer architecture you really understand pointers (actually pointers seem very trivial after knowing computer architecture).

I cannot say about myself that I am an experienced programmer but a (actually the) book on computer architecture I read was for me the most interesting book I have read, related to computers and programming. By understanding computer architecture you basically understand how everything is linked together, how a computer works and how come a program really does work; you see the big picture. Without computer architecture you cannot truly understand:

memory management: heap, stack, virtual memory, memory hierarchy and the so spoken about pointers (why is there a stack overflow, why is recursion not so good etc.)

assembly programming (if you want to program embedded)

compilers and interpreters (if you want to understand optimizations and when it is useless to optimize on code because it is already being made by the compiler)

linkers (dynamically linked libraries)

operating systems (if you want to read Linux kernel code)

the list can go on...

From my really subjective point of view it is by far more interesting and maybe even more useful than knowing algorithms.


问:
How do I share different files in a git repo with different people?
In a single directory with a Git root folder, I have a bunch of files. I am working on one of those files, X.py, with my friend Alice. The other files I am working on with other people. I want Alice (and everyone else) to have access to X.py. I want Alice to only have access to X.py though.

How can I achieve this with Git? Is there a way I can split a directory into two repos? That sounds rather cumbersome. Maybe I could add a remote repo that Alice can access containing X.py?

答:
If it was SVN you could easily handle this with permissions, but most DCVSes don't have that fine-grained permissions as that is completely unworkable with everyone having a copy of the repository.

Probably the best way to handle this would be to have alice fork the repository and submit pull requests -- you could then make sure she is just working in X.py and not anything else.

If you don't want her reading anything else then you'd need to work with subrepos. But as a great man once said "when you've got a problem and you say 'hey, I'll use subrepositories' then you've got two problems."


答:
You make a branch X and delete everything except X.py and the push the branch to a central repository. If you want to share Y.py also, you add it to your branch and you merge this branch with X from the central repository.

You don't push to the central repository anything that you don't want to be seen by others.

Title: How do I share different files in a git repo with different people?

Body:
In a single directory with a Git root folder, I have a bunch of files. I am working on one of those files, X.py, with my friend Alice. The other files I am working on with other people. I want Alice (and everyone else) to have access to X.py. I want Alice to only have access to X.py though.

How can I achieve this with Git? Is there a way I can split a directory into two repos? That sounds rather cumbersome. Maybe I could add a remote repo that Alice can access containing X.py?


问:
Should a Presenter or a View sort data in an MVP application?
A typical MVP application might have a View method such as:

void setDisplayItems(List<DisplayItem> items);
```

<p>Should the Presenter sort the list of items or is that a task for the view?</p>

<p>I feel this might be a task for the View, for two reasons:</p>

<ol>
<li><p>If it were a task for the Presenter, then methods such as:</p>

<pre class="lang-java prettyprint-override"><code>void addDisplayItem(DisplayItem item);
</code>```

won't work, as the Presenter can't insert the item at the correct place.

The View can offer different sorting options, based on user selections.

However, I also can't shake the feeling that this is too much "thinking" for the View to be doing. Can anyone comment on the correct choice for MVP?

I should note that I typically produce Swing-based Java applications (in case that allows for more specific answers).

答:
The correct answer is (drum roll please) it depends. Are you using a UI framework that supports sorting natively? Are you sorting over a large sequence that would be best performed as a database operation? Does your UI framework support sorting through the database (e.g. IQueryable support in many .NET UI toolkits).

There are multiple approaches to MVP. You have the Passive View in which the Presenter has deep knowledge of the view via an Interface, there's the supervising controller in which the presenter handles complex view logic and the rest is left to the view (so again if your view objects can handle sorting, let them do it if it's not an expensive operation), finally is the Presentation Model which is pretty much equivalent to MVVM in which the Presenter exposes behavior as properties and the View reacts to those properties.


问:
Should Repositories return IQueryable?
I have been seeing a lot of projects that have repositories that return instances of IQueryable. This allows additional filters and sorting can be performed on the IQueryable by other code, which translates to different SQL being generated. I am curious where this pattern came from and whether it is a good idea.

My biggest concern is that an IQueryable is a promise to hit the database some time later, when it is enumerated. This means that an error would be thrown outside of the repository. This could mean an Entity Framework exception is thrown in a different layer of the application.

I have also run into issues with Multiple Active Result Sets (MARS) in the past (especially when using transactions) and this approach sounds like it would lead to this happening more often.

I have always called AsEnumerable or ToArray at the end of each of my LINQ expressions to make sure the database is hit before leaving the repository code.

I am wondering if returning IQueryable could be useful as a building block for a data layer. I have seen some pretty extravagant code with one repository calling another repository to build an even bigger IQueryable.

答:
There really is only one legitimate answer: it depends on how the repository is to be used.

At one extreme your repository is a very thin wrapper around a DBContext so you can inject a veneer of testability around a database-driven app. There really is no real world expectation that this might be used in a disconnected manner without a LINQ friendly DB behind it because your CRUD app ain't ever gonna need that. Then sure, why not use IQueryable? I would probably prefer IEnumarable as you get most of the benefits [read: delayed execution] and it don't feel as dirty.

Unless you are sitting at that extreme I would try hard to take advantage of the spirit of the repository pattern and return appropriate materialized collections that don't have an implication of an underlying database connection.

Title: Should Repositories return IQueryable?


问:
Is this technique a design pattern? If so, what's it called?
I'll use C# as an example, but it should apply globally. Say I have a string value that should be one of a few constants, but I also want the client to set which string value to use so:

```<code>private int foo;
private string bar;

public int Foo {
    get { return foo; }
    set
    {
        foo = value;
        bar = getStringValueFromDatabase(value);
    }
}

public string Bar { get { return bar; } }
</code>```

I use this technique quite a lot and want to know if it's considered as any formal concept.

答:
Say I have a string value that should be one of a few constants, but I also want the client to set which string value to use so:

A constant that is changed? Eh?

What you're doing is changing a property which is backed by a data store. It's not a constant. It might be a configuration variable.

The Patterns name might be Facade or Decorator or somesuch similar. If my code works, and is maintainable, I don't bother myself with the pattern names.

My comment on your code is you should have another layer of indirection - a function that manages the data storage/restoration. That will abstract it away from the database call.


答:
Realistically, you've got three alternatives if you want deferred execution:

Do it this way - expose an IQueryable.

Implement a structure that exposes specific methods for specific filters or "questions".  (GetCustomersInAlaskaWithChildren, below)

Implement a structure that exposes a strongly-typed filter/sort/page API and builds the IQueryable internally.

I prefer the third (though I've helped colleagues implement the second as well), but there's obviously some setup and maintenance involved.  (T4 to the rescue!)

Edit: To clear up the confusion surrounding what I'm talking about, consider the following example stolen from IQueryable Can Kill Your Dog, Steal Your Wife, Kill Your Will to Live, etc.

In the scenario where you would expose something like this:

```<code>public class CustomerRepo : IRepo
{
     private DataContext ct;
     public Customer GetCustomerById(int id) { ... }
     public Customer[] GetCustomersInAlaskaWithChildren() { ... }
}
</code>```

the API I'm talking about would let you expose a method that would let you express GetCustomersInAlaskaWithChildren (or any other combination of criteria) in a flexible manner, and the repo would execute it as an IQueryable and return the results to you.  But the important thing is that it runs inside the repository layer, so it still takes advantage of deferred execution.  Once you get the results back, you can still LINQ-to-objects on it to your heart's content.

Another advantage of an approach like this is that, because they're POCO classes, this repository can live behind a web or WCF service; it can be exposed to AJAX or other callers that don't know the first thing about LINQ.

Title: Should Repositories return IQueryable?

Body:
I have been seeing a lot of projects that have repositories that return instances of IQueryable. This allows additional filters and sorting can be performed on the IQueryable by other code, which translates to different SQL being generated. I am curious where this pattern came from and whether it is a good idea.

My biggest concern is that an IQueryable is a promise to hit the database some time later, when it is enumerated. This means that an error would be thrown outside of the repository. This could mean an Entity Framework exception is thrown in a different layer of the application.

I have also run into issues with Multiple Active Result Sets (MARS) in the past (especially when using transactions) and this approach sounds like it would lead to this happening more often.

I have always called AsEnumerable or ToArray at the end of each of my LINQ expressions to make sure the database is hit before leaving the repository code.

I am wondering if returning IQueryable could be useful as a building block for a data layer. I have seen some pretty extravagant code with one repository calling another repository to build an even bigger IQueryable.

Body:
Realistically, you've got three alternatives if you want deferred execution:

Do it this way - expose an IQueryable.

Implement a structure that exposes specific methods for specific filters or "questions".  (GetCustomersInAlaskaWithChildren, below)

Implement a structure that exposes a strongly-typed filter/sort/page API and builds the IQueryable internally.

I prefer the third (though I've helped colleagues implement the second as well), but there's obviously some setup and maintenance involved.  (T4 to the rescue!)

Edit: To clear up the confusion surrounding what I'm talking about, consider the following example stolen from IQueryable Can Kill Your Dog, Steal Your Wife, Kill Your Will to Live, etc.

In the scenario where you would expose something like this:

public class CustomerRepo : IRepo
{
     private DataContext ct;
     public Customer GetCustomerById(int id) { ... }
     public Customer[] GetCustomersInAlaskaWithChildren() { ... }
}

the API I'm talking about would let you expose a method that would let you express GetCustomersInAlaskaWithChildren (or any other combination of criteria) in a flexible manner, and the repo would execute it as an IQueryable and return the results to you.  But the important thing is that it runs inside the repository layer, so it still takes advantage of deferred execution.  Once you get the results back, you can still LINQ-to-objects on it to your heart's content.

Another advantage of an approach like this is that, because they're POCO classes, this repository can live behind a web or WCF service; it can be exposed to AJAX or other callers that don't know the first thing about LINQ.


答:
I think that exposing the IQueryable on your repository is perfectly acceptable during the initial phases of development. There are UI tools that can work with IQueryable directly and handle sorting, filtering, grouping, etc.

That being said, I think that just exposing crud on the repository and calling it a day is irresponsible. Having useful operations and query helpers for common queries allows you to centralize the logic for a query while also exposing a smaller interface surface to consumers of the repository.

For the first few iterations of a project, exposing IQueryable publicly on the repository allows for quicker growth. Before the first full release, I'd recommend making the query operation private or protected and bringing the wild queries under one roof.

Title: Should Repositories return IQueryable?


答:
Returning IQueryable will definitely afford more flexibility to the consumers of the repository. It puts the responsibility of narrowing results off to the client, which naturally can both be a benefit and a crutch.

On the good side, you won't need to be creating tons of repository methods (at least on this layer) — GetAllActiveItems, GetAllNonActiveItems, etc — to get the data you want. Depending on your preference, again, this could be good or bad. You will (/should) need to define behavioral contracts which your implementations adhere to, but where that goes is up to you.

So you could put the gritty retrieval logic outside the repository and let it be used however the user wants. So exposing IQueryable gives the most flexibility and allows for efficient querying as opposed to in-memory filtering, etc, and could reduce the need for making a ton of specific data fetching methods.

On the other hand, now you have given your users a shotgun. They can do things which you may not have intended (overusing .include(), doing heavy heavy queries and doing in-memory filtering in their respective implementations, etc), which would basically side-step the layering and behavioral controls because you have given full access.

So depending on the team, their experience, the size of the app, the overall layering and architecture … it depends :-\

Title: Should Repositories return IQueryable?


答:
Update 2018: How many Software Developers does it take to change a Lightbulb??? Who Cares!? That's a Hardware Problem!

Generally NO, You don't need to know computer architecture to be a good programmer, That's more in the EE realm IMO.. unless of course you're in embedded systems development, but in that case you're married to the chip and programming right on it, so you'll need to know the architecture of THAT "computer" (and even then it may not matter), but having a general architectural understanding of how computers work wont be good for much else than Water-hole discussions.

I would say it's even less important these days at the rate hardware is declining in price and performance is improving / increasing and how quickly the technologies are changing and languages are evolving. Data structures and design patterns don't really have much to do with physical hardware architecture as far as I know.

Generally Programmers come from a computer science background, in which case, they've more than likely taken computer architecture classes, but now-a-days, Operating Systems are going virtual, disk space is shared, memory is scaleable, etc.. etc..

I have been able to make a great career in programming(10+ years) and I have very little educational knowledge of computer architecture, mostly because... I was an Art major!!!

Update: Just to be fair, MY "little educational knowledge" came from my CPU Sci. Minor. and still, I've never needed to use anything I've learned from my Assembly classes or my Computer Architecture classes in my "Programming" career.

Even now as I play around with some Mesh Networking Idea's implementing the ZigBee spec, I've found that using the products and tools available (XBee), I'm able to program in Python and plop the code right on chip (SoC) and do some really neat stuff with it.. ALL without having to worry about anything to do with actual architecture of the chips, etc.. there are definitely hardware limitations to be cognitive of because of the chip size and the intended low price target.. but even THAT will become less in the upcoming years. So I stand by my "Generally NO" answer


问:
For a web-application, should I programmatically create the database tables or provide a schema file and instructions?
The few LAMP web applications I have installed require me to create the necessary database tables myself, with a schema file, instead of doing it automatically with a script or setup page. Is this a normal and accepted practice? In my own hobby projects I find it much easier and hassle free to just create the tables in code. As the programmer I can make sure it's done correctly, and as the user I don't care - just make it work!

However, I have very limited knowledge and experience in the world of web applications and database management, so I don't know the pros and cons of either approach.

If I were to make my LAMP web applications available for download and installation by others, should I continue to do table creation in code or follow the example of the other applications I have installed, and just provide a schema file with installation instructions?

答: I'd say it's a matter of preference. Personally, I'd rather build the tables myself, in my code. SQL and Entity Framework support both methods.</p>

OwnerDisplayName: DrTwox

OwnerDisplayName: BilldrBot


答:
Are you planning to distribute it, or you're the one who's going to install it? In the former case, go with a setup script (or schema creation on the first run). In the latter case, a schema file would do. The upside of manual schema creation is that you don't have to give the database user that your PHP files are connecting as any schema editing rights. The principle of least privilege and all that.

The upside of automatic schema creation is, obviously, convenience for your users.

Title: For a web-application, should I programmatically create the database tables or provide a schema file and instructions?

Body:
The few LAMP web applications I have installed require me to create the necessary database tables myself, with a schema file, instead of doing it automatically with a script or setup page. Is this a normal and accepted practice? In my own hobby projects I find it much easier and hassle free to just create the tables in code. As the programmer I can make sure it's done correctly, and as the user I don't care - just make it work!

However, I have very limited knowledge and experience in the world of web applications and database management, so I don't know the pros and cons of either approach.

If I were to make my LAMP web applications available for download and installation by others, should I continue to do table creation in code or follow the example of the other applications I have installed, and just provide a schema file with installation instructions?

OwnerDisplayName: Seva Alekseyev


问:
Application Design with a Shared Database
I have a generic design question that relates to enterprise applications but I think that software developers from other types of projects may have experience with this as well.

The scenario is following - you have a database and you are about to write an application that provides access to its data. The database also contains data about user accounts, their roles and authorization. The problem is that other processes are accessing the database as well and any one of these processes may change any data at any moment (e.g., delete a user).

In other projects I used to have exclusive access to the database and so I usually wrapped the database with a layer that exposed the related domain functionality in a SOA-style. This wrapper had a rich domain model and could utilize caching of the database data because it was aware of any potential change (by intercepting domain events, CQRS) that could modify it.

With a shared database I don't know how to approach the design. Imagine this situation:

HTTP request arrives to you application and you are to authenticate this request.

You query the database for the claimed user account and its login data. If the supplied credential matches then a session is established (e.g., in ASP.NET a secure cookie is sent to the client).

But then a subsequent request from that user arrives. The problem is that instead of doing something useful at this moment, it seems like that one needs to check whether the associated user account still exists in the database. Any information can stop being valid at any point because some other process may alter the database.

This is of course a more generic problem. The data may not only change between requests but even between two separate database calls. The easy answer here is to use locks/transactions. But the truth is that this would require huge changes to the database schema. For example, every user account would require an additional column 'InUse' - but that is just a very simple scenario. I can imagine that completely new tables would need to be created. Also, the transactions would typically span across many HTTP requests and thus render the database useless for long periods of time for other users.

I have read the PoEAA book and I am well aware of various synchronization techniques. What I am looking for here is some generic technique or methodology how to achieve the so called "good-enough-design" that does not require changes to the database schema and can handle the mentioned situations where anything can change between two requests.

Right now I think I will go with the following approach - I will authenticate the user. Then I will not perform any subsequent checks to ensure that he was not deleted in the meanwhile. But when I perform some operation that relies on the existence of the user I will receive an exception and deduce that the user must have been deleted. The response will then be some kind of error message or error page.

I am sure this is a common problem that many of you must have encountered. I will appreciate any solution or even a reference to some book or paper. Thanks.

答:
If this is for an enterprise, I am surprised that you don't have a single sign on facility (or several) to use. That would be my suggestion. In my experience, it is not common to have applications sharing a database, even for authentication. The thing that gets me here is that it is another enterprise system reinventing the wheel with user authentication, another set of passwords to remember, etc.

Authentication is typically provided either by a web service call (SOAP), or using SSO, and sometimes using a message queue.

By having a separate database you are enforcing a separation of concerns, and you can keep your data to yourself. My suggestion would be if at all possible (which it's not always due to enterprise bureaucracy) to expose the user account management as a service.

If thats not possible and you do have to work with the constraint of the existing database, I would suggest your domain model includes the authentication tables.

In terms of dealing with data that has been changed, the easiest solution will probably to be maintain a session variable such as LoggedInDateTime that caches the value of DateTime.Now (assuming .NET and SQL Server, adjust as necessary), and have an update stamp on your user rows that have a trigger set to automatically update the stamp on an update. Then, for each request check whether the UpdateStamp has been updated since you logged in.

In terms of checking if the user is deleted, per request check that the user row still exists (if using physical deletion) otherwise check for a DeleteStamp or IsDeleted value.


答:
Most web applications I have installed have a setup "wizard" that runs when I first navigate to where the site is hosted and it will run the scripts using some nice interface, which is a very nice feature.. However, the scripts are all there for me to see usually and i have also bypassed the setup wizard all together and just run the sql scripts myself.

This also brings up the point of database requirements.. is a certain DB required? if so, then you might as well create the installation "wizard" and automatically run the scripts since there is no DB flexibility, however, if i can "choose" which type of persistent storage mechanism to use (Model 1st type design), then it'd be pretty dang nice of you to make an installer which can handle that case (thats probably a product in itself!) but I would NOT expect for an installer to be provided in that scenario. But in that case you're dev'ing for a different audience than the typical "i want to install a blog so I can blog" type of user..


问:
Converting 'char*' to 'char'. Am I passing this array correctly?
So my CS professor gave the whole class a simple assignment. "Write a recursive function that will swap the order of a section in an array of chars." I thought to myself, "Easy. I'll finish this up in about 5 minutes and I'll get to work on my Trig homework before I leave."

This is not what happened.
An hour later, the professor and I are both wondering what on Earth is going wrong.

```<code>#include<iostream>
void swap(char charList, int start, int stop);

int main()
{
    char myList[] = {'a', 'b', 'c', 'D', 'E', 'F', 'g', 'h', 'i', 'j'};
    int size = 9;
    printCharList(myList, size);
    swap(myList, 3, 5);           //<--- It doesn't seem to like this call
    return 0;
}

void swap(char charList[], int start, int stop)
{
    char temp
    if(start < stop)
    {
        temp = charList[stop];
        charList[start] = charList[stop];
        charList[stop] = temp;
        swap(charList, start+1, stop-1);
    }
    else
        std::cout << "start > stop\n";
}
</code>```

The code is stupidly simple, so you can imagine my confusion when it refused to compile.
It keeps throwing up an error message suggesting that I am trying to convert a pointer to a char, but I'm almost positive that I've done no such thing.
I'm sure the problem is right in front of my face, but I have had no luck in finding it.
Could you help a guy out?

答:
In the header, you declare the signature as

```<code>void swap(char charList, int start, int stop);
</code>```

you probably want to declare it as

```<code>void swap(char charList[], int start, int stop);
</code>```

Body:
In the header, you declare the signature as

void swap(char charList, int start, int stop);

you probably want to declare it as

void swap(char charList[], int start, int stop);


问:
Programming methodology - best way to level up?
Ok, so I'm at a point where I think I have my basics down. I'd like to think I'm just starting to become an intermediate dev. So, how do I level up here?

It feels like I have reached my threshold, but I really want to break it, and move ahead, do more crazy stuff (for context, I'm talking about programming).

So, what would be the best strategy to achieve this?

I'm working on multiple side projects of my own (I'm a student).

How should I approach learning to program from here? What I currently do is, if I'm unable to understand a how to solve a problem, Google or SO it, find an answer, modify it as required and use it. It this the correct way to go? Is there a better way?

How do I go about learning about new concepts, new classes etc. in an organized manner, one that doesn't overload my brain?

I know it's practice, practice, practice, but the question is what to practice? There's just so much and only so much I can do.

Share your knowledge!

PS: My primary field is Java, so I tagged it for some specific advice as well.

EDIT: As I said, I'm a student, NOT in CSE major, so I don't have any clients or "requirements" per se. So what's best strategies to develop my own projects? I have done a few projects and learned a lot from them, but is there a guide about standards or conventions about projects? How do I structure my projects? How do i build them efficiently?

答:
"I know it's practice, practice, practice, but the question is what to practice? There's just so much and only so much I can do."

The only answer is to do things you're passionate about, or things that you want to exist but don't.

Like games? Make games. Like web stuff? Build a website/service, use some new frameworks, tinker with third-party APIs. Like tinkering? Get an arduino/RasperryPI/etc and hack away.

You will never learn everything. Just do things you're interested in and you both won't get bored and will continue learning new things.

Also, why is your primary field Java? There's a wonderful world of languages out there, and learning some others will give you a good understanding of benefits, drawbacks, language features and lots of other fun things.


答:
It's perfectly valid OO technique, though I think it can be made a bit more efficient if the lazy-loading (which is what this effectively is) is deferred until the Bar value is actually needed.

```<code>private int foo;
private string bar;

public int Foo
{
    get { return foo; }
    set
    {
        foo = value;
        bar = null;
    }
}

public string Bar
{
    get
    {
        if(bar == null)
            bar = getStringValueFromDatabase(Foo);
        return bar;
    }
}
</code>```

Edit: I think that's an improvement on the original code though as others have observed, even better would be to abstract the data access into a separate layer.

Title: Is this technique a design pattern? If so, what's it called?

Body:
I'll use C# as an example, but it should apply globally. Say I have a string value that should be one of a few constants, but I also want the client to set which string value to use so:

```<code>private int foo;
private string bar;

public int Foo {
    get { return foo; }
    set
    {
        foo = value;
        bar = getStringValueFromDatabase(value);
    }
}

public string Bar { get { return bar; } }
</code>```

I use this technique quite a lot and want to know if it's considered as any formal concept.

Body:
It's perfectly valid OO technique, though I think it can be made a bit more efficient if the lazy-loading (which is what this effectively is) is deferred until the Bar value is actually needed.

private int foo;
private string bar;

public int Foo
{
    get { return foo; }
    set
    {
        foo = value;
        bar = null;
    }
}

public string Bar
{
    get
    {
        if(bar == null)
            bar = getStringValueFromDatabase(Foo);
        return bar;
    }
}

Edit: I think that's an improvement on the original code though as others have observed, even better would be to abstract the data access into a separate layer.


问:
Why many web developers use java programming language for web development?
I know a lot of programming languages that can be used to web development like PHP, ASP, etc. but why many web developers use java programming language for web development?</p>

答:
Java is "safe" for corporate managers.  I think it is less often you see startups without corporate managers choosing Java (sorry, no stats off hand), and more often you will see Java or .NET in large corporate environments.

Safe means several things, including the ability to hire talent.  Or that the other stuff is based on Java, so we need to stay with it.  Or that is what we learned in school.  Many reasons.

Few managers will get fired for choosing Java or .NET, but they may get fired if they choose Python or Ruby and it doesn't "work out".

I heard a developer I very much respect tell me once:

"Java allows mediocre developers to crank out mediocre software at a predictable pace"  -- And it makes sense!  There are endless docs to read, books to digest, and springs and beans to play with.

(Note: observing from the sidelines here.  I almost adopted Java for web-development once... I was that fed up with PHP.  We ended up standardizing on Python 3, and could not be happier at this point.  But there was no "manual" to read -- we built our own infrastructure)


问:
what is a efficient way to find repeating decimal
I am trying to find a efficient algorithm in Java to find the repeating decimal part of two integers a and b where a/b.

eg. 5/7 = 0.714258 714258....

I currently only know of the long division method.

答:
Long division? :/

Turn the result into a string, and then apply this algorithm to it.  Use BigDecimal if your string isn't long enough with ordinary types.


答:
You should go with a setup script, which the installer can customize.

There is much more to defining a database than you can easily include in the application code. As a system scales up data placement, index options etc. become more important. Its nice to have an install script where you have fine control over all the database parameters, rather than rely on an automatic initialization which picks up all the defaults.


答:
The right way to approach leveling up is know by another name, deliberate practice. To do deliberate practice you pick a topic and stick with it for at least 2 months. So if you pick sockets and network protocols then you should stick with it for at least 2 months and write some non-trivial code that makes use of sockets and network protocols. The great thing about this approach is that not only do you gain a deeper understanding of whatever topic you're exploring but you also get to explore some other topics along the way. You can't really do sockets and network protocols without learning about threads and parsing techniques so you end up killing several birds with one stone. This is true of any topic in CS. You can't really learn it well without exploring some nearby concepts and topics as well. So even though it feels like there is all sorts of stuff you could be learning at this instant the slow, steady, and deliberate approach always wins in the end.

In summary, pick a topic and stick with it for at least 2 months. Spend at least 1 hour a day learning and implementing something that makes use of whatever topic you're exploring and you'll be an expert in several domains in no time.

Title: Programming methodology - best way to level up?

Body:
Ok, so I'm at a point where I think I have my basics down. I'd like to think I'm just starting to become an intermediate dev. So, how do I level up here?

It feels like I have reached my threshold, but I really want to break it, and move ahead, do more crazy stuff (for context, I'm talking about programming).

So, what would be the best strategy to achieve this?

I'm working on multiple side projects of my own (I'm a student).

How should I approach learning to program from here? What I currently do is, if I'm unable to understand a how to solve a problem, Google or SO it, find an answer, modify it as required and use it. It this the correct way to go? Is there a better way?

How do I go about learning about new concepts, new classes etc. in an organized manner, one that doesn't overload my brain?

I know it's practice, practice, practice, but the question is what to practice? There's just so much and only so much I can do.

Share your knowledge!

PS: My primary field is Java, so I tagged it for some specific advice as well.

EDIT: As I said, I'm a student, NOT in CSE major, so I don't have any clients or "requirements" per se. So what's best strategies to develop my own projects? I have done a few projects and learned a lot from them, but is there a guide about standards or conventions about projects? How do I structure my projects? How do i build them efficiently?


答:
I believe that there are two general approaches here, you can essentially "brute force" look for the longest repeating string, or you can solve it as a problem of number theory.

It's been a long time since I ran across this problem, but a special case (1 / n) is problem #26 on Project Euler, so you may be able to find more information by searching for efficient solutions for that specific name.  One search leads us to Eli Bendersky's website, where he explains his solution.  Here's some of the theory from Mathworld's Decimal Expansions page:

Any nonregular fraction m/n is periodic, and has a period lambda(n) independent of m, which is at most n-1  digits long. If n is relatively prime to 10, then the period lambda(n) of m/n is a divisor of phi(n) and has at most phi(n) digits, where phi is the totient function. It turns out that lambda(n) is the multiplicative order of 10 (mod n) (Glaisher 1878, Lehmer 1941). The number of digits in the repeating portion of the decimal expansion of a rational number can also be found directly from the multiplicative order of its denominator.

My number theory is a bit rusty at the moment, so the best I can do is point you in that direction.

Title: what is a efficient way to find repeating decimal

Body:
I am trying to find a efficient algorithm in Java to find the repeating decimal part of two integers a and b where a/b.

eg. 5/7 = 0.714258 714258....

I currently only know of the long division method.


答:
Using scripts to create database used to be the case some time back. It was also the time the database administration is a full time profession.  Our application used to ship with oracle sql scripts to manage db creation.  Some time DBAs then gave feedback about missing index etc, for performance.

Today we ship with pre-built database (postgres) and customers love it.   This also reduced number of  support calls related to database administration that we had to support.  Number of  applications I recently tried also came with transparent databases.

Summary : Yes, database creation should be managed by you application or installer.  This is especially true if the target customers are very small companies.


答: If possible, it's safer to pass foo in the constructor, and make it readonly/final.  Then you've created an object with dependencies.  when you call the Bar getter, then use the captured state to compute the value if you want to do it lazily.</p>


答:
Let n < d, and you're trying to figure out the repeating part of n/d. Let p be the number of digits in the repeating part: then n/d = R * 10^(-p) + R * 10^(-2p) + ... = R * ((10^-p)^1 + (10^-p)^2 + ...). The bracketed part is a geometric series, equal to 1/(10^p - 1).

So n / d = R / (10^p - 1). Rearrange to get R = n * (10^p - 1) / d. To find R, loop p from 1 to infinity, and stop as soon as d evenly divides n * (10^p - 1).

Here's an implementation in Python:

```<code>def f(n, d):
    x = n * 9
    z = x
    k = 1
    while z % d:
        z = z * 10 + x
        k += 1
    return k, z / d
</code>```

(k keeps track of the length of the repeating sequence, so you can distinguish between 1/9 and 1/99, for example)

Note that this implementation (ironically) loops forever if the decimal expansion is finite, but terminates if it's infinite! You can check for this case, though, because n/d will only have a finite decimal representation if all the prime factors of d that aren't 2 or 5 are also present in n.

Body:
Let n < d, and you're trying to figure out the repeating part of n/d. Let p be the number of digits in the repeating part: then n/d = R * 10^(-p) + R * 10^(-2p) + ... = R * ((10^-p)^1 + (10^-p)^2 + ...). The bracketed part is a geometric series, equal to 1/(10^p - 1).

So n / d = R / (10^p - 1). Rearrange to get R = n * (10^p - 1) / d. To find R, loop p from 1 to infinity, and stop as soon as d evenly divides n * (10^p - 1).

Here's an implementation in Python:

def f(n, d):
    x = n * 9
    z = x
    k = 1
    while z % d:
        z = z * 10 + x
        k += 1
    return k, z / d

(k keeps track of the length of the repeating sequence, so you can distinguish between 1/9 and 1/99, for example)

Note that this implementation (ironically) loops forever if the decimal expansion is finite, but terminates if it's infinite! You can check for this case, though, because n/d will only have a finite decimal representation if all the prime factors of d that aren't 2 or 5 are also present in n.


答:
YAGNI applies there.

If it's for your use only, use whatever works best for you.

If it's for distribution, then you need to provide at least instructions on how to use the schema file, or a build script. The easiest it will be, the more people will be interested in going with your application.


答:
These two points

new features and bugfixes are shipped every month

and

I would like to know what is the most viable way to stabilize and finish the scope

are in conflict. If there are new features every month, there are new requirements every months, someone seems to be in need of that features, someone else is programming them and that is new software - which will typically contain some bugs at the beginning and will get better by time through tests and user feedback. Moreover, there is no "end" of this process, it is "living software" where changes seem to be part of the normal process.

It would be really helpful if you had lost some word how the current development of that "scope" looks like, is there one dev, a team, are there many people requesting new features etc.?

Best advice I can give you is to try separarating the parts where the requirements are stable from the parts with the unstable requirements. The parts where the requirements are stable should be made bullet-proof and well tested by automated regression tests. Those parts can be somewhat "finished".

The other parts - well I don't know who is defining that dynamic formula part, but assumed that is the part which changes every month. It is important to give the ones who are defining those formulas immediate feedback if they have done it right. Perhaps you have to provide some kind of "interactive testing environment" for those people, so they can try out the formulas before bringing them into the real system.

For example, think of a spreadsheet software like MS Excel - the program itself is a (more or less) stable software, (hopefully) well tested, where you don't get new versions every month. It can be used as platform for formulas and reports, and the user of those formulas and reports can enter those formulas by himself, test them, gets immediate feedback and change those things 10 times a day if he likes. If you can create a comparable "separation of concerns" in you "scope" (whatever that means), then you will be a big step further.

Title: How to work out a complex, untested scope?

Body:
The scope: it is part of a big ERP system which provides specific calculations. One can think of it as a salary calculation module in a CRM. Yet this is way beyond that in both complexity and integration points. Thus, there are a lot of technological debt, but not unmaintanable one. There some some unit/integration tests written. It is testable, but it wasn't done much. It is an old part of that ERP system (3 years old), over the years it had mutated into what it is now, and there are willingness to try to complete it even if it costs some major efforts.

The problem: the scope which provides a lot of various calculations based on a specific list of objects and the outcomes are not right sometimes.

There are a lot of interesting (not so common) features about this scope:

Dynamic C# formullas (loaded to context and then included in calculcations)

A lot of reports doing the same calculations but on the database

The scope is unstable and period-intense (new features and bugfixes are shipped every month)

This scope has a lot of input parameters on all of the dependant objects. By multiplying all the possible parameter values we have 1,62*107 cases of possible outputs. Not all the parameters warry that much, but most of them do. So slicing half of that number is something around the practical possible otucomes.

This scope is eating up a lot of time and money and is far from done in its current form.

I would like to know what is the silver bullet to the most viable way to stabilize and finish the scope to the state where only minor bugs and nice to haves will occur?

First of all I was thinking, that establishing a DoD (Definition of Done) would be a relevant thing to do. Then I came up to the fact that having that huge number of outputs working is half of that - everything must also be flexible on the UI scope as well.

Will it be enoguh to establish a test plan, maybe do some bruteforcing tests? Should I consider any kind of design patterns? Validation options (like a solid third party validation service, etc.)? Will it be enough or maybe I have gaps in my modest knowledge that I should fill?

Note: the application is not what would one call monolithic. It is based on a lot of different services that can be easily extended, etc.


问:
Limit useless complexity in code
I have a question, to explain that, what better than an entirely fictional example?

Let's say you are a young developer just being employed in a firm.

All data is stored in a huge database (let's say 500+ tables with billion rows).

Your boss ask you to make some consolidation queries stuff.

So, you start making your query and, during the development process you learn a lot of conditions to add to your query.

Result? Your query works pretty well, result asked is correct but is slow and not very easy to understand.

Why? Cause the query, due to a lot of modifications became very complicated.

After that, with checking that with a colleague who work in the firms since years,
he wrote the same query than you but... easier to learn and faster to execute.

So, in fact the main question is: how can we limit this useless complexity ? How can make code more logic in fact?

Actually, my initial idea was to draw activity diagrams of code to see where are bottlenecks but I think a better approach is possible.

Looking for Books, Links, Ideas, Approaches, Methodologies...

答:
Well, in your example, you already provided the only solution that really works: ask someone else for reviewing your code.

To limit useless complexity at first hand, you need experience you get over years by learning, learning, learning. There is no "silver bullet".

Title: Limit useless complexity in code


答:
A couple of principles to help along the way:

YAGNI - You Ain't Gonna Need It: don't build things that you don't need.  Which brings us to:

KISS - Keep It Simple, Stupid: the simplest solutions are often the best.

DRY - Don't Repeat Yourself: duplicated code often deals to a variety of issues.  Like you would normalise a DB, you should normalise your code.

Separation of Concerns / Modularity / SRP.  Keep things focussed, simple.  This makes them reusable and understandable.  This goes doubly for functions / methods, which become exponentially more difficult to (comprehensively) debug as they get longer (see: cyclomatic complexity).

Principle of Least Astonishment - people shouldn't be surprised by your code.  It should do what it claims to do (e.g. if your getters are setting things or have major side effects, you're probably doing something wrong; an exception may be lazy-loading).

Remember that code is meant to be read by people, not just the compiler.  Make sure your code explains itself by having clear names for everything.  If something is surprising, leave comments to explain the intent / why it works like that, not what it does.  A good book to read on this topic might be Clean Code.

I'm also a big fan of refining solutions iteratively.  I guess you can call it refactoring, except it's often without the TDD element, and sometimes involves major changes rather than tiny ones.

I think the value of the above practices and principles really comes together when they are all applied; things become short, simple, obvious.

Title: Limit useless complexity in code

Body:
I have a question, to explain that, what better than an entirely fictional example?

Let's say you are a young developer just being employed in a firm.

All data is stored in a huge database (let's say 500+ tables with billion rows).

Your boss ask you to make some consolidation queries stuff.

So, you start making your query and, during the development process you learn a lot of conditions to add to your query.

Result? Your query works pretty well, result asked is correct but is slow and not very easy to understand.

Why? Cause the query, due to a lot of modifications became very complicated.

After that, with checking that with a colleague who work in the firms since years,
he wrote the same query than you but... easier to learn and faster to execute.

So, in fact the main question is: how can we limit this useless complexity ? How can make code more logic in fact?

Actually, my initial idea was to draw activity diagrams of code to see where are bottlenecks but I think a better approach is possible.

Looking for Books, Links, Ideas, Approaches, Methodologies...


问:
Best way to build a hierarchy CLI menu?
I want to build a command line interface with menus and sub-menus and eventually the leafs of the tree should be operations like..
bank management -> account management -> add acount (insert account info)
And I'm looking for a good way which works with the OO principles...

Thanks for any piece of advice!

答:
if you want OO I suggest a Menu interface with methods to write out the options and parsing the input and returning the next Menu

so your main loop becomes:

```<code>Menu menu = new StartMenu();
    while(menu.isExit){
    menu.writeOptions(stdOut);
    Menu res = menu.parseInput(stdIn.readline());
    if(res==null)
        writeError(stdOut);
    else
        menu=res;
}
</code>```

Title: Best way to build a hierarchy CLI menu?

Body:
if you want OO I suggest a Menu interface with methods to write out the options and parsing the input and returning the next Menu

so your main loop becomes:

Menu menu = new StartMenu();
    while(menu.isExit){
    menu.writeOptions(stdOut);
    Menu res = menu.parseInput(stdIn.readline());
    if(res==null)
        writeError(stdOut);
    else
        menu=res;
}


问:
How to effectively put code live
What are the general strategies to employ when attempting to ensure that a module of code will function correctly on the live system? A common problem we have at our software house is that we typically deploy solutions to a remote client's server, which hasn't got any software-development-related software installed on it. When problems (inevitably) arise in the deployed software, we get annoyed clients telling us to fix the issue, but since we can't remotely debug the code, we get locked into a cycle of effectively having to guess whether our code will work before we deploy it, and then repeating the cycle when the deployed code has issues.

Furthermore, due to the remoteness of the client, it's typically not possible to get a full working test copy of their system set up on our end, so we end up developing against approximations of their system on our end. This makes our debugging and code planning on the development side significantly less effective, because when we deploy we don't know precisely what variable between our test system and the live system is causing problems with our code.

So far, I've implemented a basic error logger in all deployed code that captures the stack trace, exception details, method, class, namespace, parameters and additional messages and error codes at each exception, but this really just speeds up the process of fixing that specific error. I've also tried writing individual tests for specific subsections of modules as executables and running them on the client system, but for modules that are tens of thousands of lines long, this usually just isn't feasible without grinding all development to a halt.

I'm trying to get a strategy in place which would help us better avoid the exceptions in the first place, but I'm stuck on the fact that:

We don't have (and typically can't get) a working test system that accurately models the live system, e.g. an image of the live system, if we do have it then it's months out of date

We can't install remote debugging software on the live server

The client typically has no dedicated test server, so any deployments have to go straight to live

Having no access to a copy of the live system means that we can't write effective unit tests which actually model the conditions the code will run under on the client side

If it helps, the code is typically C# running under .NET 3.5. How to better tackle this issue?

答:
We don't have (and typically can't get) a working test system that accurately models the
  live system, e.g. an image of the live system, if we do have it then it's months out of date

This is the problem. You know this is the problem.

The more complex a system, the more integration points it might have, the more likely you are to have issues.

Why can't you get a working test system? Will your company or client not pay for the servers? Is the data typically sensitive? Can it not be anonymised?

Do you have testers? Why not? Will your company not pay for them?

Typically these types of things should be costed into the build of a system, but as often happens, price is always an issue for clients. You get what you pay for as they say.

Do you get enough time to get all your unit tests in place?

As a programmer at the coal face, you can only mitigate these things so far.

Having your unit tests and code coverage as high and up-to-date as possible, and having your system using Dependency Injection to help facilitate testing by effectively mocking out integration points is a great start.

If a manager comes moaning to you because something has failed in live, this list of reasons is a good place to start as to why it is so difficult to deploy under these conditions.

Title: How to effectively put code live


问:
Does doxygen support templates for the HTML output?
I've documented my code for doxygen, but I don't want the default HTML it gives. I know I can customize it by providing custom CSS, headers, footers, etc. (like GNOME does), and how I can add common PHP code to the files and tell it to save as .php, but that isn't really what I want.

What I want is output like MSDN. I can't describe it really. My question is, Is this possible using doxygen with something like templates, or do I have to output XML and parse it with another program (that I wouldn't mind writing)?

答:
Doxygen does not support something like HTML templates out of the box, as far as I know. You can refer to the image below (from the official doxygen homepage). It shows the input and output files that are related into the entire documentation creation workflow.

As you can see, doxygen reads a so called "Layout file". This file can be customized! However you are somehow restricted to the page structure, doxygen creates.

If you need more flexibility you should prefer to XSLT transform XML output. There is also an sample that shows you how to do this.


答:
Here's some ways:

Choose correct level of complexity. The required functionality determines minimum level of complexity, and the solution should be using that level. Too complex and you need to handle too much of it, Too simple and you can't meet the requirement.

You can't predict the future. Some people think they need to choose more complex version simply to make it possible to meet some arbitrary future requirements. This is bad idea. Arbitrary means that anything is possible. The only thing your complex solution can do is make it more complicated. It cannot solve any problems, since you don't have enough details about the problem.

Details are the king. Complex requirements demand that all the details are correct. It's not complex if the smallest details are not important. Follow the requirement structure accurately and it'll work.

Title: Limit useless complexity in code


问:
Do I check generated code in to source control or not?
I'm developing a .Net application that uses google protocol buffers. Historically the application used the approach, advocated by the protobuf-net team, of decorating the classes with attributes instead of using .proto files.

I am now in the process of migrating part of the application's client to another technology and there is a strong desire to start using the .proto files as the authority so that the two technologies can inter-operate.

My plan is to automatically generate the C# from the .proto, however, my question is should I check the resulting files back into source control? Note I expect the process of code generation to be fast.

How do I choose appropriate approach for above case? What is considered the best practice?

答: If there will be no modification to generated files, there is no point in putting them in version control. You don't put compiled binaries in version control either, right? A basic rule with version control is, that you don't have to put anything in version control, that can be generated from other files. Of course if the generation is somehow complicated or takes a long time, it might not be practical.</p>


答:
As a general rule, generated files do not belong in the source code repository.

The biggest risk you run when you do put those files in the repository is that they become out of sync with their source and the build runs with different protocol buffer files than you would think based on the .proto files.

A few reasons for deviating from the general rule are

Your build environment can't handle the additional build step automatically. If you have to generate the files by hand for every build anyway, you might as well put them in the VCS. Then it actually reduces the risk of a version mismatch due to fewer people having to do the generation step.

The generation step significantly slows down your build

After generating them, the files are further modified by hand. In that case, they are not really generated files any more and thus belong in the VCS.

The source files change very rarely (e.g. they come from a third party that only provides updates every few months or so).

Title: Do I check generated code in to source control or not?

Body:
I'm developing a .Net application that uses google protocol buffers. Historically the application used the approach, advocated by the protobuf-net team, of decorating the classes with attributes instead of using .proto files.

I am now in the process of migrating part of the application's client to another technology and there is a strong desire to start using the .proto files as the authority so that the two technologies can inter-operate.

My plan is to automatically generate the C# from the .proto, however, my question is should I check the resulting files back into source control? Note I expect the process of code generation to be fast.

How do I choose appropriate approach for above case? What is considered the best practice?


问:
Architecture change from using disk to RAM to read and write key/value pairs coming over a network
A WebApp writes a stream of data coming over a network to disk as key/value pair and then reads & send it over network again after few milliseconds in 99% of case. In 1% of cases write/read can be few hours to days apart (really don't know).

Latency is strict "no" in the app and we have to serve 100% of customers therefore I am planning to put data into RAM and later read and send it over network and only write to disk if the data is not read from RAM within a certain prefixed interval.

What could be possible solution to this problem?

P.S - Memcached looks like a good solution for using RAM instead of disk but Can memcached read/write of disk after time_to_live?

I also looking at Couchbase but I don't want to write to disk in all cases as 99% write/read happens in the matter of milliseconds.

Is it possible that after time_to_die Memcached could write the key/value to Couchbase instead of removing it from RAM and read it on demand?

[Edit 1]

After spending last few trying to explore various options. The following looks like a possible solution

Write the data stream simultaneously to the RAM (using Memcached) and disk using (GlusterFS).

In case the data is demanded by the application layer check it first in the RAM (Memcached) if it return the data ok, if it does not check the data on disk GlusterFS.

Remove data stream from RAM if data is demanded by application or when time_to_live is reached whichever is earlier.

Suggestion most welcome.

答:
Remember that RAM is volatile and if the server has to reset then that data would be lost. This sounds like it would be a concern, as you say you have to serve 100% of customers.

I would recommend writing your own buffering system which keeps data in RAM but writes it to disk after a small delay. If the data is served within this time then there is no reason to write to disk, and the resources can be freed.

Remember to reload unsent data at start-up.


问:
How does understanding computer architecture help a programmer?
It is said, by Mike P. Wittie, in the course curriculum of computer architecture that,

Students need to understand computer architecture in order to
  structure a program so that it runs more efficiently on a real machine

I'm asking to more experienced programmers or professionals that have a background in this topic:

How does learning computer architecture help you? What aspect benefits you most? How did learning computer architecture changed your programming structuring?

答:
In today's world, this reasoning is negligible if it is present at all for the majority of programming situations.

The places that it is applicable is when one is writing assembly for a particular processor, working on a situation that requires one to take advantage of a particular architecture, or limited significantly by the architecture (embedded systems) so that the previous two points become all the more important.

For programmers of interpreted languages (perl, python, ruby) or languages that run in their own virtual machine (java, C#) the underlying machine is completely abstracted away.  A Java program wouldn't be coded differently to run on a massive cluster or on one's desktop.

The cases where the architecture does make a difference as mentioned are embedded systems where it is necessary to consider very low level concerns that are for that environment.  The other extreme also exists - where one is writing high performance code either in assembly or something that is compiled to native code (not running in a virtual machine).  In these extremes, one is concerned with what fits into the processor cache and how fast it is to access different parts of memory, which way the branch prediction on the processor goes (if the processor uses branch prediction at all or delay slots).

The question of branch prediction and delay slots or processor cache does not enter in to the vast majority of programming issues and cannot enter into interpreted or virtual machine languages.

All that said, it is useful to understand a level of what is going on one deeper than the existing code is being written at.  The further than that rapidly reaches diminishing returns.  A Java programmer should understand a programming language with manual memory management and pointer math to understand what is going on under the covers.  A C programmer should understand assembly so that one can realize what pointers really are and where memory really comes with.  Assembly coders should be familiar with the architecture to understand what trade offs of branch prediction and delay slots mean... and to take it even further, those designing processors should be familiar with quantum mechanics for how semiconductors and gates work at a very basic level.


答:
First and foremost, version control software is there to help you do your job. So, the first question you ask shouldn't be "should I put X in version control?" but rather "will putting X in version control help me?". Often those two have the same answer, but not always.

In other words, instead of asking us whether we think these files should be in version control or not, ask yourself whether doing so is useful to you. If generating the files is fast, what value is there in saving the generated data? Are your build scripts designed such that they always generate the files, or do they first look to see if something is on disk?

You also have to ask yourself whether it's important to have a concrete record of what went into a build. If you need to precisely reproduce a build, there may be value in having those files in source control.  It's conceivable that a future build may use a newer version of the tool which generates slightly different files -- though another option is to also version-control the tools themselves.

The bottom line is, don't worry so much about what you should or shouldn't do based on somebody else's ideas of a standard, do what's best for your project. Only you can answer that since we don't know all of the constraints and requirements you are dealing with.


答:
It sounds like you're referring to what The Pragmatic Programmer calls passive code generation.

Passive code generators save typing. [...] Once the result is produced, it becomes a full-fledged source file in the project; it will be edited, compiled, and placed under source control just like any other file.

Uses:

Creating new source files

Performing one-off conversions among programming languages

Producing lookup tables and other resources that are expensive to compute at runtime

I would put the generated source files into source control in any of the following situations:

If you're planning to modify the generated C# code

If you want to avoid having to generate the code before running in dev environments? E.g. if a tool must be installed to do the generation

If the generation process is lengthy


问:
How to start a development project when there are too many potential stakeholders
I've just taken on a new job at a college as (the sole) Web application developer.

The college has a number of disparate but all pretty badly coded legacy systems. Mostly built in PHP they deal with things like attendance, exam results, marking etc.

My first job is to build a system that incorporates a lot of this data, which is currently resting in various databases without any kind of friendly API to pull it out (the existing systems are coded in vanilla PHP with no separation of data and view) with a new platform for recording pastoral information about students and presents it to tutors and senior staff in a useful manner so they can react to issues with students quickly.

In our first meeting, there were 18 people! There was no clear leader or voice that represented the majority. No identifiable client. The meeting swung from detailed implementation ideas on minor features from heads of faculty to arguments about whether we should use Excel spreadsheets or not for data input!

As you can imagine my head was spinning at the end. I actually had a lot of good ideas but I couldn't get them heard. This is a very new role for me, before I was part of a development team in a marketing agency. We had very well defined roles: Project Manager, Client, Designer, Developer.

I'd like to know if any seasoned developers or managers out their can give me some pointers on how I can whip my colleagues up into something that resembles a project team. Is agile the way to go? How would you approach handling all the disparate voices? It's clear that some process needs to be put in place very quickly, I'm just not sure what that is.

答:
Your first challenge is to identify the need for this project. Hold another meeting with all those folks and ask them to write down the problems that need to be solved. Don't let them talk about the many ways in which this project will be the solution. Force them to truly identify the needs/problems.

One way to do this is to ask them each individually to document those needs on sticky notes -- one idea per sticky. Then run an Affinity Diagram to help them group those disparate ideas into specific needs. Finally, make them vote (Multi-Voting) so that you can see the biggest needs.

Agile reminds us to tackle the feature that has the most customer value first. Start with the biggest need and then keep splitting that item down until you have the first small piece that you can actually do in a short period of time.


答:
One possible way would be to generate multiple log files (that are cycled and garbage-collected, at some size and/or frequency) and instrument your code to log things that are purely informational, warnings and errors to the log file(s) (and, ideally, have a whole bunch of debug logging that can be enabled by a flag or config setting, so you can get even more detailed logs from running in the customer environment).

These logs can then simply be zipped up by the customer and shipped to you (mail, ftp, burned to CD(s), ...)

Title: How to effectively put code live

Body:
What are the general strategies to employ when attempting to ensure that a module of code will function correctly on the live system? A common problem we have at our software house is that we typically deploy solutions to a remote client's server, which hasn't got any software-development-related software installed on it. When problems (inevitably) arise in the deployed software, we get annoyed clients telling us to fix the issue, but since we can't remotely debug the code, we get locked into a cycle of effectively having to guess whether our code will work before we deploy it, and then repeating the cycle when the deployed code has issues.

Furthermore, due to the remoteness of the client, it's typically not possible to get a full working test copy of their system set up on our end, so we end up developing against approximations of their system on our end. This makes our debugging and code planning on the development side significantly less effective, because when we deploy we don't know precisely what variable between our test system and the live system is causing problems with our code.

So far, I've implemented a basic error logger in all deployed code that captures the stack trace, exception details, method, class, namespace, parameters and additional messages and error codes at each exception, but this really just speeds up the process of fixing that specific error. I've also tried writing individual tests for specific subsections of modules as executables and running them on the client system, but for modules that are tens of thousands of lines long, this usually just isn't feasible without grinding all development to a halt.

I'm trying to get a strategy in place which would help us better avoid the exceptions in the first place, but I'm stuck on the fact that:

We don't have (and typically can't get) a working test system that accurately models the live system, e.g. an image of the live system, if we do have it then it's months out of date

We can't install remote debugging software on the live server

The client typically has no dedicated test server, so any deployments have to go straight to live

Having no access to a copy of the live system means that we can't write effective unit tests which actually model the conditions the code will run under on the client side

If it helps, the code is typically C# running under .NET 3.5. How to better tackle this issue?


问:
Are database developers considered under DBA jobs?
This isn't career advice, this is more of a question of logistics.

When looking at job market and such there is a lower than what I think would be out there of database admins but I'm curious if database admins actually include strictly database developers, or of database developers are more under a blanket term "Programmers" or "Software Developers"?

Like someone who enjoys programming SQL/Queries/Stored Procedures and such but probably knows some about settings a database up and such? Would this be under the DBA term or something else? (Since I haven't seen any reports for Database Developer in "job market forecasts" and such)

答:
It would depend on the individual company, but in general, database developers are called programmers or software developers.  Using a database with SQL is considered part of the skill set of a software developer.

Database administrators (DBA) administer the database.  In other words, they keep the database running at near peak efficiency.  Depending on the database engine we're talking about, these administrative activities require some training or a great deal of training.


答:
Generally, "database development staff" are lumped under the "DBA" term, as in "guys who do database stuff". That's pretty much all you get. Its a bit like job adverts who want someone with 5 years "GUI" experience.

However, the 2 should not be confused. Last place I worked that was highly competent with DBs, we had both DBAs and DB devs. The 2 did different jobs.

DB Devs did the coding, schema organisation etc. They created the SSIS packages and stored procedures, etc. The DBAs managed the DB, they handled backups and scheduled job packages. They handled deployment and managed the running of the DB servers.

So you can find places where the terms are distinct, but they are few and far between. Places that do this will know what they're doing with DBs. Other companies will expect you to do a bit of everything. Its really not that much different from the software side of things. You'll have to enquire about the job in detail (and don't expect the recruiter to know the answer) for each individual job if you can do one but not the other role. I would put your expertise on your CV and make it quite clear what skills you do have.

Title: Are database developers considered under DBA jobs?

Body:
This isn't career advice, this is more of a question of logistics.

When looking at job market and such there is a lower than what I think would be out there of database admins but I'm curious if database admins actually include strictly database developers, or of database developers are more under a blanket term "Programmers" or "Software Developers"?

Like someone who enjoys programming SQL/Queries/Stored Procedures and such but probably knows some about settings a database up and such? Would this be under the DBA term or something else? (Since I haven't seen any reports for Database Developer in "job market forecasts" and such)


答:
The distinction is more likely to exist in companies that are either larger or highly specialized.

For example, my current employer (the largest yet) has DBAs, data modelers and programmers. The duties you described are mixed between the three. DBAs do all of the setup, maintenance and tuning whereas data modelers are solely concerned with schema setup. Sprocs and such can come from either the DBAs or the software developers.

Generally, though I would say that if you want to get your hands dirty in a bit of everything (and there is a lot to be said for it) then you want to work at a smaller company. Otherwise, expect that what you refer to as a "database programmer" is probably an anagram of the duties of several different positions.


问:
Way for store User recent playlist in database
Every time when user hears one music on system, the system stores this music to recent list, but I am afraid to pass over than decimal(19,0) limit in future. If I do Reseed I will get some problems insert invalid duplicate pirmary keys, but if I reallocate all id's it is possible.

Is there a better way for that? With possibility to clean the jump of id (55-60) without future headache with Primary key overflow?
Note: I can limit this list for 255(smallint) maybe, but I want evade use same id`s.

答: Whit native C#, I did on system whit each ID_User number multiplies one logic using each ID getting one range of id whit this calc in another table whit identity AUTOSEED disabled, prevent out range and I can use any row of this table.</p>


问:
How to interview someone you know well?
How would you interview someone you know well or someone you may even be friends with? What if you already know their strengths and weaknesses. I would prefer to avoid this situation by delegating this task to somebody else, but what if this is not an option. I feel like there is just too much personal feelings involved and it is almost impossible to be unbiased. If you have been in an similar situations, how did you handle them? </p>

答:
I was not hired by someone I know very well some years ago when I was pretty hard up for a job.  He simply explained that the final decision was not his.

It stings a little more than a rejection from someone who doesn't know you, but for all I know my outspoken friend's boss just didn't want him having too many allies working at the company.

It's important, and it isn't.

Title: How to interview someone you know well?


问:
In Java, what are checked exceptions good for?
Java's checked exceptions have gotten some bad press over the years. A telling sign is that it's literally the only language in the world that has them (not even other JVM languages like Groovy and Scala). Prominent Java libraries like Spring and Hibernate also don't use them.

I personally have found one use for them (in business logic between layers), but otherwise I'm pretty anti-checked exceptions.

Are there any other uses that I don't realize?

答: They're really good for annoying programmers and encouraging exception swallowing.  Half the point of exceptions is so that you have a sane default way of handling errors and don't have to think about explicitly propagating error conditions you can't handle.  (The sane default being that if you never handle the error anywhere in your code, you've effectively asserted that it can't happen, and are left with a sane exit and a stack trace if you're wrong.)  Checked exceptions defeat this.  They feel a lot like having to explicitly propagate errors in C.</p>


问:
What arguments exist against ever using the keyword substitution feature in many version control systems?
Are there any arguments out there against using the keyword substitution (i.e. replacing $Revision$ with $Revision: 5$) feature in many version control systems?  Are there any overall bad practices that using the feature encourages?  Are the any pervasive and hard-to-solve problems that it causes when you do use it?

Here an argument against using it:

Keyword Substition: Why You Don't Need It

It's succinct, and I think the argument about the problems it causes for binary files is cogent, but I don't find it compelling.

答:
It can be misleading.   If I copy a folder out of source control to a separate tree, then edit it, it will no longer have the correct revision.   You can not simply look at the expanded revision string to determine which version of a file you have.

However, I still think they are valuable.  Having the expanded version string in a file you find outside the source control system allows you to know where to start your search through the history to determine it's validity.  I've found myself needing to do this at least twice in the last month.


答:
I think it is fair to say that checked exceptions were a bit of a failed experiment. Where they went wrong is that they broke layering:

Module A might be built on top of Module B, where

Module B might be built on top of Module C.

Module C might know how to identify an error, and

Module A might know how to handle the error.

The nice separation of concerned is broken, because now knowledge of errors that could have been limited to A and C now have to be scattered over B.

There is a nice discussion of checked exceptions on Wikipedia.

And Bruce Eckel has a nice article on it as well. Here's a quote:

[T]he more random rules you pile onto
  the programmer, rules that have
  nothing to do with solving the problem
  at hand, the slower the programmer can
  produce. And this does not appear to
  be a linear factor, but an exponential
  one

I believe for the case of C++, if all methods have the throws specification clause, then you can have some nice optimizations. I don't believe Java could have those advantages anyway, though, because RuntimeExceptions aren't checked. A modern JIT should be able to optimize the code just fine anyway.

One nice feature of AspectJ is exception softening: You can turn checked exceptions into unchecked exceptions, specifically to enhance layering.

Perhaps it's ironic that Java went through all of this trouble, when it could have checked for null instead, which might have been much more valuable.

Title: In Java, what are checked exceptions good for?

Body:
Java's checked exceptions have gotten some bad press over the years. A telling sign is that it's literally the only language in the world that has them (not even other JVM languages like Groovy and Scala). Prominent Java libraries like Spring and Hibernate also don't use them.

I personally have found one use for them (in business logic between layers), but otherwise I'm pretty anti-checked exceptions.

Are there any other uses that I don't realize?


答:
You've got two good answers which explain what checked exceptions have become in practice. (+1 to both.) But it would also be worthwhile to examine what they were intended for in theory, because the intention is actually worthwhile.

Checked exceptions are actually intended to make the language more type safe. Consider a simple method like integer multiplication. You might think that the result type of this method would be an integer, but, strictly speaking, the result is either an integer or an overflow exception. Considering the integer result by itself as the return type of the method does not express the full range of the function.

Seen in this light, it's not strictly true to say that checked exceptions have not found their way into other languages. They have just not taken the form that Java used. In Haskell applications, it is common to use algebraic data types to distinguish between successful and unsuccessful completion of the function. Although this is not an exception, per se, the intention is very much the same as a checked exception; it is an API designed to force the API consumer to handle both the successful in the unsuccessful case, e.g.:

```<code>data Foo a =
     Success a
   | DidNotWorkBecauseOfA
   | DidNotWorkBecauseOfB
</code>```

This tells the programmer that the function has two additional possible results besides success.

Body:
You've got two good answers which explain what checked exceptions have become in practice. (+1 to both.) But it would also be worthwhile to examine what they were intended for in theory, because the intention is actually worthwhile.

Checked exceptions are actually intended to make the language more type safe. Consider a simple method like integer multiplication. You might think that the result type of this method would be an integer, but, strictly speaking, the result is either an integer or an overflow exception. Considering the integer result by itself as the return type of the method does not express the full range of the function.

Seen in this light, it's not strictly true to say that checked exceptions have not found their way into other languages. They have just not taken the form that Java used. In Haskell applications, it is common to use algebraic data types to distinguish between successful and unsuccessful completion of the function. Although this is not an exception, per se, the intention is very much the same as a checked exception; it is an API designed to force the API consumer to handle both the successful in the unsuccessful case, e.g.:

data Foo a =
     Success a
   | DidNotWorkBecauseOfA
   | DidNotWorkBecauseOfB

This tells the programmer that the function has two additional possible results besides success.