问:
How easy is it to hack JavaScript (in a browser)?
My question has to do with JavaScript security.

Imagine an authentication system where you're using a JavaScript framework like Backbone or AngularJS, and you need secure endpoints. That's not a problem, as the server always has the last word and will check if you're authorized to do what you want.

But what if you need a little security without involving the server? Is that possible?

For example, say you've got a client-side routing system and you want a concrete route to be protected for logged-in users. So you ping the server asking if you're allowed to visit protected routes and you go on. The problem is that when you ping the server, you store the response in a variable, so the next time you go to a private route, it will check that if you're already logged in (no ping to the server), and depending on the response it will go or not.

How easy is for a user to modify that variable and get access?

My security (and JavaScript) knowledge isn't great. But if a variable is not in global scope and is in the private part of a module pattern which only have getters but not setters, even in that case, can you hack the thing out?

答:
Speaking specifically about Angular:

Protecting a route client side just doesn't exist. Even if you 'hide' a button to that route the user can always type it in, Angular will complain, but the client can code around that.

At some point your controller is going to have to ask your server for the data needed to render the view, if the user isn't authorized to access that data, they won't receive it as you have protected this data on the server side, and your Angular app should handle the 401 appropriately.

If you're trying to protect the the raw data, you can't client side, if you only want a particular user to only be able to view common data a certain way, create the data 'views' on the server rather than sending raw data to the client and having it reorganize it (you should already doing this for performance reasons anyway).

Side note: there should never be anything sensitive built into view templates that Angular requests, just don't do it.  If for some crazy reason there is, you need to protect those view templates on the server side, just as you would if you were doing server side rendering.


问:
Should I let my "beginnerishness" hold me back? (not rhetorical)
My situation is that I've been programming for about three years and have learned a lot but still have a long long way to go.

I have an idea for an app that I believe would take off, but I'm afraid to build it because it involves architecting a back-end with databases and authenticated payment transactions and user accounts and I've never done any of that.

I'm afraid that if I hacked it together, people would use it and things would go wrong-- security flaws, data loss, somehow I accidentally drop_table and lose everyone's payment history, etc, because I'm not sufficiently experienced with industry practices in terms of data management, versioning, hosting transitions, and plain old coding.

But I really want to build this.

Has anyone every been in a similar situation, and do you have any advice?

答:
Don't Let Fear of Failure Hold You Back

Be brave, and don't hold yourself with fears that you don't have experience to build your idea. Definitely, there are hoops, compliance and security that you would need to consider for a growing app, but you will figure out solution once you started to research. Just don't let your fears push you back.

In terms of compliance and PCI, as mentioned previously, there are services that you may use. Azure, Amazon, Vault services, etc. are some of them.

I have tried Windows Azure Services to handle credit card transactions and it worked for me. You may get more info from a nicely compiled article - PCI Compliant Credit Card Handling on Windows Azure.

Processing credit card transactions on Azure

Don’t Let Your Fear Hold You Back - for nerds

What Is Holding You Back?

ContentLicense: CC BY-SA 3.0


问:
Java Heap Allocation Faster than C++
I already posted this question on SO and it did ok. It was unfortunately closed though(only needs one vote to reopen) but someone suggested I post it on here as it is a better fit so the following is literally a copy paste of the question

I was reading the comments on this answer and I saw this quote.

Object instantiation and object-oriented features are blazing fast to use (faster than C++ in many cases) because they're designed in from the beginning. and Collections are fast. Standard Java beats standard C/C++ in this area, even for most optimized C code.

One user (with really high rep I might add) boldly defended this claim, stating that

heap allocation in java is better than C++'s

and added this statement defending the collections in java

And Java collections are fast compared to C++ collections due largely to the different memory subsystem.

So my question is can any of this really be true, and if so why is java's heap allocation so much faster.

答:
This is not a scientific claim. I am simply giving some food for thought on this issue.

One visual analogy is this: you are given an apartment (a residential unit) which is carpeted. The carpet is dirty. What is the fastest way (in terms of hours) to make the apartment's floor sparkling clean?

Answer: simply roll up the old carpet; throw away; and roll out a new carpet.

What are we neglecting here?

The cost of moving out existing personal belongings and then moving in.

This is known as the "stop-the-world" cost of garbage collection.

The cost of the new carpet.

Which, coincidentally for RAM, it is free.

Garbage collection is a huge topic and there are plenty of questions both in Programmers.SE and StackOverflow.

On a side issue, a C/C++ allocation manager known as TCMalloc together with object reference counting is theoretically able to meet the best performance claims of any GC system.

Title: Java Heap Allocation Faster than C++


问:
Is "pass by value" synonymous with "functional programming"
Is "pass by value" synonymous with "functional programming" ?

Are there any iterative programming languages that are (largely) "pass by value"?

Are there any functional programming languages that are (largely) "pass by reference"?

Thanks.

答:
C is an imperative language that is pass by value. Fortran is an imperative language that is pass by reference. C# supports both functional programming (lambdas, etc.) and pass by reference.

Therefore, the answer is no, they are not synonymous.


问:
Do sigils make source code easier to read?
In most programming languages, variables do not have identifying characters like they do in PHP. In PHP you must prefix a variable with the $ character.

Example;

```<code> var $foo = "something";
 echo $foo;
</code>```

I'm developing a new scripting language for a business application, and my target users do not have a programming background. Do these characters make code easier to read and use?

One reason PHP uses the $ is because without it PHP can't tell if a name is a function reference or variable reference. This is because the language allows strange references to functions. So the $ symbol helps the parser separate the namespace.

I don't have this problem in my parser. So my question is purely on readability and ease of use. I have coded for so many years in PHP that when I see $foo it's easy for me to identify this as a variable. Am I just giving a bias preference to this identifier?

答:
After all this answers, I want give some more points to Mathew Foscarini.

You consider problem now as a "language constructor". You try understand why another language have this or that feature to choose if to use something in your own language. I am in same position many years, because developing SQL parser for our Valentina Database.

I advice you take a look on antlr.org and even read book from Terence. It has a lots nice things for language developers.

I still not agree to "reasons" exposed by other answers. They assume that PHP author in head have decide to use $ to be able use reserved keywords and Better distinguish variables from non-variables. I do not think so ... although prove can be only his/their own story.

Most probably that they just follow to perl and more early languages. As underlines Terrence, most languages are similar, especially in LEXER part. And usually constructor of a new language can just choose what kind of language he is going to develop and then take lexer of that language grammar. And this is what you should do now. No need invent from scratch. And I bet the same did PHP authors.

All else what people mention:

distinguish variables from non-variables

reserver words as variable names

ability to place variable inside of string

may be else (I am not big expert in PHP)

are side-effects of this LEXER, because it is able to recognize a token.

Take as example: in SQL we use "" to be able to use identifiers with reserved words and even identifiers with spaces "First Name", "Group Name". GROUP is a keyword. There was problem - there was special solution.

P.S. Very good comment from MichaelT.


答:
Is "pass by value" synonymous with "functional programming" ?

No, They are not just "not synonymous", they are completely and utterly unrelated. They have absolutely nothing whatsoever to do with each other.

Are there any iterative programming languages that are (largely) "pass by value"?

I am assuming you mean imperative programming languages. Yes, there are imperative programming languages which are pass by value. In fact, there are very few which aren't: C, Java, JavaScript, Python, Ruby, pretty much all Lisps, Smalltalk, Self, Newspeak, Io, Ioke, Seph, are just some of the examples. They are not just "largely" pass by value, they are pass by value. Period. No exceptions, no ifs, no buts.

C++, C#, Visual Basic.NET, PHP are pass by value by default, you need to explicitly ask for pass by reference, e.g. using the ref keyword in C#.

Are there any functional programming languages that are (largely) "pass by reference"?

The difference between pass by value and pass by reference can only be observed when the reference is mutated. Functional programming languages don't allow mutation, ergo it is impossible to tell whether a functional programming language is pass by value or pass by reference. Language implementors make use of this by choosing either one of the two strategies depending on which one yields better performance for a particular piece of code.

For example, large objects will be passed by reference, but smaller objects where the object itself is as big as a reference or smaller (such as e.g. integers in a tagged pointer representation) will be passed by value.

Note also that both pass by value and pass by reference are eager evaluation strategies, whereas some of the more popular functional programming languages (e.g. Haskell, Frege) have non-strict semantics and thus are usually implemented using some form of lazy evaluation. Some of the more popular Haskell compilers for example use call by need with limited sharing.

Body:
Is "pass by value" synonymous with "functional programming" ?

Are there any iterative programming languages that are (largely) "pass by value"?

Are there any functional programming languages that are (largely) "pass by reference"?

Thanks.


答:
This probably won't be a popular view but my opinion is don't do it. Or at least don't do it alone.

Security is very difficult to get it right. Even seasoned developers will often introduce subtle security flaws if they have not actively considered all the possible ramifications of a particular line of code. User authentication alone is very difficult to get it right.

If you insist on going ahead with this, I strongly suggest looking into existing libraries to help you with the security stuff. Most languages and frameworks have excellent authentication and authorization libraries. Any halfway decent framework will aid you in sanitizing your input. And for god's sake, don't roll your own cryptography schemes. IT Security stackexchange is an excellent resource for this sort of stuff but be prepared to spend a lot of time on getting things right.

You should also look into getting your code reviewed by experts especially if it involves important stuff like money or PII. Definitely think ahead about any possible compliance issues such as PCI.

Title: Should I let my "beginnerishness" hold me back? (not rhetorical)

Body:
My situation is that I've been programming for about three years and have learned a lot but still have a long long way to go.

