答:
Because I am a web developer, I am using web technologies to solve the
  problem. I'm afraid of my limited mindset and picking wrong tools for
  the job.

You're falling into the "when all you have is a hammer, everything looks like a nail" trap.  But you're able to recognize it, which is a very good thing.

Is HTTP/HTTPS good enough for file transfer?

Of course.  Billions, if not trillions, of files are transferred via HTTP daily.  Any file you transfer to S3 is going to go that way.

Is Node.js good enough for working with File System?

I don't know a great deal about Node.js, but if your goal is to just have a standalone program that runs on a machine and keeps a directory and S3 in sync, it it doesn't seem like the right tool for the job.

If you're willing to learn Java, the Apache Commons Virtual File System can talk to a variety of backends (including the local file system), and VFS-S3 is an add-on that makes it work with S3.  VFS includes a class called AbstractSyncTask that takes care of 90% of the grunt work, leaving you to just extend the class and implement your transfer rules.  Being Java, it will run anywhere Java does.

Scalability: can this approach fail in large file sizes?

That would depend on the limits of Node.js, although I'd be surprised if someone hasn't already bumped into that problem and made sure it can transfer arbitrary-length files.

Title: Is it a bad idea to sync file system with remote server using HTTP?

Body:
I have started a project which will duplicate Dropbox or Google Drive behavior but using Amazon S3 az a backend.

Idea is very simple, a Node.js server that watchs a directory for file changes and PUT them on the S3. Or it will look at S3 for changes and applies them to file system structure. I'be uploaded very early version of my app to Github. You can find it here.

Because I am a web developer, I am using web technologies to solve the problem. I'm afraid of my limited mindset and picking wrong tools for the job. There are other solutions to this problem. One is S3FS which is a FUSE file system for Unix systems. In my opinion that is very hard to use and limited to platform. My solution uses Node.js to overcome cross-platform issues. I can pack my Node.js app with App.js and make it an easy to use software.

To clarify, my questions are:

Is HTTP/HTTPS good enough for file transfer?

Is Node.js good enough for working with File System?

Scalability: can this approach fail in large file sizes?


问:
Why do we need "callback functions"?
I am reading the book programming in Lua. It said that

Closures provide a valuable tool in many contexts. As we have seen, they are
  useful as arguments to higher-order functions such as sort. Closures are valuable for functions that build other functions too, like our newCounter example;
  this mechanism allows Lua programs to incorporate sophisticated programming
  techniques from the functional world. Closures are useful for callback functions,
  too. A typical example here occurs when you create buttons in a conventional
  GUI toolkit. Each button has a callback function to be called when the user
  presses the button; you want different buttons to do slightly different things
  when pressed. For instance, a digital calculator needs ten similar buttons, one
  for each digit. Y ou can create each of them with a function like this:

```<code>function digitButton (digit)
  return Button{label = tostring(digit),
                action = function ()
                  add_to_display(digit)
                  end}
end
</code>```

It seems that if I call the digitButton, it will return the action (this will create a closure), so, I can access the digit passed to digitButton.

My question is that:

```<code>Why we need call back functions? what situations can I apply this to?
</code>```

The author said:

In this example, we assume that Button is a toolkit function that creates new
  buttons; label is the button label; and action is the callback closure to be
  called when the button is pressed. The callback can be called a long time after
  digitButton did its task and after the local variable digit went out of scope, but
  it can still access this variable.

according to the author, I think a similar example is like this:

```<code>function Button(t)
  -- maybe you should set the button here
  return t.action -- so that you can call this later
end

function add_to_display(digit)
  print ("Display the button label: " .. tostring(digit))
end

function digitButton(digit)
  return Button{label = tostring(digit),
                action = function ()
                           add_to_display(digit)
                           end}
end

click_action = digitButton(10)
click_action()
</code>```

thus, the callback can be called a long time after digitButton did its task and after the local variable digit went out of scope.

答:
Callbacks have been around since the early days of Fortran, at least.
For example, if you have an ODE (ordinary differential equation) solver, such as a Runge-Kutta solver, it might look like this:

```<code>subroutine rungekutta(neq, t, tend, y, deriv)
  integer neq             ! number of differential equations
  double precision t      ! initial time
  double precision tend   ! final time
  double precision y(neq) ! state vector
  ! deriv is the callback function to evaluate the state derivative
  ...
  deriv(neq, t, y, yprime) ! call deriv to get the state derivative
  ...
  deriv(neq, t, y, yprime) ! call it several times
  ...
end subroutine
</code>```

It enables the caller to customize the behavior of the subroutine by passing it a special-purpose function to be called when necessary.
This allows the ODE solver to be used to simulate a wide variety of systems.

ContentLicense: CC BY-SA 3.0

Title: Why do we need "callback functions"?

Body:
Callbacks have been around since the early days of Fortran, at least.
For example, if you have an ODE (ordinary differential equation) solver, such as a Runge-Kutta solver, it might look like this:

subroutine rungekutta(neq, t, tend, y, deriv)
  integer neq             ! number of differential equations
  double precision t      ! initial time
  double precision tend   ! final time
  double precision y(neq) ! state vector
  ! deriv is the callback function to evaluate the state derivative
  ...
  deriv(neq, t, y, yprime) ! call deriv to get the state derivative
  ...
  deriv(neq, t, y, yprime) ! call it several times
  ...
end subroutine

It enables the caller to customize the behavior of the subroutine by passing it a special-purpose function to be called when necessary.
This allows the ODE solver to be used to simulate a wide variety of systems.


问:
Is it OK to split long functions and methods into smaller ones even though they won't be called by anything else?
Lately I've been trying to split long methods into several short ones.

For example: I have a process_url() function which splits URLs into components and then assigns them to some objects via their methods. Instead of implementing all this in one function, I only prepare the URL for splitting in process_url(), and then pass it over to process_components() function, which then passes the components to assign_components() function.

At first, this seemed to improve readability, because instead of huge 'God' methods and functions I had smaller ones with more descriptive names.
However, looking through some code I've written that way, I've found that I now have no idea whether these smaller functions are called by any other functions or methods.

Continuing previous example: someone looking at the code might think that process_components() functionality is abstracted into a function because it's called by various methods and functions, when in fact it's only called by process_url().

This seems somewhat wrong. The alternative is to still write long methods and functions, but indicate their sections with comments.

Is the function-splitting technique I described wrong? What is the preferred way of managing large functions and methods?

UPDATE: My main concern is that abstracting code into a function might imply that it could be called by multiple other functions.

SEE ALSO: discussions on reddit at /r/programming (provides a different perspective rather than most of the answers here) and /r/readablecode.

答:
I'm sure this won't be the popular opinion, but it's perfectly ok.  Locality of Reference can be a huge aid in making sure you and others understand the function (in this case I'm referring to the code and not to memory specifically).

As with everything, it's a balance.  You should be more concerned with anyone who tells you 'always' or 'never'.


问:
how to convince team and project manager that post-launch deployments are okay for new features
We have a site that launched and is "feature-complete", we've done all that the client has asked for and fixed a few bugs post-launch. However, I noticed that our site's home page is using 100 SQL queries, and there's no caching. I took half a day to add caching and did it in a branch.

Now I'm wondering how I can convince the project manager and other team members and gain support for a post-launch deployment of this optimization.

What reasons can I give that it won't harm the project to do this? Or do I just wait until the next project launches to make these sorts of improvements?

More details:

I can't very well tell future employers that I implemented caching and saw an increase in performance without actually deploying it and capturing the improvement (through google analytics or Page Speed or some other method), or can I?

This is an agency environment, none of our projects use any caching or any other performance optimization

The site has 50k visitors

The site is only live with full functionality for 1 month, around the end of May it will be replaced with a static page (marketing campaign site)

local testing reducing page load from ~3 seconds to ~700ms

答:
While what you have done is sure to be a good thing, the project manager has to worry about a number of things:

testing - what if you break something. Either something in the home page, or some other unintended consequence? The system needs to be retested. Depending on how far your company goes, this could be a large cost.

Scheduling user downtime. May be trivial, may be costly.

The PM may worry about any political reasons to do this outside a normal release. Telling the customer so soon after release that you want to do an update purely for performance issues that have NOT been raised by users may decrease confidence in the system. Maybe he would prefer this was rolled into a normal release cycle.

Additionally, if no-one has reported performance as an issue, then why should the project waste money fixing an issue that to them, does not exist?


答:
Typically one would refer to an in-between mini release as a patch.  The line between a patch and a full software release however is not a settled matter by any stretch of the imagination.  It is sometimes hard to make that determination.

How much development work went into the patch?

It is not always about how much development work went into the patch.  A patch might comprise less than an hour of easy development but the impacts these changes make can be monumental.

How much risk am I introducing?

Above all else a patch must be stable.  Stability of a patch is directly correlated with the number of components, features and deployment complexity that are impacted by the patch changes.  There are a number of different risk scoring systems to choose from but they all tend to revolve around the number of impacts and the importance of these features to the client.

High risk is okay as long as it is met with an equally high amount of quality assurance and testing effort.

Does your system have repeatable unit tests covering components or functionality that might be impacted by caching?

Does there exist any kind of automated UI or front end testing (Eg. Selenium )

Do you have load testing scripts that can verify the cache holds up to production level + demands?

These are all a minimum requirement in my mind just to even consider something like caching being introduced into software in the form of a patch.