I have an idea for an app that I believe would take off, but I'm afraid to build it because it involves architecting a back-end with databases and authenticated payment transactions and user accounts and I've never done any of that.

I'm afraid that if I hacked it together, people would use it and things would go wrong-- security flaws, data loss, somehow I accidentally drop_table and lose everyone's payment history, etc, because I'm not sufficiently experienced with industry practices in terms of data management, versioning, hosting transitions, and plain old coding.

But I really want to build this.

Has anyone every been in a similar situation, and do you have any advice?


问:
Placeholders in strings
I find that I sometimes use placeholders in strings, like this:

```<code>$ cat example-apache
<VirtualHost *:80>
    ServerName ##DOMAIN_NAME##
    ServerAlias www.##DOMAIN_NAME##
    DocumentRoot /var/www/##DOMAIN_NAME##/public_html
</VirtualHost>
</code>```

Now I am sure that it is a minor issue if the placeholder is ##DOMAIN_NAME##, !!DOMAIN_NAME!!, {{DOMAIN_NAME}}, or some other variant. However, I now need to standardize with other developers on a project, and we all have a vested interest in having our own placeholder format made standard in the organization. Are there any good reasons for choosing any of these, or others? I am trying to quantify these considerations:

Aesthetics and usability. For example, __dict__ may be hard to read as we don't know how many underscores are in there.

Compatibility. Will some language try to do something funny with {} syntax in a string (such as PHP does with "Welcome to {$siteName} today!")? Actually, I know that PHP and Python won't, but others? Will a C++ preprocessor choke on ## format? If I need to store the value in some SQL engine, will it not consider something a comment? Any other pitfalls to be wary of?

Maintainability. Will the new guy mistake ##SOME_PLACEHOLDER## as a language construct?

The unknown. Surely the wise folk here will think of other aspects of this decision that I have not thought of.

I might be bikeshedding this, but if there are real issues that might be lurking then I would certainly like to know about them before mandating that our developers adhere to a potentially-problematic convention.

答:
In the weeks since I first asked this question, I've learned which characters to avoid. I list them here for my own use and for others:

Avoid * and _ as some issue-tracking software (such as redmine) use these for wiki-syntax and they will replace the values in copy-pasted bug reports.

Bash absolutely chokes on !, in fact you cannot even up-arrow to get the back back to fix the line.

Title: Placeholders in strings


问:
Web programming most effort?
To create a website

I create Psd Files for the design (Phase 1)

I Convert these Psd's to html/css static webpages (Phase 2)

I add some javascript to these webpages (Phase 3)

I start the IDE "visual web developper" to start developping (Phase 4)

I have studied computer programming for the past five years and now I noticed that the corresponding phase (phase 4) of creating a website takes the least amount time and effort in entire process. All the hard work and effort seems to be focused on the graphical design and especially on converting this design to html/css.

My question is, is this a common case? Do I do something wrong in my development steps ?

答:
All the hard work and effort is focused on design and specially on converting this design to html/css, Don't you notice the same ? Have i any problems on my way to work ?

It Depends

The phases and approach will depend on the size of the project.
For small, static websites putting up html/css and graphics takes the most time. However, you may use free templates to have something to start with and adjust it for your project needs.

For bigger projects, preparation to the software development phase is the longest. It also plays critical role in defining later progress and success/failure of the project.

Planning

Well planning daily work/tasks before the actual implementation is very important to start with. Understanding the scope and objective of each individual tasks always shortens your development time, and raises questions to clear out unknowns. Thus, it is very important to invest time in learning/understanding the scope of the task before starting it .

Iterative approach

In today's fast phase and very competitive environment, it is very hard to get complete requirements of the project before starting the development, and traditional waterfall development process does not work well.

Thus, most projects get their requirements piece by piece from project owners and use Iterative development approach in their software development process.

The basic idea behind this method is to develop a system through repeated cycles (iterative) and in smaller portions at a time (incremental), allowing software developers to take advantage of what was learned during development of earlier parts or versions of the system. Learning comes from both the development and use of the system, where possible key steps in the process start with a simple implementation of a subset of the software requirements and iteratively enhance the evolving versions until the full system is implemented. At each iteration, design modifications are made and new functional capabilities are added.

Title: Web programming most effort?


答:
Deskilling effect

The introduction of new, productivity-enhancing technology sometimes enable less-skilled members of the trade to perform nearly as well as its more-skilled members. This may or may not be the intended consequences of the proponents of the new technology. If it is intended, it may bring enormous profits to its proprietors, because the enlargement of candidate pool means the supply of labor is increased, which results in lower wage, and thus brings savings to companies that hire this new technology.

This has been happening ever since the industrial revolution. Please pick up any history-of-science book, or take a short course in a nearby college.

There are a lot of productivity-enhancing technology in web development; there are many client-side and server-side libraries that shorten development time. In some cases it enabled a small team to deliver applications that traditionally would have required a much bigger team.

This is not to imply that web-based or CSS-based program require any less skill in development - the demand for skill simply shift from algorithmic knowledge to visual aesthetics and interaction design, as suggested in MainMa's answer.

Obsolescence

In other cases, the new technology simply made some older, less efficient technology obsolete. Thus, skilled labors who cannot transfer their knowledge to the newer technology will lose their competitive advantage, or even lose their usefulness entirely.

A recent example of obsolescence is desktop software development. It is being gradually replaced by web-based systems. There are great economic advantages in moving to the latter: it shortens the product release cycles from years down to virtually zero.

Structural change

Jobs that are eliminated from deskilling effect or obsolescence, or from global competition, will usually not bounce back to its historical levels.

Knowledge workers

Software development is one of the trades known to require a never-ending, life-long continuous learning effort.

It is possible to identify engaging challenges and rooms for improvements in an otherwise repetitive or passive day job. Perhaps you can learn visual design and electronic commerce in your personal time, and bring more value to your employer. Simply seeing the bigger picture would have helped you find a career path. The imperative is on you to either find a more intellectually challenging job, or to make your current job more interesting.

Title: Web programming most effort?

Body:
To create a website

I create Psd Files for the design (Phase 1)

I Convert these Psd's to html/css static webpages (Phase 2)

I add some javascript to these webpages (Phase 3)

I start the IDE "visual web developper" to start developping (Phase 4)

I have studied computer programming for the past five years and now I noticed that the corresponding phase (phase 4) of creating a website takes the least amount time and effort in entire process. All the hard work and effort seems to be focused on the graphical design and especially on converting this design to html/css.

My question is, is this a common case? Do I do something wrong in my development steps ?


答:
What were your studies? CS or programming, right?

Phase 1 is made by visual designers, based on the work of the interaction designers. Those two jobs have nothing to do with programming.

Phase 2 is closer to programming (even if HTML and CSS are not programming languages). For simple static websites, it will indeed take most of the time. For web applications, on the other hand, it will be only a minor fraction of the project.

So, what can you do now?

Find a web designer. If you were never learnt to do web design, chances are that your work will be of a not very high quality, and you'll spend days doing something a web designer can do in minutes. If for some reason you can't work with a web designer, then learn it yourself. Be ready to spend at least five years full-time learning all the stuff: designer's work require lots of things to know and a lot of practice. Note that interaction design and visual design are two different jobs, both being extremely important for a successful project.

Note that user experience encompasses interaction design side by side with visual design, usability and information architecture. That's why interaction design is essential but not sufficient for a web application.

If having a designer or spending five years in college is an overkill for the project you're working on, then at least invest in learning how to use existent software to accelerate your work. If you're working on websites, and not on web applications, why not using WYSIWYG editors instead of writing code by hand? Those editors would generate crappy code, but does it matter in a context of a small website?

If you're curious, discover other steps required to accomplish a project. In any large project, visual design and programming (i.e. all you do right now) is the easiest and shortest step (10 to 20% of time). What takes much longer is to set up correct communication between project participants, chose standards to follow, gather requirements, test, do architecture and design, write documentation, do security audits, consider legal aspects, prepare deployment process and dozens of other tasks which are more or less unrelated to programming itself.

Title: Web programming most effort?


答:
1. Stop using Photoshop for web design

The fact that people use photo-manipulation software for designing websites never ceases to amaze me. Adobe Indesign provides much more relevant functionality, but, more importantly, it's vector-based. This means no layers (which are completely irrelevant to web design) and easy access to any object on the page. Objects can also be copied into Illustrator and then exported as PNGs with transparent background.

Actually, I'm going to stop there, because I believe that ditching Photoshop and picking up Indesign will make you much more productive. Alternatives to Indesign include Illustrator and Fireworks, but Indesign is much more powerful in my opinion.

Title: Web programming most effort?


问:
Creating Set Subclasses or Allowing Outside Configuration
I have a TriggerCaller and a TriggerAction class. The Caller "calls" the do() method on the action, which is set with the TriggerCallers setAction() method. The rest of the program should deal with the Trigger class, which contains the caller and the action.

I have two options:

1) Give the Trigger class the methods setAction and setCaller. This seems to make sense because it means I won't have to redefine things for each subclass (see option 2) and I won't favor one object over the other (see option 3).

2) I can define Trigger subclasses for each Caller-Action combination. This seems to "feel" better for some reason I can't articulate, but is impractical. There are simply too many combinations of TriggerCallers and TriggerActions to make.

3) Define a Trigger subclass for each TriggerCaller, for instance TriggerMIDI for the MIDICaller and give that class a setAction(). This seems to make sense from a GUI perspective, because in the interface people select the TriggerCaller type from a "Create" menu and then config it with an action in a dialog (along with other things).