Caches are no small thing.  Small mistakes can have profound show stopping issues so you must be sure that you get it right.

I can't very well tell future employers that I implemented caching and saw an increase in performance without actually deploying it and capturing the improvement (through google analytics or Page Speed or some other method), or can I?

You can and you should.  It is important that you have a seperate test environment for your web site that is self contained, has quality data, and attempts to mimic the production environment in as much a way as possible.  How can this be done?

Seperate public hosting for your test environment, or unutilized or under utilized in house server that can be used to host your web application.

Database can be established with the same exact schema and a snapshot of real production data (De-identified of course if it contains sensitive user information).

Now with automated UI testing tools as well as load testing tools against your test environment, you should be able to mimic the 50k visitor load against a test environment that is a copy of the production.  This will help flesh out any hidden problems that you would otherwise unfortunately discover in production post deployment.

Title: how to convince team and project manager that post-launch deployments are okay for new features

Body:
We have a site that launched and is "feature-complete", we've done all that the client has asked for and fixed a few bugs post-launch. However, I noticed that our site's home page is using 100 SQL queries, and there's no caching. I took half a day to add caching and did it in a branch.

Now I'm wondering how I can convince the project manager and other team members and gain support for a post-launch deployment of this optimization.

What reasons can I give that it won't harm the project to do this? Or do I just wait until the next project launches to make these sorts of improvements?

More details:

I can't very well tell future employers that I implemented caching and saw an increase in performance without actually deploying it and capturing the improvement (through google analytics or Page Speed or some other method), or can I?

This is an agency environment, none of our projects use any caching or any other performance optimization

The site has 50k visitors

The site is only live with full functionality for 1 month, around the end of May it will be replaced with a static page (marketing campaign site)

local testing reducing page load from ~3 seconds to ~700ms


答:
Well first and foremost, it has to pass all your regular Q/A steps AND show a real performance improvement.

Now you're faced with a business problem: the client has what they paid for.  They've accepted it, and are using it in production.  They think the project is finished.

It is possible that they don't want to risk 'rocking the boat' for an update that would bring little or no visible changes to them (this is why pure refactoring changes are sometimes resisted by the business).

It is also possible that your sales folk would want to get paid for this improvement.

Old joke: a sales guy & a programmer are meeting with the customer.  The customer asks for a difficult feature & the sales guy promises it immediately.  The programmer kicks the sales guy under the table.  The customer then asks that the main page's color be changed.  When the programmer says 'no problem', the sales guy kicks him under the table.  Later he says, 'you idiot - the customer would have paid $10k for that change'.

TL;DR

Test your changes to ensure they are truly improvements.  Run them through your Q/A process.  After that, introduce the improvements to the project manager and have her run it past the business folk.  Maybe the changes will be promoted, maybe they won't.  Either way you did some good work.


答:
Has the client approved the cache expiration period?

Have you run the same tests as for the last release against your
branch?

Will redeploying result in the site being unavailable? If yes, will the client provide a variance to your service level agreement regarding up-time?

If the answer to any of these is "No", then it isn't ready for deployment. If the answer to all is "Yes", then it should be a clear win for integrating back into the trunk/main branch.


问:
Best way to implement a website login and database tables for a small shop
I'm building a website for a small store and I was told that its better not to keep the login and the rest of the users information in the same table.

Now im wondering, what is the best way to implement this website's database?

Some of the database tables are:

Customers

Providers

Products

Etc..

I need to have different users, after someone is logged in the site, depending on their rights they can do a series of things:

If they are a customer they can order stuff

If they are employees they can order stuff, manage stock, add customers, etc

If they are the owner or administrator they can add employees and everything else.

Should I keep the login and the rest of the information of the user in the same table?

答: I think you should use the ASP.NET membership functionality. You can create a separate database structure with that and many of the security level hard working is done for you by the ASP.NET.</p>

ContentLicense: CC BY-SA 4.0


问:
How to reject a code review that you believe is unnecessary?
I am in a position where I have been asked to review some code that fixes a problem that I don't believe exists.

The fixer, who is more senior than me, insists his fix is necessary but it appears to be no more than C++ sophistry to me.  Part of our deployment process is a code review, and as the 2nd highest engineer in a small company I am expected to review changes.

I believe that reviewers are just as responsible for code changes as the original coder, and I am unwilling to accept responsibility for this change. How would you go about rejecting this review?

答:
Recently in LinkedIn news, there was an article about conflict resolution in the workplace and being humble, rather than appearing arrogant. Sadly, I can't find the link now, but the general gist was to try to form questions in a non-confrontational way. Please don't take this as me implying that you are arrogant, it's just the way in which the article was written.

Anyhow, in telling the senior programmer that he is wrong in his assumption will only lead to him taking a defensive approach and attack you back in his response. However, if you pose the question of why he believes the problem exists, from the point of view of your lack of understanding, then s/he is likely to be more open to discuss the matter.

So, rather than say "The problem doesn't exist, so I shouldn't have to review this", perhaps ask something like "In order to review this properly, I need to understand the problem. Can you please explain it from your point of view?"

As an example, the article described a test given to children where an adult held up a cube whose faces all were one colour, except for the one facing the adult. When the children were asked what colour face the adult was seeing, those under 5 years of age would almost always say the colour they could see, as they couldn't comprehend that the adult could have a different view to their own.


问:
Is this the correct way to implement .NET MVC website structure?
I have recently seen a .NET MVC solution in which the markup in the .aspx views which appear to have a Controller as their model i.e the controller seems to be providing the data for the view, and the .ascx user controls they contain use a separate model. I'm new to MVC and I wanted to find out about a few things I'm not clear on.

An example of how the code is implemented:

UserDetails.aspx view has markup that shows it's using the UserDetailsController.cs as the model. It contains RenderPartial("User_Details.ascx", UserDetailsModel) and passes it the UserDetailsModel.

Is this the standard/correct way of implementing MVC? Or just one way to implement it?

I also noticed that the classes used as Models appear to be Service classes that have [DataMember] and [DataContract] attributes on the class name and properties - what is the advantage of this implementation?

答:
The basic concept of MVC is:

Model: This is the domain model of your application.
View: This is the User Interface of your application. (you should not put any logic here.)
Controller: This is responsible for the interaction with the user. Pull data from 'Model' and select 'View' for the user interface.

For more details: http://www.asp.net/mvc/tutorials/older-versions/overview/asp-net-mvc-overview.

You can see the basic folder structure when you create a new ASP.NET MVC application in Visual Studio. Here Controlers are kept in one folder, Models are another and there is a view folder in which you can find many subfolders with the same name of the controllers. Under these folder you can find the views which the perticular controller will use. There is a 'shared' folder, under it has the views which can be usable for all the controllers. Microsoft create a convention over configuration concept in ASP.NET MVC. That is why the nameing convention and the folder stucture is essential.

[DataMember] and [DataContract] attributes are related with WCF.

Hope this help.


问:
Should my async task library swallow exceptions quietly?
I've just learned that .NET 4.5 introduced a change to how exceptions inside a Task are handled. Namely, they are quietly suppressed.

The official reasoning for why this was done appears to be "we wanted to be more friendly to inexperienced developers":

In .NET 4.5, Tasks have significantly more prominence than they did in .NET 4, as they’re baked in to the C# and Visual Basic languages as part of the new async features supported by the languages.  This in effect moves Tasks out of the domain of experienced developers into the realm of everyone.  As a result, it also leads to a new set of tradeoffs about how strict to be around exception handling.

(source)

I've learned to trust that many of the decisions in .NET were made by people who really know what they're doing, and there is usually a very good reason behind things they decide. But this one escapes me.

If I were designing my own async task library, what is the advantage of swallowing exceptions that the developers of the Framework saw that I'm not seeing?

答:
TL;DR - No, you shouldn't just ignore exceptions quietly.

Let's look at the assumptions.

Your blind faith

I've learned to trust that many of the decisions in .NET were made by people who really know what they're doing, and there is usually a very good reason behind things they decide. But this one escapes me.

MS has some really brilliant people on staff, no doubt.  They also have a number of managers and executives who are ... clueless, and who consistently make bad decisions.  As much as we'd like to believe the fairy tale of the technically savvy scientist driving the product development, the reality is far more grim.

My point being, if your instinct tells you something may be wrong then there's a good chance (and past precedent) that it is wrong.

That this is a technical issue

How to handle exceptions in this case is a human factors decision, not a technical decision.  Loosely, an exception is an error.  Presentation of the error, even if poorly formatted, provides critical information to the end user.  Namely, something went wrong.

Consider this hypothetical conversation between and end user and a developer.

User: The app is broken.

  Dev: What's broken?

  User: I dunno, I click on the button and nothing happens.

  Dev: What do you mean by nothing happens?

  User: Look, I click, I wait, and I wait, and I wait, and nothing... it's obviously broken.

Our poor, fortunately hypothetical Developer now has to figure out what went wrong in the chain of events.  Where was it?

Event handler notification -> Routine to handle the event -> Method triggered by handler -> beginning of asynchronous call -> the 7 layers of OSI networking -> physical transmission -> back up the 7 layers of OSI networking -> receiving service -> method called by service -> ... -> return reply sent by service -> .... -> asynch receipt -> processing of asynch reply -> ...

And please note that I've glossed over several potential error paths there.

After addressing some of the underlying assumptions, I think it becomes more clear why quietly suppressing exceptions is a bad idea.  An exception and associated error message is a key indicator to the users that something went wrong.  Even if the message is meaningless to the end user, the embedded information can be useful to the Developer in understanding what went wrong.  If they're lucky, the message will even lead towards the solution.

I think part of the problem here is that an incorrectly handled exception will crash your application.  By suppressing the exceptions, the application won't crash.  There is some validity to this though since the point of async is to allow the application to keep working while data is being retrieved.  It's not that much of a logical extension to say that if you could keep operating while waiting on the results then a failure in retrieval shouldn't crash the application either - the async call is implicitly declared as not important enough to end the application.

So it's a solution, but it's solving the wrong problem.  It doesn't take that much effort to provide a wrapper to the exception handling so that the application can remain running.  The exception is caught, error message thrown to the screen or logged, and the application is allowed to keep on running.  Suppressing the exception implies that ignoring errors is going to be A-OK and that your testing will make sure exceptions never escape to the field anyway.

So my end assessment is that this is a half-baked attempt at resolving an issue created by pushing people into an asynchronous model when they weren't ready to shift their thinking to that approach.


问:
Should I Do Calculations in T-SQL or Program?
I am creating a stored procedure that is logging some data. The data ultimately needs to end up in 2 tables. The incoming data is coming from a JSON string containing 15 parameters, and the data is then logged to the database using the stored procedure I'm writing. I would like to send the data into 1 stored procedure and INSERT it into both tables.

The first table is a raw data logging table. It will be used for debug and troubleshooting.

The second table will be used for report generation. This table requires some simple mathematical calculations to be done on incoming data. For example:

DECLARE @Table2Fld3 DECIMAL = @IncomingFld9 - @IncomingFld4;

I'll have about 8 of these calculations to do to compute the values for table 2, then I'll do an INSERT to save the data.

So my question is, is it a good practice to do these calculations in the T-SQL? Or would it be better for me to create 2 separate stored procedures and do the calculations in my code?

One trade-off I see is that if I do it all in the code then I have to create 2 database connections.

EDIT

I should elaborate on the "2 database connections" comment. The application in question is a windows service that establishes multi-threaded server/client communication. The logging system is asynchronous to the server/client communication. Using that existing system, in order for me to target multiple stored procedures, it would require 2 calls to the logger which would spin up 2 connections to the database.

答:
The primary responsibility of database engine is for storing data and pull them when we need them.

We should put our business logic / manipulation with data in program code. So that it will not dependent on database. It can be unit testable. Better in performence. We can use the full OOP concept in our business logic.


问:
Worst coding standard you've ever had to follow?
Have you ever had to work to coding standards that:

Greatly decreased your productivity?

Were originally included for good reasons but were kept long after the original concern became irrelevant?

Were in a list so long that it was impossible to remember them all?

Made you think the author was just trying to leave their mark rather than encouraging good coding practice?

You had no idea why they were included?

If so, what is your least favorite rule and why?

Some examples here

答:
Limited space for variable/object names is probably my largest irritation. I've worked in a relatively modern, proprietary language that only allows 10 characters. This is a holdover from its original versions.

The net result is that you end up with funny naming conventions defining what each character of your allowed 10 is to represent. Something like:

1-3: application prefix

4-6: module prefix

7-9: user defined section

10: a number just in case two...or 9 are named the same thing.


问:
What are the worst false economies in software development?
What are the worst false economies (that is ways of saving money that ultimately cost more than they save) prevalent in the software industry and how do you combat them?</p>

答:
No dedicated resources for project management

I've experienced several times when a few programmers were contracted, and someone who already has a demanding day-job should have managed the project, but in fact was too busy with other tasks so the project never really gained momentum. The programmers made "prototypes" and stuff, but without a lead, much of it was running in circles to look busy.

Bad equipment for new programmers

I've once experienced a company where the policy was "new programmers have to work on an really old PC with a small screen until they prove they are worthy". No surprise that such a policy caused a negative selection that drove off good people who always have the choice to work in a more sane environment.

ContentLicense: CC BY-SA 2.5


问:
Is a try and catch that does not throw an exception more efficient than a conditional?
I came across this example recently:

If 999 times out of 1,000 an exception will not be thrown then the exception is only generated once. On the other hand a conditional would have been called needlessly 999 times, therefore in this case the exception is superior.

In this instance it's C#, but generally speaking is this true? I had previously assumed try/catch statements had their own overhead that would equal the time spent handling a conditional.

Granted, just throwing try/catch blocks anyplace a conditional would normally go would be a terrible way to code, but resource-wise does this statement hold up?

答:
There are scenarios to use try/catch and others to use conditionals.

Using a try/catch does not harm performance as outlined extensively Here

The overall cost of a try...catch block that never handles an exception is a few bytes of memory

Beyond performance, proper exception handling is important. The last thing you want is uncaught errors being shown to a user, buggy performance or the application hanging after an issue is encountered.


答:
It may or may not make sense to have the login and customer information in the same table. As always it just depends on the needs of the business/application. If employees and administrators are "customers" (and more), then you could get away with putting the logins in with the customer information.

However, I tend toward keeping the logins in their own table both for clarity and for the possibility that a future role may not have customer functions. It provides more flexibility and a better separation of concerns.

Title: Best way to implement a website login and database tables for a small shop

Body:
I'm building a website for a small store and I was told that its better not to keep the login and the rest of the users information in the same table.

Now im wondering, what is the best way to implement this website's database?

Some of the database tables are:

Customers

Providers

Products

Etc..

I need to have different users, after someone is logged in the site, depending on their rights they can do a series of things:

If they are a customer they can order stuff

If they are employees they can order stuff, manage stock, add customers, etc

If they are the owner or administrator they can add employees and everything else.

Should I keep the login and the rest of the information of the user in the same table?

OwnerDisplayName: Facio Ratio


答:
Since the design is for a small website, I think you can have a normalize database up to 2Nf.

By keeping the user login credentials in one table and their information's in another table. As you mentioned this is an exercise project try to make a good impression demonstrating professionalism.

Have separate role and permission tables. Depending upon the role identify, the permission.

OwnerDisplayName: I am legend


答:
I think below table design would suit your requirements.

```<code>**User**
-----------
UserId
UserRoleId
UserFname
UserLName
.
.
.

**UserLogin**
------------
UserId
LoginName
Password

**UserRoles**
---------------
RoleId
Type
</code>```

Now based on the UserRole you can decide which all features need to be shown on website.
Here UserRole could be Customer,Provider or Admin

Body:
I think below table design would suit your requirements.

**User**
-----------
UserId
UserRoleId
UserFname
UserLName
.
.
.

**UserLogin**
------------
UserId
LoginName
Password

**UserRoles**
---------------
RoleId
Type

Now based on the UserRole you can decide which all features need to be shown on website.
Here UserRole could be Customer,Provider or Admin

OwnerDisplayName: AnandPhadke


答:
If your programming language supports it, you might be able to define your "helper" functions within the scope of your process_url() function to get the readability benefits of separate functions.  e.g.

```<code>function process_url(url) {

    function foo(a) {
        // ...
    }

    function bar(a) {
        // ...
    }

    return [ foo(url), bar(url) ];

}
</code>```

If your programming language doesn't support this, you might move foo() and bar() out of the scope of process_url() (so that it is visible to other functions/methods)--but consider this a "hack" you've put in place because your programming language doesn't support this feature.

Whether to break a function into sub-functions will probably depend on whether there are meaningful/useful names for the parts and how large each of the functions are, among other considerations.

Title: Is it OK to split long functions and methods into smaller ones even though they won't be called by anything else?

Body:
Lately I've been trying to split long methods into several short ones.

For example: I have a process_url() function which splits URLs into components and then assigns them to some objects via their methods. Instead of implementing all this in one function, I only prepare the URL for splitting in process_url(), and then pass it over to process_components() function, which then passes the components to assign_components() function.

At first, this seemed to improve readability, because instead of huge 'God' methods and functions I had smaller ones with more descriptive names.
However, looking through some code I've written that way, I've found that I now have no idea whether these smaller functions are called by any other functions or methods.

Continuing previous example: someone looking at the code might think that process_components() functionality is abstracted into a function because it's called by various methods and functions, when in fact it's only called by process_url().

This seems somewhat wrong. The alternative is to still write long methods and functions, but indicate their sections with comments.

Is the function-splitting technique I described wrong? What is the preferred way of managing large functions and methods?

UPDATE: My main concern is that abstracting code into a function might imply that it could be called by multiple other functions.

SEE ALSO: discussions on reddit at /r/programming (provides a different perspective rather than most of the answers here) and /r/readablecode.

Body:
If your programming language supports it, you might be able to define your "helper" functions within the scope of your process_url() function to get the readability benefits of separate functions.  e.g.

function process_url(url) {

    function foo(a) {
        // ...
    }

    function bar(a) {
        // ...
    }

    return [ foo(url), bar(url) ];

}

If your programming language doesn't support this, you might move foo() and bar() out of the scope of process_url() (so that it is visible to other functions/methods)--but consider this a "hack" you've put in place because your programming language doesn't support this feature.

Whether to break a function into sub-functions will probably depend on whether there are meaningful/useful names for the parts and how large each of the functions are, among other considerations.


答:
An example of a better performance using try/catch instead of a conditional is when dealing with Dictionaries, when asking if contains the key is O(n) versus just try to access/add the key and catch the potential exception.</p>