I feel that (1) makes the most sense, but somehow it just feels... wrong. I think that I'm exposing too much of the classes inner workings. I'm also putting the burden on the GUI code of setting the class up (sort of). Is there a better solution? Which one should I choose?

答:
I would choose option one but with a small change: Since it seems that a Trigger can not work without an Action and Caller, I would move the injection of those dependencies up to the Trigger's constructor (and probably remove the setters).

Also:

```<code> I'm also putting the burden on the GUI code of setting the class up (sort of).
</code>```

The responsibility of setting the Trigger's up should not be on the GUI. I'd create a TriggerFactory whose only responsibility is to create triggers on demand. It would have semantic methods that clients could call to get Triggers fully wired up.

Some examples of those methods could be

```<code>TriggerFactory.buildMidiTrigger();
TriggerFactory.buildEmailTrigger();
etc...
</code>```

Body:
I would choose option one but with a small change: Since it seems that a Trigger can not work without an Action and Caller, I would move the injection of those dependencies up to the Trigger's constructor (and probably remove the setters).

Also:

 I'm also putting the burden on the GUI code of setting the class up (sort of).

The responsibility of setting the Trigger's up should not be on the GUI. I'd create a TriggerFactory whose only responsibility is to create triggers on demand. It would have semantic methods that clients could call to get Triggers fully wired up.

Some examples of those methods could be

TriggerFactory.buildMidiTrigger();
TriggerFactory.buildEmailTrigger();
etc...


答:
This is an interesting question, and the answer is complex.

Overall, I think it is fair to say that the JVM garbage collector is very well designed and extremely efficient. It's probably the best general purpose memory management system.

C++ can beat the JVM GC with specialised memory allocators that are designed for specific purposes. Examples might be:

Per-frame memory allocators, which wipe the whole memory area at periodic intervals. These are frequently used in C++ games, for example, where a temporary memory area is used once per frame and immediately discarded.

Custom allocators managing a pool of fixed-sized objects

Stack based allocation (although note that the JVM also does this in various circumstances, e.g. via escape analysis)

Specialised memory allocators are, of course, limited by definition. They usually have restrictions on object lifecycle and/or restrictions on the type of object that can be managed. Garbage collection is much more flexible.

Garbage collection also gives you a some significant advantages from a performance perspective:

Object instantiation is indeed extremely fast. Because of the way that new objects are allocated sequentially in memory, it often requires little more than one pointer addition, which is certainly faster than typical C++ heap allocation algorithms.

You avoid the need for lifecycle management costs - e.g. reference counting (sometimes used as an alternative to GC) is extremely poor from a performance perspective since the frequent incrementing and decrementing of reference counts adds a lot of performance overhead (typically much more than GC).

If you use immutable objects, you can take advantage of structural sharing to save memory and improve cache efficiency. This is used heavily by functional languages on the JVM like Scala and Clojure. It is very difficult to do this without GC, because it is extremely hard to manage the lifetimes of shared objects. If you believe (as I do) that immutability and structural sharing are key to building large concurrent applications, then this is arguably the biggest performance advantage of GC.

You can avoid copying if all types of object and their respective lifecycles are managed by the same garbage collection system. Contrast with C++, where you often have to take full copies of data because the destination requires a different memory management approach or has a different object lifecycle.

Java GC has one major downside: because the work of collecting garbage is deferred and done in chunks of work at periodic intervals, it causes occasional GC pauses to collect garbage, which can affect latency. This is usually not a problem for typical applications, but can rule Java out in situations where hard realtime is a requirement (e.g. robotic control). Soft realtime (e.g. games, multimedia) is typically OK.

Title: Java Heap Allocation Faster than C++

Body:
I already posted this question on SO and it did ok. It was unfortunately closed though(only needs one vote to reopen) but someone suggested I post it on here as it is a better fit so the following is literally a copy paste of the question

I was reading the comments on this answer and I saw this quote.

Object instantiation and object-oriented features are blazing fast to use (faster than C++ in many cases) because they're designed in from the beginning. and Collections are fast. Standard Java beats standard C/C++ in this area, even for most optimized C code.

One user (with really high rep I might add) boldly defended this claim, stating that

heap allocation in java is better than C++'s

and added this statement defending the collections in java

And Java collections are fast compared to C++ collections due largely to the different memory subsystem.

So my question is can any of this really be true, and if so why is java's heap allocation so much faster.


答:
I noticed that the corresponding phase (phase 4) takes the less time and effort in all the process, and all the hard work and effort is focused on design and specially on converting this design to html/css, Don't you notice the same ? Have i any problems on my way to work?

It really depends on the kind of website you are developing.

Most static-only websites (means that you can pretty much just use HTML/CSS) will take you more time to design rather than to put online.

The same does not apply to dynamic websites (something like a social network, SE, any user-based websites) which requires a lot of database and server side programming compared to just designing the skeleton of the application.

Also taking a look at your workflow you seems to be using Dreamweaver or programs just like it. I feel the need to warn you: using such programs may produce code that is either non-w3c-valid, inefficient or not compatible with most modern browsers.

Title: Web programming most effort?


问:
How to handle divide by zero in a language that doesn't support exceptions?
I'm in the middle of developing a new programming language to solve some business requirements, and this language is targeted at novice users. So there is no support for exception handling in the language, and I wouldn't expect them to use it even if I added it.

I've reached the point where I have to implement the divide operator, and I'm wondering how to best handle a divide by zero error?

I seem to have only three possible ways to handle this case.

Ignore the error and produce 0 as the result. Logging a warning if possible.

Add NaN as a possible value for numbers, but that raises questions about how to handle NaN values in other areas of the language.

Terminate the execution of the program and report to the user a severe error occurred.

Option #1 seems the only reasonable solution. Option #3 is not practical as this language will be used to run logic as a nightly cron.

What are my alternatives to handling a divide by zero error, and what are the risks with going with option #1.

答:
I would strongly advise against #1, because just ignoring errors is a dangerous anti-pattern. It can lead to hard-to-analyze bugs. Setting the result of a division by zero to 0 makes no sense whatsoever, and continuing program execution with a nonsensical value is going to cause trouble. Especially when the program is running unattended. When the program interpreter notices that there is an error in the program (and a division-by-zero is almost always a design error), aborting it and keeping everything as-is is usually preferred over filling your database with garbage.

Also, you will unlikely be successful with thoroughly following this pattern through. Sooner or later you will run into error situations which just can't be ignored (like running out of memory or a stack overflow) and you will have to implement a way to terminate the program anyway.

Option #2 (using NaN) would be a bit of work, but not as much as you might think. How to handle NaN in different calculations is well-documented in the IEEE 754 standard, so you can likely just do what the language your interpreter is written in does.

By the way: Creating a programming language usable by non-programmers is something we've been trying to do since 1964 (Dartmouth BASIC). So far, we've been unsuccessful. But good luck anyway.


答:
Terminate the running application with extreme prejudice. (While providing adequate debug information)

Then educate your users to identify and handle conditions where the divisor might be zero (user entered values, etc.)

Title: How to handle divide by zero in a language that doesn't support exceptions?

Body:
I'm in the middle of developing a new programming language to solve some business requirements, and this language is targeted at novice users. So there is no support for exception handling in the language, and I wouldn't expect them to use it even if I added it.

I've reached the point where I have to implement the divide operator, and I'm wondering how to best handle a divide by zero error?

I seem to have only three possible ways to handle this case.

Ignore the error and produce 0 as the result. Logging a warning if possible.

Add NaN as a possible value for numbers, but that raises questions about how to handle NaN values in other areas of the language.

Terminate the execution of the program and report to the user a severe error occurred.

Option #1 seems the only reasonable solution. Option #3 is not practical as this language will be used to run logic as a nightly cron.

What are my alternatives to handling a divide by zero error, and what are the risks with going with option #1.


问:
Benchmark of asynchronous code
Asynchronous programming seems to be getting quite popular these days. One of the most quoted advantages is performance gain from removing operations that block threads. But I also saw people saying that the advantage is not that great. And that properly configured thread pool can have same effect as fully asynchronous code.

My question is, are there any real benchmarks that compare blocking vs. asynchronous code? It can be any language, but I think C# and Java would be most representative. Not sure how good would this benchmark be with Node.js or similar.

Edit : My attempt at general question combined with unclear terminology seems to have failed. By "asynchronous code"  I mean what some answers described as event or callback programming. In the final result, operations that would block thread are instead delegated to some callback system so threads can be utilized better.

And if I wanted to ask specific question : Are there any benchmarks that compare throughput/latency gain of async/await server code in .NET? Or any other similar comparison?

答:
Based on your comments, it seems that you're really interested in "non-blocking IO." This differs from my definition of "asynchronous programming," which is an approach to decomposing work exemplified by Erlang processes or Goroutines.

And, if that is your definition, then yes, there have been benchmarks. But, like all benchmarks, they shouldn't be accepted blindly. Instead, you need to think about what goes on behind the scenes.

A thread is the unit of OS scheduling. Platforms such as Erlang and Go build their own schedulers on top of the OS scheduler, allowing multiple units of execution to share the same thread. This is great, as long as your units of execution are lightweight, because it avoids the overheads associated with threads.* However, IO operations require a trip to the kernel, which means that you need a real thread to do them. And if you're implementing a sub-thread scheduler, you need to be smart about not scheduling sub-thread tasks on a thread that's blocked in a kernel operation.

All IO operations have the potential to block.** When you make a read or write request, the kernel looks to see if there is data available or (for write) room in a buffer. If not, the kernel suspends the thread until the operation can complete. This makes thread-per-connection servers really simple to implement, but worries people who think about thread overheads.

Operating systems provide a way to block on multiple IO channels simultaneously. The select call on POSIX is one of these: you provide it with a list of channels (file descriptors / sockets) that you care about, and it will tell you when one of them is ready to read or write (read is what most people care about). You still have to make a kernel call, and you'll still end up blocking a thread if nothing's available, but that's only one thread. This is how Node.js works: when data is available, the proper event handler is called (I don't know the internals of Node, but hope that they also verify that write buffers are available before calling write).

When you max CPU, you're done. It doesn't matter whether you use select or a thread-per-connection approach, you still need to spend CPU to do whatever your server is meant to do. With the thread-per-connection approach, you don't really pay attention to that: the scheduler will assign threads to cores, and you'll degrade gracefully. With select, you will need to hand connections off to threads when they're ready for processing, or you'll be limited by the performance of a single core (Node.js gets around that by letting you spawn multiple servers).

As I said, benchmarks shouldn't be accepted blindly; they're only valid as long as they model the real-world problem that you're trying to solve. The author of the linked benchmark works for (worked for?) Mailinator, which if you haven't used it, is a poste restante service for ad hoc email addresses. Which means that it's going to be getting short-lived, high-activity connections from a relatively small number clients. This is a perfect use case for thread-per-connection scheduling. As noted in the comments, a chat server (long-lived, low activity) might be different.

In my mind, the question of blocking versus non-blocking IO is rather boring: most real-world servers don't have that many concurrent connections. More interesting to me is the programming model: a worker-based model like Erlang or Go means that you can focus on your business logic, and not care how connections are being managed.

* These overheads include kernel scheduling structures, and perhaps most important, a multi-megabyte thread stack, most of which goes unused. While 2MB doesn't seem like much, it adds up quickly if you have 100k processes ... which most applications don't have.

** Not 100% true, but I don't want to get too deep into the weeds here.

Title: Benchmark of asynchronous code


答:
1 - Ignore the error and produce 0 as the result. Logging a warning if possible.

That's not a good idea. At all. People will start depending on it and should you ever fix it, you will break a lot of code.

2 - Add NaN as a possible value for numbers, but that raises questions about how to handle NaN values in other areas of the language.

You should handle NaN the way runtimes of other languages do it: Any further calculation also yields NaN and every comparison (even NaN == NaN) yields false.

I think this is acceptable, but not necessarily new comer friendly.

3 - Terminate the execution of the program and report to the user a severe error occurred.

This is the best solution I think. With that information in hand users should be able to handle 0. You should provide a testing environment, especially if it's intended to run once a night.

There's also a fourth option. Make division a ternary operation. Any of these two will work:

div(numerator, denumerator, alternative_result)

div(numerator, denumerator, alternative_denumerator)


答: This isn't a question of hacking JavaScript. If I wanted to attack your app, I would use a private proxy that would allow me to capture, modify, and replay traffic. Your proposed security scheme doesn't appear to have any protections in place against that. </p>

Title: How easy is it to hack JavaScript (in a browser)?

Body:
My question has to do with JavaScript security.

Imagine an authentication system where you're using a JavaScript framework like Backbone or AngularJS, and you need secure endpoints. That's not a problem, as the server always has the last word and will check if you're authorized to do what you want.

But what if you need a little security without involving the server? Is that possible?

For example, say you've got a client-side routing system and you want a concrete route to be protected for logged-in users. So you ping the server asking if you're allowed to visit protected routes and you go on. The problem is that when you ping the server, you store the response in a variable, so the next time you go to a private route, it will check that if you're already logged in (no ping to the server), and depending on the response it will go or not.

How easy is for a user to modify that variable and get access?

My security (and JavaScript) knowledge isn't great. But if a variable is not in global scope and is in the private part of a module pattern which only have getters but not setters, even in that case, can you hack the thing out?


问:
Queues and threading
I am developing a new project where I will be constantly checking a webpage for data and adding this data to a queue for processing. This data will then be removed from the queue and added to a list if it hasn't already been added. This list will then be stored in the database.

I was wondering about how to use threads as I am relatively new to programming? I think it would be ideal if I had a thread constantly checking the webpage for data, a thread processing the queue and a thread for processing the list.

Any suggestions on whether this method is the most effective or not and how to achieve this would be much appreciated.

答:
Unless the data on the website changes very frequently (multiple times per second), there is no reason to use multiple threads for this kind of application.

You can just use a big loop that, in order

retrieves data from the site

updates the list, if needed

updates the database with the new list, if needed.

Multiple threads and a queue for communicating between them only adds overhead to the program, for which there seems to be no reason judging by your description of what the program needs to do.

Title: Queues and threading


答: In Haskell (and similar in Scala), instead of throwing exceptions (or returning null references) the wrapper types Maybe and Either can be used. With Maybe the user has a chance to test if the value he got is "empty", or he might provide a default value when "unwrapping". Either is similar, but can be used returns an object (e.g. an error string) describing the problem if there is one.


问:
Who does test-driven development?
I've been working in the enterprise space for the past 4½ years and have noticed that generally speaking, enterprises are not conducive environments for the test-first style of development. Projects are usually fixed-cost, fixed-timeline and waterfall style. Any unit testing, if done at all, usually comes after development in the QA phase and done by another team.

Prior to working for an enterprise, I consulted for many small to medium sized companies, and none of them were willing to pay for a test-first style of development project. They usually wanted development started immediately, or after a short design stint: i.e., something more akin to Agile, though some clients wanted everything mapped out similar to waterfall.

With what types of shops, companies, and clients does test-driven development work
best? What types of projects tend to be conducive to TDD?

答: Those who implement clones.  I can't think of a better process when you're to develope something, that works exactly like an existing program.</p>

Title: Who does test-driven development?

ContentLicense: CC BY-SA 2.5


问:
Java vs C#: What should a junior programmer (just finished degree) consider when entering the programming world?
I just want to hear some pro and con's - it's obvious that there is no right answer

So would Java be a better choice for a long term career? Or should I opt for .Net since it could be the platform having all the latest advances?

I just want to hear others point of view, so please don't close this question just because it's being subjective

答: Learn whatever's in demand in the area where you want to live.  The languages are very similar, so it doesn't take a whole lot of time to learn one once you know the other.  Check out job listings for the area and see what's in demand.  Part of making yourself marketable is knowing the market.</p>

OwnerDisplayName: oneWhoNeedGuidenss


问:
What percentage of change is considered artistic license vs. a clone in video game development?
I'm a "weekend coder" developer making mobile apps and games and I've had the desire to make my own version of a game made for the Commodore 64 back in the early 80's by a small publisher e.g. Not EA Games or similar.

The game hasn't been ported / released on any platform since.

I want to make my version because it was fun to play but I can obviously create much better graphics now (the original had 8 colors at most, and requires a good imagination to transform a character image in the game into something with 4 limbs and a head).

Since it will take effort to make from scratch, and the game will be different when done I'd like to charge 99 cents to users... But I have fears of being sued should the original developer pop out of the woodwork from somewhere.

Here's a breakdown of original vs. My game intentions.

Original game was a 2D side view game (think original Mario bros) with 3 screens that flipped when you crossed the edge.  You could collect items, there were AI players that "chased" you, ladders, and some ride-able objects... All within a specific theme.

My version would also be a 2D sideview, but be a sidescroller with at least 3 but maybe 4 or 5 "screens" worth of content. Images would be all original, have a nice 3D look to them, full color, shadows and lighting effects. You would still collect items but they would be all different, and you'd still get chased etc. but I'd try as best as possible to make my game unique... and of course it would have a different name.

To a video game enthusiast that played this game in the early 80's they'd be able to proclaim..."oh man this game reminds me of __!" I don't think they would declare it to be a "ripoff" or "just a clone" though.

Are there documented cases where a developer has been sued successfully for copying another game?.. And if so just how close was it?  Likewise is there a statute of limitations?... This game is pushing 30 years old...

Or is the answer just build it and see?

答:
#include <IANAL.h>

/* If you need a lawyer, talk to one */

This is all based on my very limited knowledge of intellectual property law (and mostly around board games and photographs).  Do not assume that because I am writing this I know what I'm talking about.  Nintendo has lawyers who know much more about this than I do.

There are three key parts of intellectual property law:

Copyright (protections on particular expressions of an idea)

Patent (protections on a general idea)

Trademark (protections on something that resembles something from a particular source)

Gameplay is not copyrightable.  The code itself, the artwork, the wording of the rules - these are tangible (for some meaning of the word) expressions of an idea. The gameplay of a game is not these things and is not protected under copyright.

Gameplay is patentable.  The best example of this is board games.  US Patent 7,264,242 protects the board game Khet (and means that I really can't try to make the same game as a computer game with different artwork and wording of the rules.  Side note, boardgamers will trawl old board game patents for games never published where the patent has expired.)  One might also take note that Miyamoto wanted to patent jumping in games back with Mario Bros. This might have been patentable, it wasn't patented and the rest is history.

Titles, branding and marketing is trademarked.  Titles of games fall into this.  Mario falls into this.  One might wonder if the "guy jumping into a pipe" is part of Nintendo branding.  It might be.  Tread with caution.

I will also point to Triple Town / Yeti Town and the lawsuit over this.  Its a clone and apparently fell into copyright, though I can't read legalese quite well enough to determine what exactly it was with my limited knowledge that the infringement was on (there was infringement, but what area? I'm guessing not enough change to the UI...)

Further reading: Video Games and the law: Copyright, Trademark and Intellectual Property


问:
Is it okay to add time for "unexpected delays" to a time estimate?
I've landed my first contract (hooray, self employment!) and the company is asking for time estimates.