```<code>try
{
    dict.Add(key, val);
}
catch (Exception)
{

    dict[key] = val;
}
</code>```

Title: Is a try and catch that does not throw an exception more efficient than a conditional?

Body:
I came across this example recently:

If 999 times out of 1,000 an exception will not be thrown then the exception is only generated once. On the other hand a conditional would have been called needlessly 999 times, therefore in this case the exception is superior.

In this instance it's C#, but generally speaking is this true? I had previously assumed try/catch statements had their own overhead that would equal the time spent handling a conditional.

Granted, just throwing try/catch blocks anyplace a conditional would normally go would be a terrible way to code, but resource-wise does this statement hold up?

Body:
An example of a better performance using try/catch instead of a conditional is when dealing with Dictionaries, when asking if contains the key is O(n) versus just try to access/add the key and catch the potential exception.

try
{
    dict.Add(key, val);
}
catch (Exception)
{

    dict[key] = val;
}


答:
It's generally true for commercial-quality compilers. However, conditionals may achieve similar efficiencies using assume(false)-style annotations. Profile Guided Optimization may beat both.

The underlying reason is that good compilers can generate more efficient code by making correct assumptions about the likelyhood of code being executed. Since the convention is that exceptions are exceptional, most compilers (in the absence of profiling data) will generate code that is optimal when exceptions are indeed rare.

For instance, the exception-handling code may be put in its own segment, and only be paged in when the first exception occurs. This means the CPU cache can be used more efficiently, storing only non-exceptional code.


问:
Testing multi-threaded race conditions
Reading the comments to this answer, specifically:

Just because you can't write a test doesn't mean it's not broken. Undefined behaviour which usually happens to work as expected (C and C++ are full of that), race conditions, potential reordering due to a weak memory model... – CodesInChaos 7 hours ago

@CodesInChaos if it cant be reproduced then the code written to 'fix' cant be tested either. And putting untested code into live is a worse crime in my opinion – RhysW 5 hours ago

...has me wondering if there are any good general ways to consistently trigger very infrequently occurring in production problems caused by race conditions in test case.

答:
Exposing a multi-threading bug requires forcing different threads of execution to perform their steps in a particular interleaved order. Usually this is hard to do without manual debugging or manipulating the code to get some kind of "handle" to control this interleaving. But changing code that behaves unpredictably will often influence that unpredictability, so this is hard to automate.

A nice trick is described by Jaroslav Tulach in Practical API Design: if you have logging statements in the code under question, manipulate the consumer of those logging statements  (e.g. an injected pseudo-terminal) so that it accepts the individual log messages in a particular order based on their content. This allows you to control the interleaving of steps in different threads without having to add anything to production code that isn't already there.

Title: Testing multi-threaded race conditions


答:
After having been in this crazy business since about 1978, having spent almost all of that time in embedded real-time computing, working multitasking, multithreaded, multi-whatever systems, sometimes with multiple physical processors, having chased more than my fair share of race conditions, my considered opinion is that the answer to your question is quite simple.

No.

There's no good general way to trigger a race condition in testing.

Your ONLY hope is to design them completely out of your system.

When and if you find that someone else has stuffed one in, you should stake him out an anthill, and then redesign to eliminate it.  After you have designed his faux pas (pronounced f***up) out of your system, you can go release him from the ants.  (If the ants have already consumed him, leaving only bones, put up a sign saying "This is what happens to people who put race conditions into XYZ project!" and LEAVE HIM THERE.)

Title: Testing multi-threaded race conditions

Body:
Reading the comments to this answer, specifically:

Just because you can't write a test doesn't mean it's not broken. Undefined behaviour which usually happens to work as expected (C and C++ are full of that), race conditions, potential reordering due to a weak memory model... – CodesInChaos 7 hours ago

@CodesInChaos if it cant be reproduced then the code written to 'fix' cant be tested either. And putting untested code into live is a worse crime in my opinion – RhysW 5 hours ago

...has me wondering if there are any good general ways to consistently trigger very infrequently occurring in production problems caused by race conditions in test case.


答:
If you are in the ms tool chain.  Ms research has created a tool which will force new interlevings for each run and can recreated failed runs its called chess.

here is a video showing it in use.

Title: Testing multi-threaded race conditions


问:
In functional programming, are local mutable variables with no side effects still considered "bad practice"?
Is having mutable local variables in a function that are only used internally, (e.g. the function has no side effects, at least not intentionally) still considered "non functional"?

e.g. in the "Functional programming with Scala" course style check considers any var usage as bad

My question, if the function has no side effects, is writing imperative style code still discouraged?

e.g. instead of using tail recursion with the accumulator pattern, what's wrong with doing a local for loop and creating a local mutable ListBuffer and adding to it, as long as the input is not changed?

If the answer is "yes, they are always discouraged, even if there is no side effects" then what is the reason?

答:
It's not really good to boil this down to "good practice" vs "bad practice". Scala supports mutable values because they solve certain problems much better than immutable values, namely those that are iterative in nature.

For perspective, I'm fairly sure that via CanBuildFrom almost all immutable structures provided by scala do some sort of mutation internally. The point is that what they expose is immutable. Keeping as many values immutable as possible helps make the program easier to reason about and less error prone.

This doesn't mean that you necessarily need to avoid mutable structures and values internally when you have a problem that is better suited to mutability.

With that in mind, a lot of problems that typically require mutable variables (such as looping) can be solved better with a lot of the higher order functions that languages like Scala provide (map/filter/fold). Be aware of those.


答:
The one thing that is unequivocally bad practice here is claiming that something is a pure function when it isn't.

If mutable variables are used in a way that is truly and completely self-contained, the function is externally pure and everyone is happy. Haskell in fact supports this explicitly, with the type system even ensuring that mutable references can't be used outside the function that creates them.

That said, I think talking about "side effects" is not the best way to look at it (and is why I said "pure" above). Anything that creates a dependency between the function and external state makes things harder to reason about, and that includes things like knowing the current time or using concealed mutable state in a non-thread-safe way.

Title: In functional programming, are local mutable variables with no side effects still considered "bad practice"?

Body:
Is having mutable local variables in a function that are only used internally, (e.g. the function has no side effects, at least not intentionally) still considered "non functional"?

e.g. in the "Functional programming with Scala" course style check considers any var usage as bad

My question, if the function has no side effects, is writing imperative style code still discouraged?

e.g. instead of using tail recursion with the accumulator pattern, what's wrong with doing a local for loop and creating a local mutable ListBuffer and adding to it, as long as the input is not changed?

If the answer is "yes, they are always discouraged, even if there is no side effects" then what is the reason?


问:
How does Facebook strip html/apostrophes for XSS but also display it?
I'm not quite sure if this is a question for programmers.se rather than stackoverflow, but here goes.  So Facebook [or any other large company] when given something like an apostrophe or html, can strip it of its malicious intent, but still display it properly.  My current sanitizing function in PHP just strips those characters/makes them harmless via htmlentities() and such.  So if I wrote an HTML tag, I would want it to be sanitized but also displayed on the website.  How do I do this?</p>

答:
First, you need to figure out what kind of text your input represents. One possibility is raw text: a letter means a letter, an angle bracket means an angle bracket, a backslash means a backslash, and so on. Or maybe it's text with backslash escapes: a letter means a letter, an angle bracket means an angle bracket, but a backslash starts an escape sequence. Another possibility is HTML markup: a left angle bracket starts an HTML element, an ampersand starts an entity, and so on. There are all sorts of other markup languages: XML, Markdown, bbcode, etc.

Validation: You might get an input that doesn't fit your interpretation, like malformed HTML markup. You should either reject it or automatically correct it. Maybe you only want to allow certain tags in your HTML markup. You should do something with the other tags: delete them, treat them as literal text (ie, display them as a left angle bracket, letters, right angle bracket), reject the input, or whatever. You will probably need to parse the input according to your interpretation in order to validate it; if you try to do it with regular expressions, you will probably get it wrong.

Now that you know what you have (interpretation), and you know that you have it (validation and/or parsing), it should be easy to translate it into whatever you want to render it as. When rendering raw text to HTML, you need to pay attention to the characters that are special to HTML. If you're rendering some wiki markup, you need to map the wiki markup to HTML markup---and again, if the wiki markup can contains characters special to HTML, they need to be escaped. If you're rendering a subset of HTML and you've already validated the input so it contains only the subset you allow, then you're done.

That's the whole process. Now you can look at various libraries and decide whether they do the translation you need. For example, IIUC, htmlentities() renders a string representing raw text to HTML markup. Maybe. Depending on how you wanted to render newlines. Probably the output should be though of as HTML-where-newlines-are-still-significant.

It's easy to deceive yourself into thinking that shortcuts are okay (eg "I'll just remove script tags with a regexp"). Beware.


答:
I would say it is mostly ok. What's more, generating structures this way might be a good way to improve performances in some cases. Clojure has takled this problem by providing Transient data structures.

The basic idea is to allow local mutations in a limited scope, and then to freeze the structure before returning it. This way, your user can still reason about your code as if it were pure, but you are able to perform in place transformations when you need to.

As the link says:

If a tree falls in the woods, does it make a sound? If a pure function
  mutates some local data in order to produce an immutable return value,
  is that ok?


问:
Is it a good practice to create a ClassCollection of another Class?
Lets says I have a Carclass:

```<code>public class Car
{
    public string Engine { get; set; }
    public string Seat { get; set; }
    public string Tires { get; set; }
}
</code>```

Lets say we're making a system about a parking lot, I'm going to use a lot of the Car class, so we make a CarCollection class, it may has a few aditionals methods like FindCarByModel:

```<code>public class CarCollection
{
    public List<Car> Cars { get; set; }

    public Car FindCarByModel(string model)
    {
        // code here
        return new Car();
    }
}
</code>```

If I'm making a class ParkingLot, what's the best practice?

Option #1:

```<code>public class ParkingLot
{
    public List<Car> Cars { get; set; }
    //some other properties
}
</code>```

Option #2:

```<code>public class ParkingLot
{
    public CarCollection Cars { get; set; }
    //some other properties
}
</code>```

Is it even a good practice to create a ClassCollection of another Class?

答: No. The creation of XXXCollection classes pretty much fell out of style with the advent of generics in .NET 2.0. In fact, there is the nifty Cast<T>() LINQ extension that folks use these days to get stuff out of those custom formats.


答:
I think that the only reason to create a special class for holding a collection of other items should be when you add something of value to it, something more than just encapsulate/inherit from an instance of IList or another type of collection.

For example, in your case, adding a function that would return sublists of cars parked on even/uneven lot spaces... And even then... maybe only if it's often reused, because if it only takes one line with a nice LinQ function and is used only once, what's the point ? KISS !

Now, if you plan to offer a lot of sorting/finding methods, then yes, I think this could be useful because this is where they should belong, in that special collection class. This is also a good way for "hiding" the complexities of some "find" queries or whatever you could do in a sorting/finding method.

Title: Is it a good practice to create a ClassCollection of another Class?

Body:
Lets says I have a Carclass:

```<code>public class Car
{
    public string Engine { get; set; }
    public string Seat { get; set; }
    public string Tires { get; set; }
}
</code>```

Lets say we're making a system about a parking lot, I'm going to use a lot of the Car class, so we make a CarCollection class, it may has a few aditionals methods like FindCarByModel:

```<code>public class CarCollection
{
    public List<Car> Cars { get; set; }

    public Car FindCarByModel(string model)
    {
        // code here
        return new Car();
    }
}
</code>```

If I'm making a class ParkingLot, what's the best practice?

Option #1:

```<code>public class ParkingLot
{
    public List<Car> Cars { get; set; }
    //some other properties
}
</code>```

Option #2:

```<code>public class ParkingLot
{
    public CarCollection Cars { get; set; }
    //some other properties
}
</code>```

Is it even a good practice to create a ClassCollection of another Class?


答:
Researching / Reading / Writing code not related to the product development is a waste of resource.

Some programmers and even managers believe in that. Normally, they just do programming based on the knowledge in their heads, and do research and look for answers when they face problems. They don't continuously improve their knowledge proactively. My opinion, we should always keep ourselves up to date, and the knowledge we gathered would be useful to us in solving existing and future problems. Of course, you must allocate your time wisely to do so.

This is also similar to Dan's answer. Some managers just want the developers to quickly dive in and develop the product according to requirements without researching on existing products in the market.

Title: What are the worst false economies in software development?

Body: What are the worst false economies (that is ways of saving money that ultimately cost more than they save) prevalent in the software industry and how do you combat them?</p>


答:
You could download src code of some good framework for this (drupal for example cant use the code but the db is good). What I have always seen is to have separate roles and/or permissions tables. So 1 table has site wide roles, another has permissions (like a admin user who can see reports but not edit users); and the users table has the actual logins.

Another table has the link -> user ids to permissions and another has user ids to roles


答:
Prior to generics in .NET, it was common practice to create 'typed' collections so you would have class CarCollection etc for every type you needed to group. In .NET 2.0 with the introduction of Generics, a new class List<T> was introduced which saves you having to create CarCollection etc as you can create List<Car>.

Most of the time, you will find that List<T> is sufficient for your purposes, however there may be times that you want to have specific behaviour in your collection, if you believe this to be the case, you have a couple of options:

Create a class which encapsulates List<T> for example public class CarCollection { private List<Car> cars = new List<Car>(); public void Add(Car car) { this.cars.Add(car); }}

Create a custom collection public class CarCollection : CollectionBase<Car> {}

If you go for the encapsulation approach, you should at minimum expose the enumerator so you would declare it as follows:

```<code>public class CarCollection : IEnumerable<Car>
{
    private List<Car> cars = new List<Car>();

    public IEnumerator<Car> GetEnumerator() { return this.cars.GetEnumerator(); }
}
</code>```

Without doing that, you can't do a foreach over the collection.

Some reasons you might want to create a custom collection are:

You don't want to fully expose all the methods in IList<T> or ICollection<T>

You want to perform additional actions upon adding or removing an item from the collection

Is it good practice? well that depends on why you are doing it, if it is for example one of the reasons I have listed above then yes.

Microsoft do it quite regularly, here are some fairly recent examples:

System.Web.Http.Filters.HttpFilterCollection

System.Web.Mvc.ModelBinderProviderCollection

As for your FindBy methods, I would be tempted to put them in extension methods so that they can be used against any collection containing cars:

```<code>public static class CarLookupQueries
{
    public static Car FindByLicencePlate(this IEnumerable<Car> source, string licencePlate)
    {
        return source.SingleOrDefault(c => c.LicencePlate == licencePlate);
    }

    ...
}
</code>```

This separates the concern of querying the collection from the class which stores the cars.

Body:
Prior to generics in .NET, it was common practice to create 'typed' collections so you would have class CarCollection etc for every type you needed to group. In .NET 2.0 with the introduction of Generics, a new class List<T> was introduced which saves you having to create CarCollection etc as you can create List<Car>.

Most of the time, you will find that List<T> is sufficient for your purposes, however there may be times that you want to have specific behaviour in your collection, if you believe this to be the case, you have a couple of options:

Create a class which encapsulates List<T> for example public class CarCollection { private List<Car> cars = new List<Car>(); public void Add(Car car) { this.cars.Add(car); }}

Create a custom collection public class CarCollection : CollectionBase<Car> {}

If you go for the encapsulation approach, you should at minimum expose the enumerator so you would declare it as follows:

public class CarCollection : IEnumerable<Car>
{
    private List<Car> cars = new List<Car>();

    public IEnumerator<Car> GetEnumerator() { return this.cars.GetEnumerator(); }
}

Without doing that, you can't do a foreach over the collection.

Some reasons you might want to create a custom collection are:

You don't want to fully expose all the methods in IList<T> or ICollection<T>

You want to perform additional actions upon adding or removing an item from the collection

Is it good practice? well that depends on why you are doing it, if it is for example one of the reasons I have listed above then yes.

Microsoft do it quite regularly, here are some fairly recent examples:

System.Web.Http.Filters.HttpFilterCollection

System.Web.Mvc.ModelBinderProviderCollection

As for your FindBy methods, I would be tempted to put them in extension methods so that they can be used against any collection containing cars:

public static class CarLookupQueries
{
    public static Car FindByLicencePlate(this IEnumerable<Car> source, string licencePlate)
    {
        return source.SingleOrDefault(c => c.LicencePlate == licencePlate);
    }

    ...
}

This separates the concern of querying the collection from the class which stores the cars.


问:
Which technologies to create web interface for scientific instrument?
I am working on software for some scientific instrumentation. An embedded computer running Windows 7 is inside the instrument, which controls various components of the device; cameras, motors, thermometers, etc. I've written a basic interface in WPF/C# and use remote desktop to test the application. (Reason is safety, the instrument will be used in high radiation areas).

What I want:
A server runs on the embedded PC. The user accesses a web page at 192.168.x.x (Internet access would be nice, but not necessary) and clicks 'collect data' and the server loads the relevant C# .dll's, acquires data and uploads the data to the user. Speed/scalability is not much of a concern, as data acquisition could take anywhere from minutes to days.

What I'm confused about:
How do I go about executing my existing C# code from a web application? i.e., which .NET technology(ies) should I look into using?

答:
How do I go about executing my existing C# code from a web
  application? i.e., which .NET technology(ies) should I look into
  using?

If your goal is for your web application (i.e., the application running on the embedded machine) to run your C# code, you would probably want to have an ASP.Net application running on the embedded machine.  Assuming you don't want to run IIS on your embedded machine (because it uses too excessive resources), I suggest using Cassini.

If the C# code is client-side, just give the web application some web services (e.g., using REST or SOAP) and call them from your C# application.


答:
It is perfectly acceptable to name your methods like this:

```<code>MyProcedure()
  MyProcedure_part1()
  MyProcedure_part2()
end;
</code>```

I don't see why anyone would think that these would be called by some other process, and it is perfectly clear what they are for.

Body:
It is perfectly acceptable to name your methods like this:

MyProcedure()
  MyProcedure_part1()
  MyProcedure_part2()
end;

I don't see why anyone would think that these would be called by some other process, and it is perfectly clear what they are for.


问:
Object-Oriented Class Design
I was wondering about good object oriented class design. In particular, I have a hard time deciding between these options:

static vs instance method

method with no parameters or return value vs method with parameters and return value

overlapping vs distinct method functionality

private vs public method

Example 1:

This implementation uses instance methods, with no return value or parameters, with no overlapping functionality, and all methods public

```<code>XmlReader reader = new XmlReader(url);
reader.openUrl();
reader.readXml();
Document result = reader.getDocument();
</code>```

Example 2:

This implementation uses static methods, with return values and parameters, with overlapping functionality and private methods

```<code>Document result = XmlReader.readXml(url);
</code>```

In example one, all methods are public instance, which makes them easy to unit test. Although all methods are distinct, readXml() is dependent on openUrl() in that openUrl() must be called first. All data is declared in instance fields, so there's no return values or parameters in any method, except in the constructor and accessors.

In example two, only one method is public, the rest are private static, which makes them hard to unit test. Methods are overlapping in that readXml() calls openUrl(). There's no fields, all data is passed as parameters in methods and the result is returned immediately.

What principles should I follow to do proper object oriented programming?

答:
static vs instance method

In practice you'll find static methods are generally confined to utility classes and shouldn't be cluttering your domain objects, managers, controllers or DAOs. Static methods are most useful when all the necessary references can reasonably be passed in as parameters and profide some functionality that can be reused across many classes. If you find yourself using a static method as a workaround for having a reference to an instance object, ask yourself why you don't just have that reference instead.

method with no parameters or return value vs method with parameters and return value

If you don't need parameters on the method, don't add them. Same goes with the return value. Keeping this in mind will simplify your code and ensure you aren't coding for a multitude of scenarios which never end up happening.

overlapping vs distinct method functionality

It's a good idea to try to avoid overlapping functionality. Sometimes it can be difficult, but when a change in logic is necessary, it's a lot easier to change one method which is reused, than changing a whole bunch of methods with similar functionality

private vs public method

Commonly getters, setters and constructors should be public. Everything else you'll want to try to keep private unless there is a case where another class needs to execute it. Keeping methods defaulting to private will help maintain Encapsulation. Same goes for fields, get used to private as default

Title: Object-Oriented Class Design


答:
The problem isn't mutability per se, it's a lack of referential transparency.

A referentially transparent thing and a reference to it always have to be equal, so a referentially transparent function will always return the same results for a given set of inputs and a referentially transparent "variable" is really a value rather than a variable, since it can't change.  You can make a referentially transparent function that has a mutable variable inside; that isn't a problem.  It might be harder to guarantee the function is referentially transparent, though, depending on what you're doing.

There's one instance I can think of where mutability has to be used to do something that is very functional: memoization.  Memoization is caching values from a function, so they don't have to be recomputed; it's referentially transparent, but it does use mutation.

But in general referential transparency and immutability go together, other than a local mutable variable in a referentially transparent function and memoization, I'm not sure there are any other examples where this is not the case.


问:
Is the algorithm more important than the programming language?
During the current (2013) Google Code Jam contest, there was a problem that took C++ and Java people 200+ lines of code as compared to Python people that solved the same problem only using 40 lines of code.

Python is not directly comparable with C++ and Java but the difference in verbosity I thought might perhaps have an influence on the efficiency of the algorithm.

How important is knowing the right algorithm compared to the choice of language? Could an excellently implemented Python program be implemented in C++ or Java in a better way (using the same algorithm) and does this have any relation to the natural verbosity of certain programming languages?

答:
Language matters.

DARPA and the US Navy did a shootout experiment almost 20 years ago.  The dark horse runaway winner was Haskell.  Ada and C++ were both represented; Java was not.

Around the same time, Pratt & Whitney did a data mining study on jet engine controller projects, looking at timecard and bug tracker data.  They discovered that Ada gave double the programmer productivity and 1/4 the defect density of any other language they were using.

Atari used to use FORTH to develop videogames, and the fact that they were using FORTH was considered extremely proprietary.

Paul Graham's comments on using LISP are well-known.  Erann Gat's comments on LISP at JPL are equally cogent, although not as well-known.

The Boeing 777 avionics software is pretty much all Ada.  Their experience was very good, even though one major subcontractor had to start over in mid-stream.

Language matters.


问:
How do you stay motivated for hobby projects?
I started seriously programming as a hobbiest, student and then intern about 4 years ago and I've always done small projects on the side as a learning exercise.  Schools over now though, and I spend my days at work as a software developer. I would still love to do projects on the side to learn about areas in computer science that I'm not exposed to at work, but I've noticed that after 8 hours of starring at an IDE it's far to tempting to veg out.  Any time I do get up the gumption to work on something for a few hours lately it's gotten left by the wayside.

Anyone have any advice for sticking with side projects when you spend most of your day coding?

答: Try to earn money with it.  Even if you don't make much, hopefully it'll be enough to subsidize the cost of your hobby.</p>


问:
Relation of CPU Hz's and network speed in clusters
In cluster the Amdahl's law, Gustafson's law exists.

I though that there might be some law which states the relation between CPU Hz's and network speed: maximum network speed after which no additional Mbps would increase the calculations. Does such limits exists and what it depends on?

答:
For an algorithm with very little communication between computers (not sensitive to bandwidth limitations) that uses asynchronous communication (not sensitive to network latency), number of CPUs will dominate performance and networking is mostly irrelevant.

For an algorithm with high communication between computers (very sensitive to bandwidth limitations) that uses synchronous communication (very sensitive to network latency), number of CPUs isn't going to matter much.

In practice, all algorithms fit between these extremes somewhere.

Amdahl's law applies to these algorithms. It says (paraphrased as I'm lazy) "the total time is the time taken for the part that can't be done in parallel, plus the part that can be done in parallel divided by the number of CPUs". This ignores all networking/communication delays.

It would be trivial to amend Amdahl's law to take into account the networking/communication delays. For example: "the total time is the time taken for the part that can't be done in parallel, plus networking delays (telling other CPUs what to do), plus the part that can be done in parallel divided by the number of CPUs, plus networking delays (getting results back from other CPUs)". However note that this example only works for a certain style of networking (request and reply) and won't work for other cases (e.g. if the work is pipelined).

The problem is that software is typically not one algorithm - software is a collection of many algorithms. The "amended Amdahl's law" ends up being too complex (as different things can overlap with networking/communication delays; different communication styles can be in use for different pieces; it depends on timing and bandwidth and latency, etc).

At the end of the day; it's more practical to write a prototype/simulation and measure actual behaviour (and find bottlenecks, and improve/exchange algorithms, until you can't find a way of improving it further and give up and claim you've found an answer).


问:
Programming by Intention, Depth-First or Breadth-First?
Say I have the following graph of dependencies between procedures/functions/methods:

```<code>      o
    /   \
  v       e
 / \     / \
r   f   l   w
</code>```

That is, function o first calls function v, and then function e. Function v itself first calls function r, and then function f. Function e itself first calls function l, and then function w.

With Programming by Intention, in which order should these functions be defined?

Depth-First: o, v, r, f, e, l, w

Breadth-First: o, v, e, r, f, l, w

答:
I don't know if this is actually specified in programming by intention, but maybe we can analyse pros and cons.

In prolog, a 5th generation programming language, you can use both forms of execution, thus from a pseudocode point of view both could be valid.

Breadth-First has the advantage that it can be read in a top-down fashion, if your functions vary widely in abstraction with each level then it may be possible to skip one level. Breadth-first would be the way to go.

However, if someone reading this has to read everything, or to avoid making assumptions about that, then depth-first would be better. It allows reading the code in the same way it is executed and makes reading it more easy and natural, think about the example of alligator eggs ;)

The best option is to have some tool that allows to sort everything in a depth-first fashion and collapse inner elements. I've seen that in many code editors and in the html generated by FreeMind. That allows the reader to choose the direction to continue, either breadth-first by collapsing and scrolling or depth-first by expanding and scrolling.

In general, depth-first is a much safer option.


答:
The best tool I know for these sort of problems is an extension of Valgrind called Helgrind.

Basically Valgrind simulates a virtual processor and runs your binary (unmodified) on top of it, so it can check every single access to memory. Using that framework, Helgrind watch system calls to infer when an access to a shared variable is not properly protected by a mutual exclusion mechanism. That way it can detect a theorical race condition even if it has not actually happened.

Intel sells a very similar tool called Intel Inspector.

These tools give great results but your program will be considerably slower during analysis.

Title: Testing multi-threaded race conditions


问:
Is it OK to split long functions and methods into smaller ones even though they won't be called by anything else?
Lately I've been trying to split long methods into several short ones.

For example: I have a process_url() function which splits URLs into components and then assigns them to some objects via their methods. Instead of implementing all this in one function, I only prepare the URL for splitting in process_url(), and then pass it over to process_components() function, which then passes the components to assign_components() function.

At first, this seemed to improve readability, because instead of huge 'God' methods and functions I had smaller ones with more descriptive names.
However, looking through some code I've written that way, I've found that I now have no idea whether these smaller functions are called by any other functions or methods.

Continuing previous example: someone looking at the code might think that process_components() functionality is abstracted into a function because it's called by various methods and functions, when in fact it's only called by process_url().

This seems somewhat wrong. The alternative is to still write long methods and functions, but indicate their sections with comments.

Is the function-splitting technique I described wrong? What is the preferred way of managing large functions and methods?

UPDATE: My main concern is that abstracting code into a function might imply that it could be called by multiple other functions.

SEE ALSO: discussions on reddit at /r/programming (provides a different perspective rather than most of the answers here) and /r/readablecode.