Programmers are notoriously bad at time estimates, and I know I've been laughably wrong before.  It's fixed bid, so I'm not concerned about charging.  I'm just concerned about expectation management.

So far I've itemized the work I need to do, estimated the time it would take, then significantly padded that time.  I'm still nervous about it, though.  Is it acceptable to write in time for "unexpected delays"?  I've estimated 4 weeks, and I'd like to add a 5th week for problems I haven't thought of.  Is that something people do?  Would you balk if someone gave you an estimate with that in it?

答:
First, congratulations on your contract! Ok, enough celebrating, let's get down to business. ;)  I've been a consultant for over 15 years -- here's my advice.

In project management, what you are talking about it "contingency" planning -- and you absolutely should do it, else you are likely to disappoint your client (and make yourself unhappy throughout the project). However, you should NOT specifically put it into the plan as it's own line item -- as then when you need it (and you most likely will), it only makes you look like a bad planner, and by definition you will be behind schedule.

There is a motto you live by: "Under-promise and Over-Deliver". Set expectations (in this case delivery time) low, but not so low that the client would be put off, and then beat the timeline (and demonstrate that you are ahead of schedule).

Instead, of one contingency block at the end of the project, you should distribute contingency planning throughout the project. Assuming you haven't already committed to delivering in your estimated 4 weeks, suggest and plan for 6, with your planned 4 weeks of effort spread out evenly over all 6 weeks. This will make you, and your client much happier, as you should generally be slightly "ahead of schedule" throughout. :)

Important: You should plan progress updates / partial demos on a frequency that:
1) mitigates the risk of building what they asked for but not what they want
2) builds customer confidence while not being overly burdensome on you. Be SURE to plan for this time working with the customer, giving demos, tweaking things, etc.

In a project of that length, that is most likely every three days or so.

Finally, when you are planning the work, front-load the most "risky" or "unknown" items first and plan the most contingency for them. Risk takes many forms -- and it's most often not the technical stuff. Generally, the biggest risk is that the customer doesn't TRULY know exactly what they want. You want to get stuff in front of them early and often to ensure you are in alignment. This means prototypes, mockups and such. If there is misalignment or misunderstanding, you want to find it as early as possible! Generally, if you find this out very early (before significant work has been done), you can renegotiate the contract to work for both parties. The biggest mistake freelancers make is delivering what the customer asked for, but not what they want. You need to understand that you are responsible for ensuring that both parties are the same page.

On riskier technical items, do enough of a proof-of-concept early so that you know you won't crash into a roadblock late in the game. Fight the tendency most people have to focus on the stuff they already know to "build momentum".

Have fun with it and I hope this helps. If you would, let us know how it went after you complete the project. Good luck!


答:
How publishers handled this issue in amiga times was that they asked programmer to implement one feature like blue background tiles to the game. This supposedly ensures the game is not exact copy of some other game, and that the programmer actually wrote the game.

But of course if you can make your game have original elements in it, it'll be better. Basically good way to think about it is that taking someone elses game elements for your game saves about 1/2 of the development time, and thus makes your game cheaper. One year project turns to two year project if you make original game elements. Quality games spend the time.

Title: What percentage of change is considered artistic license vs. a clone in video game development?

Body:
I'm a "weekend coder" developer making mobile apps and games and I've had the desire to make my own version of a game made for the Commodore 64 back in the early 80's by a small publisher e.g. Not EA Games or similar.

The game hasn't been ported / released on any platform since.

I want to make my version because it was fun to play but I can obviously create much better graphics now (the original had 8 colors at most, and requires a good imagination to transform a character image in the game into something with 4 limbs and a head).

Since it will take effort to make from scratch, and the game will be different when done I'd like to charge 99 cents to users... But I have fears of being sued should the original developer pop out of the woodwork from somewhere.

Here's a breakdown of original vs. My game intentions.

Original game was a 2D side view game (think original Mario bros) with 3 screens that flipped when you crossed the edge.  You could collect items, there were AI players that "chased" you, ladders, and some ride-able objects... All within a specific theme.

My version would also be a 2D sideview, but be a sidescroller with at least 3 but maybe 4 or 5 "screens" worth of content. Images would be all original, have a nice 3D look to them, full color, shadows and lighting effects. You would still collect items but they would be all different, and you'd still get chased etc. but I'd try as best as possible to make my game unique... and of course it would have a different name.

To a video game enthusiast that played this game in the early 80's they'd be able to proclaim..."oh man this game reminds me of __!" I don't think they would declare it to be a "ripoff" or "just a clone" though.

Are there documented cases where a developer has been sued successfully for copying another game?.. And if so just how close was it?  Likewise is there a statute of limitations?... This game is pushing 30 years old...

Or is the answer just build it and see?


问:
Translating variables into english
I have recently came across some code where the variable names are in french, I could not use google translate because only part of the code needs translated, and it would not be that practical to copy and paste each of the variables into the translator.

How can i easily translate the variable names? what translator could i use?

Here is some french that i tried to translate.

```<code>#include <stdio.h>

int main()
{
    unsigned int nombre;
    unsigned int diviseur;
    unsigned int reste;

    for (nombre = 2; nombre != 0; nombre++)
    {
        for (diviseur = 2, reste = 1; (diviseur < nombre) && (reste > 0); diviseur++)
        {
            reste = nombre % diviseur;
        }
        if (reste > 0)
        {
            printf("%d\n", nombre);
        }
    }

    return;
}
</code>```

But when i tried to translate the code using google translate i got this bad translation.

```<code># include stdio.h

int main ()
{
    unsigned int number;
    unsigned int divisor;
    unsigned int left;

    for (count = 2, number = 0;! number + +)
    {
        for (divisor = 2, left = 1, (divisor <number) && (rest> 0) + + divider)
        {
            remainder = number% divisor;
        }
        if (left> 0)
        {
            printf ("% d \ n", count);
        }
    }

    return;
}
</code>```

答:
Depending on your language, parsers or some sort of static checking tools may be available, letting you to extract the list of variables.

For example, PHP has a tokenizer, while for JavaScript, one can use jslint. For C#, there is StyleCop which will, with minor configuration changes, highlight every variable written in a language other than English (or containing spelling errors).

Once you get the list of variables, use a translator tool of your choice and get a translation as a table where the first column is the Russian name, and the second one — the English translation.

This step would be done mostly manually. I don't know any automated translator which will be able to deal with the variable name such as iCountActivations. Regular expressions can help to get rid of prefixes or translating camel case into separate words.

Then use refactoring tools to rename the variables. Refactoring tools will take care of renaming the variable in every place where it is used, and rename only this variable.

Finally, use search to check manually that there is anything left.

Warning: be prepared to obtain meaningless variable names because of a bad translation. If code quality matter, prefer to translate the source code by a person who knows both Russian and English and who is also a programmer and is able to understand the source code to translate. Such people are easy to find, I believe (I'm one of them and know a lot of developers who know languages other than English), but very probably, they wouldn't do it for free.

Title: Translating variables into english


问:
When is it ok to use a Global variable
Ok, so this is a bit of a devils advocate question really.

When are global variables ok, and if never, what would you use as an alternative?

An interesting side-case to this question, how is a public static class field different from a global?

答:
In a horrible VB6 code base that abuses globals like there's no tomorrow, I'm guilty of introducing a new one:

```<code>Global CsExt As New TheAppBeingRewrittenInCSharpWhileVb6CodeIsStillBeingMaintained
</code>```

I think it's one of the few valid use cases for a global object.

Title: When is it ok to use a Global variable

Body:
In a horrible VB6 code base that abuses globals like there's no tomorrow, I'm guilty of introducing a new one:

Global CsExt As New TheAppBeingRewrittenInCSharpWhileVb6CodeIsStillBeingMaintained

I think it's one of the few valid use cases for a global object.


问:
Clean, Modular Code vs MV* Frameworks
I've been hearing a lot about the "new" MV* frameworks. I've tinkered with KnockoutJS (I created an invoicing application), but I much prefer to write clean, modular code in raw JavaScript - leveraging utility APIs and other libraries when necessary.

Given a methodical/structured/SOLID approach to writing a JavaScript application, where OOP, SOC, SRP and other design principles are adhered to, wouldn't the usage of MV* frameworks be superfluous?

Are there any articles that express/address these concerns?

I've found one in the past

note - I've migrated this question from SO to this site, as its more appropriate for this audience.

答:
Clean code always trump any architectural models, in that if you give me the choice of clean, understandable code that's not written in a MVC way, or nasty convoluted code that does fit in a MVC way.. I'll take the former.

there are disadvantages to these things. John Gossman said of MVVM (the WPF model-view-viewmodel version of MVC) that it is overkill for small projects, and the complexity of designing it can be too much for large projects. He also said the resource usage can make it unsuitable for medium sized projects too :)

So separation of code into presentation, logic and data tiers in some way is a good thing, makes your code simpler to write, and allows you to manage each section in isolation. Whether that means you must use an existing MVC framework is another matter however, as Gossman said, your exact requirements for your project may be different enough to those defined for a MVC framework to make that framework more trouble than its worth. As always, there's no silver bullet.

Title: Clean, Modular Code vs MV* Frameworks

ContentLicense: CC BY-SA 4.0


问:
difference between accounting and potential methods in Amortized Analysis
I was going through the Introduction to Algorithms by Cormen et al.In the chapter titled Amortized Analysis,the difference between accounting and potential methods is given like this

The accounting method overcharges some operations early in the
  sequence, storing the overcharge as “prepaid credit” on specific
  objects in the data structure. ...

The potential method maintains the credit as the “potential energy” of
  the data structure as a whole instead of associating the credit with
  individual objects within the data structure.

I tried to understand this using the example of resizable array which doubles its size when there is no space to insert an element.What I couldn't understand was the following sentence

storing the overcharge as “prepaid credit” on specific
  objects in the data structure

Using Accounting method:

ci = the charge for the i-th operation

c'i = amortized cost of the i-th operation

Si = array size after the i-th operation

bi = balance of credit after the i-th operation

```<code>i   1  2  3  4  5  6  7  8  9 10
si  1  2  4  4  8  8  8  8 16 16
ci  1  2  3  1  5  1  1  1  9  1
c'i 3  3  3  3  3  3  3  3  3  3
bi  2  3  3  5  3  5  7  9  3  5
</code>```

what exactly does "storing on specific  objects in the data structure"  mean  here?

In potential method ,if I use phi to be 2n-m where n = number of currebt elements in the array ,and m=array size

```<code>                n     m    phi
empty            0     0    0
add first elem   1     1    1
add secondelem   2     2    2
add third elem   3     4    2
add fourth elem  4     4    4
add fifth  elem  5     8    2
</code>```

In this ,what does it mean by "...the “potential energy” of the data structure as a whole instead of associating the credit with individual objects within the data structure."

答:
Accounting Method:

The accounting method works to determine a payment of some extra time units charged to each individual operation so that the sum of all payments isn't exceeded by the total actual cost.

You can think about it like a bank account where you charge a little more than actual cost for each and bank extra in a pool so that very high-cost operations can be charged less than their actual cost, paid for by the pool of savings. This spreads out the high-cost operations over the entire sequence.

The charges for each operation need to be big enough to keep the balance in the account positive while being small enough so as to not significantly overcharge the operation above its actual cost.

Question: Help understand this:
  storing the overcharge as “prepaid credit”
  on specific objects in the data structure

  Answer:

  Given:

    Cost to insert is 1

    Cost to move each value when table is doubled is 1

    Charge for each insert is 3

Insert #1:

  cost = 1 (insert: 1)

  charge = 3

  bankedAmount= 2

  bankBalance = 2

  NOTE: The bankedAmount is associated with this transaction, but then just put into the account -- that is the prepaid credit due to specific object in the data structure.

Insert #2:

  cost = 2 (doubleTableAndMove: 1*1 = 1, insert: 1)

  charge = 3

  bankedAmount= 1

  bankBalance = 3

Insert #3:

  cost = 3 (doubleTableAndMove: 2*1 = 2, insert: 1)

  charge = 3

  bankedAmount= 0

  bankBalance = 3

Insert #4:

  cost = 1 (insert: 1)

  charge = 3

  bankedAmount= 2

  bankBalance = 5

Insert #5:

  cost = 5 (doubleTableAndMove: 4*1 = 4, insert: 1)

  charge = 3

  bankedAmount= -2

  bankBalance = 3

etc...

Potential Method:

Question:
  In this ,what does it mean by "...the “potential energy” of the data
  structure as a whole instead of associating the credit with individual
  objects within the data structure."

Answer:
  Its analogous to the
  bank account concept in the accounting method. The potential function
  keeps track of the total extra time charged at any time in a sequence
  of operations, but it's only based on the current state of the
  structure, regardless of all the operations that put it into that
  state.

There's further explanation with more detail here.


答:
Number 1 (insert undebuggable zero) is always bad.  The choice between #2 (propagate NaN) and #3 (kill the process) depends on context and ideally should be a global setting, as it is in Numpy.

If you're doing one big, integrated calculation, propagating NaN is a bad idea because it will eventually spread and infect your whole calculation--- when you look at the results in the morning and see that they're all NaN, you'd have to throw out the results and start again anyway.  It would have been better if the program terminated, you got a call in the middle of the night and fixed it--- in terms of the number of wasted hours, at least.

If you're doing many little, mostly-independent calculations (like map-reduce or embarrassingly parallel calculations), and you can tolerate some percentage of them being unusable due to NaNs, the that's probably the best option.  Terminating the program and not doing the 99% that would be good and useful on account of the 1% that are malformed and divide by zero might be a mistake.

Another option, related to NaNs: the same IEEE floating-point specification defines Inf and -Inf, and these are propagated differently than NaN.  For instance, I'm pretty sure that Inf > any number and -Inf < any number, which would be what you wanted if your division by zero happened because the zero was just supposed to be a small number.  If your inputs are rounded and suffer from measument error (like physical measurements taken by hand), the difference of two large quantities can result in zero.  Without the division-by-zero, you would have gotten some large number, and maybe you don't care how large it is.  In that case, In and -Inf are perrfectly valid results.

It can be formally correct, too--- just say you're working in the extended reals.


答:
Short version: when it makes it easier to reason about the program. Typically cases are some type of global state or static resource that is widely used.

Long version: Tom Hawtin said "with spooky action at a distance"...that is exactly the problem with globals -- you have to know where it is being used and how, or you can get some really weird and hard to track down bugs.  Locals are nothing more or less than a strategy to reduce the scope of what the programmer needs to understand in order to reason about the program.

Another side of the problem with knowing where they are used is that you can end up with duplicate globals -- in which case things can get really weird as most of the programs gets and sets var1 while in a couple of places var2 is used to hold the same information.  Particularly when multiple people are working on the same code. IDE's can be helpful with finding usage reducing the cost of globals, but they do nothing for duplicates.

The more globals you have the harder it is to keep track of what is happening with them.  They should be few and far between.

Title: When is it ok to use a Global variable

Body:
Ok, so this is a bit of a devils advocate question really.

When are global variables ok, and if never, what would you use as an alternative?

An interesting side-case to this question, how is a public static class field different from a global?


问:
Is it poor practice to call methods through multiple objects?
I have code that looks like this:

```<code>tr.t.findIndexSmoothed(arg0.getX(), arg0.getY());
</code>```

"tr" and "t" are objects. Is it bad practice to reach all the way down the object hierarchy to call methods? The only reason I can think of is that is breaks encapsulation, and if that's the case, can anybody tell me why that poses a problem? Also, does this code structure inhibit performance in any way?

答:
The Law of Demeter suggests that you shouldn't call methods on objects two layers down like that.

The idea is that, in your case, you should have a method findIndexSmoothed on tr which in turn calls findIndexSmoothed on t.

What this does is makes your code adaptable. It means that the nature of tr.findIndexSmoothed can change without necessarily changing the nature of t.findIndexSmoothed, which may be called by other code you don't intend to change.

However, it is commonly noted that it's pretty rare you really need that adaptability, and you'll have put yourself in a position where maintenance of t.findIndexSmoothed now requires maintenance of tr.findIndexSmoothed so, in some cases, you will increase your workload.

So, the answer to your question, as so many style questions in programming, is: Use your common sense. Keep the Law of Demeter in mind, but never make it an inflexible truth.


答:
Other answers have already considered the relative merits of your ideas. I propose another one: use basic flow analysis to determine whether a variable can be zero. Then you can simply disallow division by variables that are potentially zero.

```<code>x = ...
y = ...

if y ≠ 0:
  return x / y    // In this block, y is known to be nonzero.
else:
  return x / y    // This, however, is a compile-time error.
</code>```

Alternatively, have an intelligent assert function that establishes invariants:

```<code>x = ...
require x ≠ 0, "Unexpected zero in calculation"
// For the remainder of this scope, x is known to be nonzero.
</code>```

This is as good as throwing a runtime error—you skirt undefined operations entirely—but has the advantage that the code path need not even be hit for the potential failure to be exposed. It can be done much like ordinary typechecking, by evaluating all branches of a program with nested typing environments for tracking and verifying invariants:

```<code>x = ...           // env1 = { x :: int }
y = ...           // env2 = env1 + { y :: int }
if y ≠ 0:         // env3 = env2 + { y ≠ 0 }
  return x / y    // (/) :: (int, int ≠ 0) → int
else:             // env4 = env2 + { y = 0 }
  ...
...               // env5 = env2
</code>```

Furthermore, it extends naturally to range and null checking, if your language has such features.

Body:
Other answers have already considered the relative merits of your ideas. I propose another one: use basic flow analysis to determine whether a variable can be zero. Then you can simply disallow division by variables that are potentially zero.

x = ...
y = ...

if y ≠ 0:
  return x / y    // In this block, y is known to be nonzero.
else:
  return x / y    // This, however, is a compile-time error.

Alternatively, have an intelligent assert function that establishes invariants:

x = ...
require x ≠ 0, "Unexpected zero in calculation"
// For the remainder of this scope, x is known to be nonzero.

This is as good as throwing a runtime error—you skirt undefined operations entirely—but has the advantage that the code path need not even be hit for the potential failure to be exposed. It can be done much like ordinary typechecking, by evaluating all branches of a program with nested typing environments for tracking and verifying invariants:

x = ...           // env1 = { x :: int }
y = ...           // env2 = env1 + { y :: int }
if y ≠ 0:         // env3 = env2 + { y ≠ 0 }
  return x / y    // (/) :: (int, int ≠ 0) → int
else:             // env4 = env2 + { y = 0 }
  ...
...               // env5 = env2

Furthermore, it extends naturally to range and null checking, if your language has such features.


问:
Estimating tasks in Scrum
Our team using Scrum three iterations. We successfully estimate PBI in storypoints using poker-planning. But next we cannot do anything because we don't know

Who create tasks? PBI is created by everyone and approved by product owner, but what about task

Who estimates task?

What technique should be using for task estimating? Poker planning are good for PBI estimating