答:
I've never taken issue with other developers splitting larger methods into smaller methods as it's a pattern that I follow myself. The "God" method is a terrible trap to fall into and others who are less experienced or simply don't care tend to get caught more often than not. That being said...

It's incredibly important to use appropriate access identifiers on the smaller methods. It's frustrating to find a class littered with small public methods because then I totally lose confidence finding where/how the method is used throughout the application.

I live in C#-land so we have public, private, protected, internal, and seeing those words shows me beyond a shadow of a doubt the scope of the method and where I must look for calls. If it's private, I know the method is used in only one class and I have full confidence when refactoring.

In the Visual Studio world, having multiple solutions (.sln) exacerbates this anti-pattern because IDE/Resharper "Find Usages" helpers will not find usages outside of the open solution.


问:
Not using SQL Transactions
I am looking at a codebase where a developer never uses SQL transactions i.e. each update/insert into the SQL database is an atomic operation.

I believe the codebase would benefit from transactions.  I was looking online today and in one Stackoverflow question an answerer says: "In some situations it is better to have a partial update rather than none at all".  Is there any truth in this? I suppose it depends on the problem domain to some extent.

答:
It depends not just on the problem domain, but also on technical considerations. Basically, it comes down to the simple question: "Will there be unacceptable problems when a partial update happens?" Some examples:

Transfering money from one account to another. A partial update is completely unacceptable for domain reasons.

Adding a vote to an online poll and incrementing a result count (which is only for display, with the final result being recalculated from the vote table). A partial update is probably acceptable.

Adding something to an n:m relationship. A partial update would result in an inconsistent state where you have either an entry that is not connected, or a connection without a matching entry. This is unacceptable for technical reasons - you'd get an error whenever that entry is encountered and it would likely be impossible to fix this error within the system.

A hardware unit that sends batches of sensor data to be stored. Adding only some of the entries of a batch is probably better than losing all of it.

Honestly, it's really rare for partial updates to be tolerable.

Title: Not using SQL Transactions


答:
Everything old is new again. The folks who brought us the eXtreme Programming books were, among other things, promoting a set of best practices in software development. One of them, which they termed “Programming by Intention”, was not actually new, but was something that had been a very common coding technique in languages like COBOL and Smalltalk (usually called “top down” programming) years before.

(from the introduction of Programming by Intention)

Programming by Intention appears to be a rediscovery of top down programming where one starts with the top layer and goes down from there.

This was the way that code was written in the days of Pascal and its contemporaries.  You started writing the main procedure (it happened to be located at the end of the file, by necessities of the language definition and compiler) and then write a procedure or function, which was then located before the code that called it.

This contrasts with the 'bottom up design' model that OO emphasizes - building the building blocks first, and then building them up from there.

It would be a misconception and misapplication of Programming by Intention to use this to lay a structural framework to the code.  Unless the language defines it (a la pascal), it doesn't matter what order the methods are declared in.  Readability (and consistency between programmers) is key.

With modern IDEs, one can see the outline of the code quickly and easily.  Hitting F3 in eclipse will open up the definition of a method call no matter where it is located in the code.  To this extent, the organization of the methods is unimportant.  If there is an order use the following guideline:

Program within the language constraints.

Program within the language conventions.

Program within the team's style constraints.

Program consistently.

If you order top down or bottom up or depth first or breath first or alphabetical, do it.  And stick with it.

Title: Programming by Intention, Depth-First or Breadth-First?

Body:
Say I have the following graph of dependencies between procedures/functions/methods:

```<code>      o
    /   \
  v       e
 / \     / \
r   f   l   w
</code>```

That is, function o first calls function v, and then function e. Function v itself first calls function r, and then function f. Function e itself first calls function l, and then function w.

With Programming by Intention, in which order should these functions be defined?

Depth-First: o, v, r, f, e, l, w

Breadth-First: o, v, e, r, f, l, w


问:
Unit Test code generation
We have a project that was written over a period of 2 years, but has a poorly designed architecture and not unit tests.

The software works well, but we're at the point where we want to refactor some core modules.

The budget is also limited so we can not hire a sufficient number of developers to write unit tests.

Would generating unit test code automatically to cover (as an example) integration scenarios via some too be a viable strategy? An assumpion is that considering the system works fine right now, and the its output can be converted into XML data for unit testing?

This approach gives us a possibility of quickly starting to refactor existing code and receieve immediate feedback if some core functionality is broken because of those changes.

答:
I never heard about unit test generation. If it was possible, it would be used by everyone. If something exists, I'm sure that it can't give you the guarantee you that your code is properly covered.

I think that the best approach for this kind of legacy code is to write test for any new feature and any new refactoring. The first steps will be quite dangerous for the application stability but you will be soon the owner of a relatively large set of tests.

This way could be really efficient because, in a first time, you will be in front of the worst parts of the code (for necessary refactoring) and the core features (because they will be impacted by any evolution). Like that, the most critic sections of your application will be covered quickly.

You will not achieve a complete covering but if you never need to go back into an old piece of code it probably means that this code has a weak granularity with the rest.

Title: Unit Test code generation


问:
What guidelines should I follow while designing a library?
I am working on a Arduino project using the Uno. The project contains a significant amount of code. I would like to create a library and I may even share it later on. What guidelines should I follow while designing the library?</p>

答:
There are a lot of points that you should keep in mind while designing a library. As you will probably, end up sharing your work with others, it is extremely important to  follow consistent design patterns. Keep in mind that other users will be of extremely varying skill levels, so design an easy to use library, to the maximum extent possible.

Basic Tips

Pin Map

Provide a basic pin map that your library is expecting. Do not keep the pin mapping static but allow the user to make changes easily.

Working Library

One of the first things that you should try to ensure is that your library is working. If it isn't, clearly state so. You wouldn't want to end up wasting your time trying to work with broken software, so dont let others do so as well.

Basic Readme

Mention very clearly which board(s) the library was designed for, on which it was tested, and what board(s) is it expected to work. Specify the generation (version) of every board mentioned here.

Interfaces

The next thing is that you should have clearly defined interfaces. A working library with convoluted interfaces is frustrating. This will help you yourself use the library later on, and will make things easier for other users. This should be one of the most important aspects to keep in mind.

Whether you follow a top-down or a bottom-up approach, the interfaces should always be clear in your mind. In a bottom-up approach, this may and will be hard, but it should not be ignored. Otherwise, you will end up with a overly complex library which may not be usable.

Special Functions

If you have any functions that use some special board characteristics, make sure to make these functions stand out, mention in the readme and also in the comments.

Busy Waits

There might be scenarios where you might have to use a busy wait. Such functions, depending on program logic, may prevent normal control flow thus causing trouble while in the middle of a critical task. Try to use interrupts or other algorithms, if possible. If not, then clearly mention mark any such functions.

Comments

Make sure to keep commenting every small and big change you make. Write nice long comments for all of the critical functions and smaller ones for others. Explicitly describe your interface, each argument, what it does, and what it returns. This is a lot of extra work, but will be immensely helpful for both you and others. If you have any functions that may not work across different board(s), mention it here. If these are intermediate functions being used by other functions and may be necessary, mention in the Readme.

Consistency

Make sure everything, even the comments, are consistent across the .h and .cpp files.

Keep only related functions within a single file. Having a few small, but logically consistent, modules is better than one huge file with everything in it.

Advanced Tips

Detailed Readme

Write a clear readme file describing the library, its abilities, any problems or bugs, and basic usability. Use a separate file for defining and explaining each interface as described above.

Directory Structure

Once the library becomes large dividing it into directories may be needed. This is not easily possible while using the arduino-ide. But, if you reached this far, you probably are an advanced Arduino user and use more powerful development tools. If not, this is the universe telling you to do so.

Licensing

Be sure to add a license.

Version Control

Use a VCS tool such as Git or SVN. This will make it much easier to see changes made, revert to old versions, spot error causing code, and even collaborate with others.

OwnerDisplayName: anon

OwnerDisplayName: AshRj


答:
AshRj's answer is very good - I have just 2 points to add to it.

Point 1: Documentation