答:
Think about why you are estimating the tasks in the first place; what is the point if you're already tracking the teams progress via story points and velocity. In my opinion, it is secondary to story level estimates.

The point is you want to track the progress of the team in the current iteration. In theory, people are better at estimating smaller chunks of time. The estimates are usually used to create a burn down.

These estimates should have little impact on the macro-level, story points, which makes them a bit redundant. Don't get me wrong, a some redundancy is OK. You need checks in place to make sure that you're original estimates are sound.

To answer your specific questions:

The tasks are created by the team who are going to implement the stories. You don't have to do this as a full team, but you could break up to smaller teams to do this. I find it is best not to involve the whole team in this.

The people who write them. It is better if you don't have just one person estimating a task.

If there is any uncertainty I find a small spike (hours rather than days) to be effective. A team member is tasked with looking at the code and coming up with a breakdown. You could argue that this should have already happened when the original estimate was made, but I find those estimates can be correct even if there is some disagreement about how a story should be implemented. However, for most tasks, I found Planning Poker to be effective.

Title: Estimating tasks in Scrum


问:
Is an atomic action supposed to be deterministic?
I was reading about the at most once property that defines what an atomic action is and I've been curios about this example:

```<code>x = 0, y = 0
x = y + 1 || y = y + 1
</code>```

If we use the 'at most once' definition, we can say that, indeed, x = y + 1 is atomic because

y is only referenced in an another thread and

x is not being read by any thread

But the result of this action is not deterministic, it could be 1 or 2 depending on which operation is being computed first.

I might be wrong from the start, isn't atomicity suppose to grant some kind of determinism?

答:
There is no order guaranteed with multithreading, y = y + 1 may be run before or after x = y + 1, which would change the result.

Atomicity avoids any "intermediate" state in a variable. For example, if y is 64-bits long on a 32-bit system, setting it to a value is not naturally atomic, since the processor has to first set the lower 32 bits, then the upper ones. Reading y from another thread in that case would mean that there's a possibility that you'll get a version of y with only the lower 32 bits set.


答:
What is the "at most once" property? If you're discussing the definition of a term, I think it would be most useful to provide it.

As far as I understand it, an atomic action is supposed to guarantee that only two of the possible states are observable: either the action is carried out or it isn't. The operation is not divisible. This is supported by the page over at Wikipedia.

It seems to me that you are making an assumption in your post that some arithmetic or logical operation is atomic, but under general conditions this assumption is false.

Title: Is an atomic action supposed to be deterministic?

Body:
I was reading about the at most once property that defines what an atomic action is and I've been curios about this example:

```<code>x = 0, y = 0
x = y + 1 || y = y + 1
</code>```

If we use the 'at most once' definition, we can say that, indeed, x = y + 1 is atomic because

y is only referenced in an another thread and

x is not being read by any thread

But the result of this action is not deterministic, it could be 1 or 2 depending on which operation is being computed first.

I might be wrong from the start, isn't atomicity suppose to grant some kind of determinism?


答:
Yes and no.

Yes, an atomic action should always produce the same result given the same inputs.

No, a set of atomic actions, executed in parallel will not necessarily produce the same overall result. When steps are executed in parallel, the input(s) to a particular step may vary, in which case its output(s) may change accordingly.

Atomic actions are necessary but not sufficient to produce deterministic results.


答:
First, distinguish between elapsed time and billable time. You will not manage to get a full 8 hours billable every day for weeks on end, because things will happen. You'll get sick or your kids will, or you'll need to go to the bank or take your car in. You need to eat and drink and go to the bathroom, check your email and take phone calls from the client. One way to deal with this is to plan for 6 hours a day billable. If you do that, then unexpected things shouldn't make you miss your deadline, assuming the total effort that you estimated was correct.

Then there's the matter of extra effort. We call the money we don't get to bill because we estimated badly "tuition for estimating school." It hurts. You should definitely add contingency into any fixed bid. It might be 10% or even 20% if parts of the project are really vague but you're being asked for a fixed bid anyway. And be prepared to dig in your heels if the unexpected delays are things like "here's the format for the file you need - or should I say formats hee hee hee" then you need to raise a change request and arrange for more time and more money to handle it.

But sometimes the extra effort is your own fault - you did it wrong and now you have to do it over, you thought it would be simple but it wasn't - and you'll have to eat it. That's when you'll be grateful for the 6 hours a day pace - you may still have to give them an hour or two free every day making up for your mis-estimate, but you'll probably meet your deadline and end up with a happy client.

Title: Is it okay to add time for "unexpected delays" to a time estimate?

Body:
I've landed my first contract (hooray, self employment!) and the company is asking for time estimates.

Programmers are notoriously bad at time estimates, and I know I've been laughably wrong before.  It's fixed bid, so I'm not concerned about charging.  I'm just concerned about expectation management.

So far I've itemized the work I need to do, estimated the time it would take, then significantly padded that time.  I'm still nervous about it, though.  Is it acceptable to write in time for "unexpected delays"?  I've estimated 4 weeks, and I'd like to add a 5th week for problems I haven't thought of.  Is that something people do?  Would you balk if someone gave you an estimate with that in it?


答:
If you are sharing variables across multiple threads with no thread safety then you're going to have a bad time when it comes to determinacy.

First up. Atomic actions are deterministic unto themselves.

x = y + 1, will always yield you the value of y +1, whatever y happens to be at the time of execution. At a system level, the result of this statement is a possible race condition due to y being updated in a different thread.

This isn't technically a case of "non determinism", as would be if you were to do
x = y + 1 * rand() (without using a set seed). This is more of a straight up race condition because you're making assumptions about the state of variables that may or may not be correct.

The reality of this situation is "dont do that". Dont use shared variables across threads. Copy input information into threads, copy information out. Always ensure that your threads have the data they need to do their work, and ensure they never need to reach outside their local threadspace to do it.

If you absolutely completely and utterly need to hit shared data, you must use synchronization primitives, but it makes things a hell of a lot simpler if you adopt a paradigm where threads do not hit shared memory except to deposit results.

And at that point you can use things like lockless queues and other wonderful bits and pieces to avoid the whole lock/unlock procedure.

For information purposes. if you absolutely must share y between threads. I would advise using a gate style write lock to block access to y while it is being written to by the other thread. all other times y will be able to be read immediately as the gate would be "open" to readers.


问:
One Scheduler vs. Many Schedulers
I've been using a single scheduler to handle all my scheduled tasks (Quartz.Net).  I'm starting to rethink how this operates because if I need to update 1 task, I have to stop the entire process, release the update, and start it back up.

My Question: Is it better to have many Windows Services running individual tasks (or groups of similar tasks) or 1 Service running all of the tasks (my current set up)?

答:
I've spent a bit of time recently with Quartz.NET, and I can share some of the experiences we had. My scenario covered a single process (windows service) that was responsible for updating the existing schedule (using details downloaded as XML from a server). The service process also covered most of the functionality that could be triggered.

I think the key factor here is what you mean by "updating" a task. Do you mean simply altering scheduling details (what tasks to run, when to run them etc), or the actual binaries involved in executing the task? If you're expecting frequent updates to binaries and features, then a single monolithic model is probably not right for you. If you are only expecting frequent updates to when and what task should run, then it's worth centralizing the management of the tasks and scheduling.

Trigger groups work nicely for dividing up and managing various job types. This makes the CRUD against the in memory schedule much cleaner. For example, you can get all the jobs in a group fairly easily

Title: One Scheduler vs. Many Schedulers


问:
many include files vs one large include file
The company I work for is in the process of completely overhauling our current intranet site by moving it onto a Drupal framework. One area I am questioning is the number of include files we have. As it exists right now, every single .php page has a corresponding .inc page. There are .inc pages that provide certain functionality, but aren't tied to a specific page. However, each .php page includes multiple files - at a minimum, it will load the .inc page corresponding to it, and a few general scripts. This makes trying to find a function definition a nightmare when debugging.

My options right now seem to be the following:

1)Keep using this same system, but make it part of our convention to include a commented line stating where the function is defined
2) Consolidate all the include files into one large file. This would include class definitions, function definitions, everything. My concern here is that this file would get monolithic in a real big hurry. However, it would have the advantage of keeping all function definitions in one place.

I don't like either option to be honest. My goal is to reduce dependencies and make our code readable and easy to navigate. What is our best method to handle this problem?

I should also state that this is very much legacy code - it hasn't been refactored since it was created in 2003-2004. Just to give you an idea what I'm working with, there is a lot of php echoing html:

```<code>printf("<center>\n");
printf("<table>\n");
printf("<tr>\n");
printf("<td align=left>Select Division:</td>\n");
printf("<td><select name=\"div\">\n");
printf("  <option value=1 \"selected\">Division1\n");
printf("  <option value=2>Division2\n");
printf("</select></td>\n");
printf("</tr>\n");
printf("<tr>\n");
printf("<td align=center colspan=2><input type=\"submit\" value=\"Next->\"></td>\n");
printf("</tr>\n");
printf("</table>\n");
printf("</center>\n");
</code>```

答: I would convert that to a class-based system and separate the HTML templates and PHP code.</p>


问:
"Everything is a Map", am I doing this right?
I watched Stuart Sierra's talk "Thinking In Data" and took one of the ideas from it as a design principle in this game I'm making. The difference is he's working in Clojure and I'm working in JavaScript.  I see some major differences between our languages in that:

Clojure is idiomatically functional programming

Most state is immutable

I took the idea from the slide "Everything is a Map" (From 11 minutes, 6 seconds to > 29 minutes in).  Some things he says are:

Whenever you see a function that takes 2-3 arguments, you can make a case for turning it into a map and just passing a map in.  There are a lot of advantages to that:

You don't have to worry about argument order

You don't have to worry about any additional information.  If there are extra keys, that's not really our concern.  They just flow through, they don't interfere.

You don't have to define a schema

As opposed to passing in an Object there's no data hiding.  But, he makes the case that data hiding can cause problems and is overrated:

Performance

Ease of implementation

As soon as you communicate over the network or across processes, you have to have both sides agree on the data representation anyway.  That's extra work you can skip if you just work on data.

Most relevant to my question. This is 29 minutes in: "Make your functions composable". Here's the code sample he uses to explain the concept:

```<code>;; Bad
(defn complex-process []
  (let [a (get-component @global-state)
        b (subprocess-one a)
        c (subprocess-two a b)
        d (subprocess-three a b c)]
    (reset! global-state d)))

;; Good
(defn complex-process [state]
  (-> state
    subprocess-one
    subprocess-two
    subprocess-three))
</code>```

I understand the majority of programmers aren't familiar with Clojure, so I'll rewrite this in imperative style:

```<code>;; Good
def complex-process(State state)
  state = subprocess-one(state)
  state = subprocess-two(state)
  state = subprocess-three(state)
  return state
</code>```

Here are the advantages:

Easy to test

Easy to look at those functions in isolation

Easy to comment out one line of this and see what the outcome is by removing a single step

Each subprocess could add more information on to the state.  If subprocess one needs to communicate something to subprocess three, it's as simple as adding a key/value.

No boilerplate to extract the data you need out of the state just so that you can save it back in.  Just pass in the whole state and let the subprocess assign what it needs.

Now, back to my situation:  I took this lesson and applied it to my game.  That is, almost all of my high level functions take and return a gameState object.  This object contains all the data of the game.  EG: A list of badGuys, a list of menus, the loot on the ground, etc.  Here's an example of my update function:

```<code>update(gameState)
  ...
  gameState = handleUnitCollision(gameState)
  ...
  gameState = handleLoot(gameState)
  ...
</code>```

What I'm here to ask about is, have I created some abomination that perverted an idea that is only practical in a functional programming language?  JavaScript isn't idiomatically functional (though it can be written that way) and it's really challenging to write immutable data structures.  One thing that concerns me is he assumes that each of those subprocesses are pure. Why does that assumption need to be made?  It's rare that any of my functions are pure (by that, I mean they often modify the gameState.  I don't have any other complicated side effects other than that).  Do these ideas fall apart if you don't have immutable data?

I'm worried that one day I'll wake up and realize this whole design is a sham and I've really just been implementing the Big Ball Of Mud anti-pattern.

Honestly, I've been working on this code for months and it's been great.  I feel like I'm getting all the advantages he's claimed.  My code is super easy for me to reason about.  But I'm a one man team so I have the curse of knowledge.

Update

I've been coding 6+ months with this pattern. Usually by this time I forget what I've done and that's where "did I write this in a clean way?" comes into play. If I haven't, I'd really struggle. So far, I'm not struggling at all.

I understand how another set of eyes would be necessary to validate its maintainability. All I can say is I care about maintainability first and foremost. I'm always the loudest evangelist for clean code no matter where I work.

I want to reply directly to those that already have a bad personal experience with this way of coding.  I didn't know it then, but I think we're really talking about two different ways of writing code.  The way I've done it appears to be more structured than what others have experienced.  When someone has a bad personal experience with "Everything is a map" they talk about how hard it is to maintain because:

You never know the structure of the map that the function requires

Any function can mutate the input in ways you'd never expect.  You have to look all over the code base to find out how a particular key got into the map or why it disappeared.

For those with such an experience, perhaps the code base was, "Everything takes 1 of N types of maps." Mine is, "Everything takes 1 of 1 type of map". If you know the structure of that 1 type, you know the structure of everything.  Of course, that structure usually grows over time.  That's why...

There's one place to look for the reference implementation (ie: the schema). This reference implementation is code the game uses so it can't get out of date.

As for the second point, I don't add/remove keys to the map outside of the reference implementation, I just mutate what's already there. I also have a large suite of automated tests.

If this architecture eventually collapses under its own weight, I'll add a second update.  Otherwise, assume everything is going well :)

答:
I have found that my code tends to end up structured like so:

Functions that take maps tend to be larger and have side effects.

Functions that take arguments tend to be smaller and are pure.

I didn't set out to create this distinction but that is often how it ends up in my code.  I don't think using one style necessarily negates the other.

The pure functions are easy to unit test.  The larger ones with maps get more into the "integration" test area since they tend to involve more moving parts.

In javascript, one thing that helps a lot is using something like Meteor's Match library to perform parameter validation.  It makes it very clear what the function expects and can handle maps quite cleanly.

For example,

```<code>function foo (post) {
  check(post, {
    text: String,
    timestamp: Date,
    // Optional, but if present must be an array of strings
    tags: Match.Optional([String])
    });

  // do stuff
}
</code>```

See http://docs.meteor.com/#match for more.

:: UPDATE ::

Stuart Sierra's video recording of Clojure/West's "Clojure in the Large" also touches on this subject.  Like the OP, he controls side effects as part of the map so testing becomes much easier.  He also has a blog post outlining his current Clojure workflow which seems relevant.

Body:
I have found that my code tends to end up structured like so:

Functions that take maps tend to be larger and have side effects.

Functions that take arguments tend to be smaller and are pure.

I didn't set out to create this distinction but that is often how it ends up in my code.  I don't think using one style necessarily negates the other.

The pure functions are easy to unit test.  The larger ones with maps get more into the "integration" test area since they tend to involve more moving parts.

In javascript, one thing that helps a lot is using something like Meteor's Match library to perform parameter validation.  It makes it very clear what the function expects and can handle maps quite cleanly.

For example,

function foo (post) {
  check(post, {
    text: String,
    timestamp: Date,
    // Optional, but if present must be an array of strings
    tags: Match.Optional([String])
    });

  // do stuff
}

See http://docs.meteor.com/#match for more.

:: UPDATE ::

Stuart Sierra's video recording of Clojure/West's "Clojure in the Large" also touches on this subject.  Like the OP, he controls side effects as part of the map so testing becomes much easier.  He also has a blog post outlining his current Clojure workflow which seems relevant.


答:
I think there is nothing wrong with it as long as it makes sense from the design point of view. Just ask yourself those questions:

Is it sensible for tr to contain and expose t?

Is findIndexSmoothed() the responsibility of t?

If you answer yes to both of the questions then it is a way to go. Only thing I would do differently is to make t private and access it via getter.

Also I think it actually makes sense with the single responsibility principle. What if t had 5 different methods? would you delegate all of those from tr? how would you name them? findIndexSmoothedInTr()?

Title: Is it poor practice to call methods through multiple objects?

Body:
I have code that looks like this:

```<code>tr.t.findIndexSmoothed(arg0.getX(), arg0.getY());
</code>```

"tr" and "t" are objects. Is it bad practice to reach all the way down the object hierarchy to call methods? The only reason I can think of is that is breaks encapsulation, and if that's the case, can anybody tell me why that poses a problem? Also, does this code structure inhibit performance in any way?


问:
What to test when building websites using CMS?
My job is mainly building websites using CMS such as Drupal, eZPublish or Magento.

Most of the work is templating, CRUD and adhering to the specs with this. Occasionally, there is some business-specific logic, but it's usually 20 lines or so. Most of the logic is a bunch of if in the templates.

In this context, I find it hard to argue over unit testing. What should I test? Only integration tests? They usually can only be done at a very high level, such as using Selenium IDE. And Selenium is a pain to handle in project-mode products, because the requirements often change in 6 months and then the project is over. There are also other difficulties such as URLs being dynamic. (CMS don't always respect a RESTful architecture, especially for contents.)

I am in favor of unit testing. For libraries or framework-based projects, I easily write them and sets up a Jenkins server for them. But for CMS-based projects, I just don't know how and where to get started.

答:
You don't write unit tests just for sake of it, you write them when it is efficient way to reduce your bug count. For that unit tests are used in two ways:

To test lower layer components in isolation either to gain some trust in them before you write the higher layers or because it is easier way to debug than setting up the higher layers.

To make sure that you maintain the functionality when you need to restructure the components for technical reasons.

Now in CMS-based project the lower layer is the CMS, which probably has it's own tests and you are not modifying it, so you don't need to write tests for it yourself.

And in short projects just filling in pieces in a CMS you rarely restructure anything except when the requirements change. In which case you'd need to rewrite the test too.

So neither of the reasons apply to any significant extent. So I'd not bother with unit tests. I would not even try to go out of my way to automate the integration tests. Browser automation is fine, but if you just need to test it a few times and than it goes live and noone will touch it anymore unless the requirements change, you only need to test it a few times, so the automation may not save as much time testing to pay off. Especially since you need to do some manual integration testing anyway.


答:
To build on MichaelT's comment to your post, if the website is a local website that you have complete control of then you might be better off having it publish a notification of change that your app subscribes to.

Queues/topics will work for this, and it will give you the ability to scale up if required.

Title: Queues and threading

Body:
I am developing a new project where I will be constantly checking a webpage for data and adding this data to a queue for processing. This data will then be removed from the queue and added to a list if it hasn't already been added. This list will then be stored in the database.

I was wondering about how to use threads as I am relatively new to programming? I think it would be ideal if I had a thread constantly checking the webpage for data, a thread processing the queue and a thread for processing the list.

Any suggestions on whether this method is the most effective or not and how to achieve this would be much appreciated.