AshRj recommended to write a detailed readme. Whilst this can be good practice, it can quickly get out of hand with larger libraries - indeed, even at a few thousand lines (which is really not much), a readme will have almost no benefit. My recommendation would be to go one step further and use the equivalent of Javadocs for C++ - as this answer explains (it's on Stack Overflow), Doxygen is a very useful tool for keeping documentation manageable and to hand (no-one wants to edit a 10K line readme file...)

Point 2: Directories

Again contrasting against AshRj's answer, always use directories. Even if you only have 10 files, maybe even with just 7 or 8. I know it sounds a bit stupid, but it's future-proofing your work, and if you don't start early you'll just end up with a mess of files. Directories aren't just for large projects - small ones should use them as well. Just remember that in C++ (the de-facto Arduino language), directories are ignored when including files from a library - they exist as a code-management tool only.

Title: What guidelines should I follow while designing a library?

Body: I am working on a Arduino project using the Uno. The project contains a significant amount of code. I would like to create a library and I may even share it later on. What guidelines should I follow while designing the library?</p>

OwnerDisplayName: Polar


答:
What do you use to stimulate the unit test generator?

Consider:

```<code>int foo(int x, int y)
{
   return (x + y);
}
</code>```

Your unit test generator can certainly generate code to test the addition, but it has no way of knowing that what you really wanted was a multiplication.  I saw something like that bite a co-worker of mine really badly several years ago.  We were using Ada, doing operator overloading to impose dimensional analysis on our arithmetic.  He cut-and-pasted the rename for "+" four times, and changed the operator on the left, but not on the right.  It cost him some hair-tearing to figure out why his multiply routine was returning sums instead of products.  (Doing this protects you against trying to add a velocity and a distance, which is by definition meaningless.)

Title: Unit Test code generation

Body:
We have a project that was written over a period of 2 years, but has a poorly designed architecture and not unit tests.

The software works well, but we're at the point where we want to refactor some core modules.

The budget is also limited so we can not hire a sufficient number of developers to write unit tests.

Would generating unit test code automatically to cover (as an example) integration scenarios via some too be a viable strategy? An assumpion is that considering the system works fine right now, and the its output can be converted into XML data for unit testing?

This approach gives us a possibility of quickly starting to refactor existing code and receieve immediate feedback if some core functionality is broken because of those changes.

Body:
What do you use to stimulate the unit test generator?

Consider:

int foo(int x, int y)
{
   return (x + y);
}

Your unit test generator can certainly generate code to test the addition, but it has no way of knowing that what you really wanted was a multiplication.  I saw something like that bite a co-worker of mine really badly several years ago.  We were using Ada, doing operator overloading to impose dimensional analysis on our arithmetic.  He cut-and-pasted the rename for "+" four times, and changed the operator on the left, but not on the right.  It cost him some hair-tearing to figure out why his multiply routine was returning sums instead of products.  (Doing this protects you against trying to add a velocity and a distance, which is by definition meaningless.)


答:
1. Static vs. instance

I think there are very clear guidelines about what is good OO design and what isn't. The
problem is that the blogosphere makes it hard to separate the good from the bad and ugly. You can find some kind of reference supporting even the worst practice you can think of.

And the worst practice I can think of is Global State, including the statics you mentioned and everyone's favorite Singleton. Some excerpts from Misko Hevery's classic article on the subject.

To really understand the dependencies, developers must read every line of code. It causes Spooky Action at a Distance: when running test suites, global state mutated in one test can cause a subsequent or parallel test to fail unexpectedly. Break the static dependency using manual or Guice dependency injection.

Spooky Action at a Distance is when we run one thing that we believe is isolated (since we did not pass any references in) but unexpected interactions and state changes happen in distant locations of the system which we did not tell the object about. This can only happen via global state.

You may not have thought of it this way before, but whenever you use static state, you’re creating secret communication channels and not making them clear in the API. Spooky Action at a Distance forces developers to read every line of code to understand the potential interactions, lowers developer productivity, and confuses new team members.

What this boils down to is that you should not supply static references to anything that has some sort of stored state. The only place I use statics is for enumerated constants, and I have misgivings about even that.

2. Methods with input parameters and return values vs. methods with none

The thing you need to realize is that methods that have no input parameters and no output parameters are pretty much guaranteed to be operating on some sort of internally stored state (otherwise, what are they doing?). There are entire languages that are built on the idea of avoiding stored state.

Any time you have stored state, you have the possibility for side-effects, so make sure that you always use it mindfully. This implies that you should prefer functions with defined inputs and/or outputs.

And, in fact, functions that have defined inputs and outputs are much easier to test--you don't have to run a function here and go look over there to see what happened, and you don't have to set a property somewhere else before you run the function under test.

You can also safely use this type of function as statics. However, I wouldn't, because if I then later wanted to use a slightly different implementation of that function somewhere, rather than providing a different instance with the new implementation, I'm stuck with no way to replace the functionality.

3. Overlapping vs. Distinct

I don't understand the question. What would the advantage be in 2 overlapping methods?

4. Private vs. Public

Don't expose anything you don't need to expose. However, I'm not a big fan of private, either. I'm not a C# developer, but an ActionScript developer. I've spent a lot of time in Adobe's Flex Framework code, which was written circa 2007. And they made some really bad choices of what to make private, which makes it kind of a nightmare trying to extend their Classes.

So unless you think you're a better architect than the Adobe developers circa 2007 (from your question, I'd say you have a few more years before you have a chance to make that claim), you probably want to just default to protected.

There are some problems with your code examples which mean that they're not well-architected, so it's not possible to pick A or B.

For one thing, you probably should separate your object creation from its use. So you usually wouldn't have your new XMLReader() right next to where it is used.

Also, as @djna says, you should encapsulate the methods used in your XML Reader uses, so your API (instance example) might be simplified to:

```<code>_document Document = reader.read(info);
</code>```

I don't know how C# works, but since I've worked with a number of web technologies, I'd be suspicious that you wouldn't always be able to return an XML document immediately (except maybe as a promise or future type object), but I can't give you advice on how to handle an Asynchronous load in C#.

Note that with this approach, you could create several implementations that can take a parameter that tells them where/what to read and return an XML object, and swap them based on your project needs. For example, you might be reading directly from a database, from a local store, or, as in your original example, from a URL. You can't do that if you use a static method.

Title: Object-Oriented Class Design

Body:
I was wondering about good object oriented class design. In particular, I have a hard time deciding between these options:

static vs instance method

method with no parameters or return value vs method with parameters and return value

overlapping vs distinct method functionality

private vs public method

Example 1:

This implementation uses instance methods, with no return value or parameters, with no overlapping functionality, and all methods public

```<code>XmlReader reader = new XmlReader(url);
reader.openUrl();
reader.readXml();
Document result = reader.getDocument();
</code>```

Example 2:

This implementation uses static methods, with return values and parameters, with overlapping functionality and private methods

```<code>Document result = XmlReader.readXml(url);
</code>```

In example one, all methods are public instance, which makes them easy to unit test. Although all methods are distinct, readXml() is dependent on openUrl() in that openUrl() must be called first. All data is declared in instance fields, so there's no return values or parameters in any method, except in the constructor and accessors.

In example two, only one method is public, the rest are private static, which makes them hard to unit test. Methods are overlapping in that readXml() calls openUrl(). There's no fields, all data is passed as parameters in methods and the result is returned immediately.

What principles should I follow to do proper object oriented programming?

Body:
1. Static vs. instance

I think there are very clear guidelines about what is good OO design and what isn't. The
problem is that the blogosphere makes it hard to separate the good from the bad and ugly. You can find some kind of reference supporting even the worst practice you can think of.

And the worst practice I can think of is Global State, including the statics you mentioned and everyone's favorite Singleton. Some excerpts from Misko Hevery's classic article on the subject.

To really understand the dependencies, developers must read every line of code. It causes Spooky Action at a Distance: when running test suites, global state mutated in one test can cause a subsequent or parallel test to fail unexpectedly. Break the static dependency using manual or Guice dependency injection.

Spooky Action at a Distance is when we run one thing that we believe is isolated (since we did not pass any references in) but unexpected interactions and state changes happen in distant locations of the system which we did not tell the object about. This can only happen via global state.

You may not have thought of it this way before, but whenever you use static state, you’re creating secret communication channels and not making them clear in the API. Spooky Action at a Distance forces developers to read every line of code to understand the potential interactions, lowers developer productivity, and confuses new team members.

What this boils down to is that you should not supply static references to anything that has some sort of stored state. The only place I use statics is for enumerated constants, and I have misgivings about even that.

2. Methods with input parameters and return values vs. methods with none

The thing you need to realize is that methods that have no input parameters and no output parameters are pretty much guaranteed to be operating on some sort of internally stored state (otherwise, what are they doing?). There are entire languages that are built on the idea of avoiding stored state.

Any time you have stored state, you have the possibility for side-effects, so make sure that you always use it mindfully. This implies that you should prefer functions with defined inputs and/or outputs.

And, in fact, functions that have defined inputs and outputs are much easier to test--you don't have to run a function here and go look over there to see what happened, and you don't have to set a property somewhere else before you run the function under test.

You can also safely use this type of function as statics. However, I wouldn't, because if I then later wanted to use a slightly different implementation of that function somewhere, rather than providing a different instance with the new implementation, I'm stuck with no way to replace the functionality.

3. Overlapping vs. Distinct

I don't understand the question. What would the advantage be in 2 overlapping methods?

4. Private vs. Public

Don't expose anything you don't need to expose. However, I'm not a big fan of private, either. I'm not a C# developer, but an ActionScript developer. I've spent a lot of time in Adobe's Flex Framework code, which was written circa 2007. And they made some really bad choices of what to make private, which makes it kind of a nightmare trying to extend their Classes.

So unless you think you're a better architect than the Adobe developers circa 2007 (from your question, I'd say you have a few more years before you have a chance to make that claim), you probably want to just default to protected.

There are some problems with your code examples which mean that they're not well-architected, so it's not possible to pick A or B.

For one thing, you probably should separate your object creation from its use. So you usually wouldn't have your new XMLReader() right next to where it is used.

Also, as @djna says, you should encapsulate the methods used in your XML Reader uses, so your API (instance example) might be simplified to:

_document Document = reader.read(info);

I don't know how C# works, but since I've worked with a number of web technologies, I'd be suspicious that you wouldn't always be able to return an XML document immediately (except maybe as a promise or future type object), but I can't give you advice on how to handle an Asynchronous load in C#.

Note that with this approach, you could create several implementations that can take a parameter that tells them where/what to read and return an XML object, and swap them based on your project needs. For example, you might be reading directly from a database, from a local store, or, as in your original example, from a URL. You can't do that if you use a static method.