答:
The following practices are well known but often neglected for various reasons, often due to tight deadlines, so they deserve mentioning here (in effect, these are mechanism for spending time in advance to avoid spending more time later):

Do test-driven development; it'll help you write only the amount of code that is actually required and, will help you avoid the introduction of bugs when adding features or refactoring

Write comments, but only where the code is complex enough to warrant it

Refactor and simplify your code often

Use decent source control software (like Git or Mercurial) -if your employer uses something else, use your own locally-

Commit code changes often: for every feature or refactoring, do a commit, as reverting will be less costly to you if something goes awry

Don't be afraid to branch; Git especially has a very fast and "safe" branching mechanism (for instance, in comparison to Subversion)

Title: How to become a "faster" programmer?

OwnerDisplayName: Nick Toumpelis


问:
Is HTML5 XHTML 1.0 compliant?
For a school assignment, I wrote up a web-page in HTML5.

After looking over the assignment's requirements again though, I found the line:

• a minimum of three HTML pages complying with at least XHTML 1.0 standards or (better).

I'd really rather not need to add all the ugly tags to the top of the page, and change all my meaningful  and  tags to generic
s, so I tried validating it.

According to the W3C validator, my site checks out when I set it to validate it as XHTML 1.0

According to Validome though, it's not valid, and gives me errors for the lack of appropriate attributes at the top, and errors for each of the HTML5 tags that I used.

That leaves me with the question:

Is HTML5 at least XHTML 1.0 compliant?

The page:

```<code><!DOCTYPE html>
<html>
<head>
    <title>Brendon's Uromastyces Fact Site</title>
    <meta charset="utf-8">
</head>

<body>
    <div id="mainBody">
        <section>

            <header>
                <h1>Brendon's Uromastyces Fact Site</h1>
            </header>
            <h2>
                Welcome to my site dedicated to providing reliable, referenced information about Uromastyces.
            </h2>
            <p>
                My goal in creating this site was to allow fast access to the most important information surrounding
                Uromastyces. Especially regarding issues like the choice of substrate (the bedding that lines the
                bottom of the cage), there is a lot of contradictory information, which can be overwhelming for
                someone new to reptile keeping.
            </p>
            <p>
                Below, as well as to your left, are navigation bars you can use to explore each of the site's main
                topics: information relating to <em>Enclosures</em>, <em>Diet</em>, and general information about
                their <em>Behavior and Life</em>.
            </p>

            <nav class="navBar">
                <ul>
                    <!--    Only 3 of the links are currently valid: "Home" (this page; sample1.html), "Enclosure"
                            (sample2.html fixed up), and "About Me" (the third required page for the exercise)-->
                    <li>Home</li>
                    <li>Enclosure</li>
                    <li>Diet</li>
                    <li>Behavior and Life</li>
                    <li>About Me</li>
                </ul>
            </nav>

        </section>

    </div>

    <aside class="sideBar">
        <!--    This image is currently pretty big. Once I have access to CSS, I can have it automatically adjust the
                size. The width/height attributes seem to be considered deprecated (http://www.tutorialspoint
                .com/html/html_deprecated_tags.htm) in favor of using CSS. I'll have this adjust itself according to
                the screen size down the road to accommodate small screens. My goal will be to have the width of the
                nav bar == the width of the image, and have the height adjusted automatically.-->
        <img src="pascal-cropped-shrunk.jpg" alt="Picture of Pascal">
        <!--    Without being able to do any styling, this is going to sit below the main content body, when ideally it
            would float to the left-->
        <nav class="navBar">
            <ul>
                <!--    Unfortunately, this is copy-and-pasted from above. I'm not sure the best, most maintainable solution
                         would be at this point. If a link is updated, or the menu is changed, I want to ensure that all
                         menus reflect the change.
                         Down the road, I may generate the menus using JS (along with other content that doesn't vary
                          between pages), but that's not an option right now.
                         Also, since I can't arrange the elements yet, both of the nav menus are immediately on top of
                         each other, which is redundant. Later on, one will be a nav bar on the left, and the other a
                         quick access nav bar underneath the site introduction.-->
                <li>Home</li>
                <li>Enclosure</li>
                <li>Diet</li>
                <li>Behavior and Life</li>
                <li>About Me</li>
            </ul>
        </nav>

    </aside>

</body>
</html>
</code>```

Just an update:

The above page was accepted. I ended up using JS to generate the page, but the end result was nearly the same.

答:
Is HTML5 at least XHTML 1.0 compliant?

No.

If you mean in the sense that valid HTML5 is necessarily valid XHTML 1.0, then this plainly untrue: HTML5 permits unclosed tags for many elements, such as img, br, hr, which in a DOM would be interpreted in much the same way as XHTML self-closing tags would.

In your document, for instance, <meta charset="utf-8"> is an unclosed tag - legitimate in HTML5 but not XHTML 1.0.

XHTML documents must be XML documents, but HTML4/5 documents do not have to be valid XML, indeed part of the genesis of HTML5 was about avoiding requiring this sort of compliance with XML.

Furthermore, HTML5 introduces new elements not present in XHTML 1.0.

According to the W3C validator, my site checks out when I set it to validate it as XHTML 1.0

When I use that tool on the HTML you provided, the validator says there are 16 errors. You may have permitted the validator to detect the doctype, which would cause it to be validated as HTML5.

If you mean in the sense that valid XHTML is necessarily valid HTML5, this is also not the case, as some elements, such as tt and big are no longer valid in HTML5, and some attributes have changed values and/or meanings. You may find this document helpful (although it refers to HTML4 rather than XHTML).

The only thing which can be said is there exists a subset of HTML5 and XHTML which can exist in both documents. This subset looks more like XHTML - minus some older features - than HTML5. Even then, because both XHTML 1.0 and HTML5 have expectations about how they're 'introduced' (i.e. doctypes etc), there are few if any options for sending whole documents which truly conform to both.

If you want to fulfil the criterion "complying with at least XHTML 1.0 standards or (better)[sic].", and you are not able to clarify whether HTML5 is acceptable, if I were you, I would interpret this as XHTML 1.0 or 1.1 only. Version 2.0 only exists as an editor's draft/'working group note' and the working group on this has been closed.

ContentLicense: CC BY-SA 3.0

Title: Is HTML5 XHTML 1.0 compliant?

Body:
For a school assignment, I wrote up a web-page in HTML5.

After looking over the assignment's requirements again though, I found the line:

• a minimum of three HTML pages complying with at least XHTML 1.0 standards or (better).

I'd really rather not need to add all the ugly tags to the top of the page, and change all my meaningful  and  tags to generic
s, so I tried validating it.

According to the W3C validator, my site checks out when I set it to validate it as XHTML 1.0

According to Validome though, it's not valid, and gives me errors for the lack of appropriate attributes at the top, and errors for each of the HTML5 tags that I used.

That leaves me with the question:

Is HTML5 at least XHTML 1.0 compliant?

The page:

```<code><!DOCTYPE html>
<html>
<head>
    <title>Brendon's Uromastyces Fact Site</title>
    <meta charset="utf-8">
</head>

<body>
    <div id="mainBody">
        <section>

            <header>
                <h1>Brendon's Uromastyces Fact Site</h1>
            </header>
            <h2>
                Welcome to my site dedicated to providing reliable, referenced information about Uromastyces.
            </h2>
            <p>
                My goal in creating this site was to allow fast access to the most important information surrounding
                Uromastyces. Especially regarding issues like the choice of substrate (the bedding that lines the
                bottom of the cage), there is a lot of contradictory information, which can be overwhelming for
                someone new to reptile keeping.
            </p>
            <p>
                Below, as well as to your left, are navigation bars you can use to explore each of the site's main
                topics: information relating to <em>Enclosures</em>, <em>Diet</em>, and general information about
                their <em>Behavior and Life</em>.
            </p>

            <nav class="navBar">
                <ul>
                    <!--    Only 3 of the links are currently valid: "Home" (this page; sample1.html), "Enclosure"
                            (sample2.html fixed up), and "About Me" (the third required page for the exercise)-->
                    <li><a href="sample1Fixed.html">Home</a></li>
                    <li><a href="sample2Fixed.html">Enclosure</a></li>
                    <li><a href="diet.html">Diet</a></li>
                    <li><a href="behaviorAndLife.html">Behavior and Life</a></li>
                    <li><a href="page3.html">About Me</a></li>
                </ul>
            </nav>

        </section>

    </div>

    <aside class="sideBar">
        <!--    This image is currently pretty big. Once I have access to CSS, I can have it automatically adjust the
                size. The width/height attributes seem to be considered deprecated (http://www.tutorialspoint
                .com/html/html_deprecated_tags.htm) in favor of using CSS. I'll have this adjust itself according to
                the screen size down the road to accommodate small screens. My goal will be to have the width of the
                nav bar == the width of the image, and have the height adjusted automatically.-->
        <img src="pascal-cropped-shrunk.jpg" alt="Picture of Pascal">
        <!--    Without being able to do any styling, this is going to sit below the main content body, when ideally it
            would float to the left-->
        <nav class="navBar">
            <ul>
                <!--    Unfortunately, this is copy-and-pasted from above. I'm not sure the best, most maintainable solution
                         would be at this point. If a link is updated, or the menu is changed, I want to ensure that all
                         menus reflect the change.
                         Down the road, I may generate the menus using JS (along with other content that doesn't vary
                          between pages), but that's not an option right now.
                         Also, since I can't arrange the elements yet, both of the nav menus are immediately on top of
                         each other, which is redundant. Later on, one will be a nav bar on the left, and the other a
                         quick access nav bar underneath the site introduction.-->
                <li><a href="sample1Fixed.html">Home</a></li>
                <li><a href="sample2Fixed.html">Enclosure</a></li>
                <li><a href="diet.html">Diet</a></li>
                <li><a href="behaviorAndLife.html">Behavior and Life</a></li>
                <li><a href="page3.html">About Me</a></li>
            </ul>
        </nav>

    </aside>

</body>
</html>
</code>```

Just an update:

The above page was accepted. I ended up using JS to generate the page, but the end result was nearly the same.


问:
Is it possible to know what programming language a web-site uses?
For example, stackexchange.com, without asking the site owner or Google their information about developing the website, is this possible to know what language is used in the back end?

Seems, the website don't have .extension bar, for example .php that can indicated which is developed in PHP, but without the extension, how can I know that?

答: You should check out this site:  Built With.  It lets you know all that information if available.  </p>


问:
Number of parallel lines in a given set of lines
I am given N lines, i.e, I am given a,b,c for N lines. And each line is of the form ax + by + c = 0. I need to find out the maximum number of lines that are parallel to each other.

For this, first I create a 2 dimensional array of the ratio A/B (-slope) and C/B (-intercept), except for b == 0 (for which I take slope as 1/eps , I have defined eps as a very small positive number).

Then I remove the duplicate entries in $O(n^2)$ complexity.

Again I do $O(N^2)$ scans to find how many lines for a given slope exist and report the maximum number.

It turns out that this a slower way of doing this. How can I make this faster ?

Also, I am writing my code in C++.

答:
I would suggest creating a simple Slope object that simply contains a -A/B slope value.

Then for each line create a Slope object and save it to a Bag (counted set.)

Once you are done adding Slopes to the Bag, look for the Bag that contains the largest count. That's your largest number of parallel lines.


问:
Is it ever appropriate to lazy load properties?
Consider the following contrived program:

class Program
{
    static void Main(string[] args)
    {
        var myClass = new MyClass();
        var stuff = myClass.MyProperty; // this takes 5 seconds
        var stuff2 = myClass.MyProperty; // this is effectively instantaneous
    }
}

class MyClass
{
    private Lazy<IEnumerable<string>> _myProperty =
        new Lazy<IEnumerable<string>>(MyService.TimeConsumingLoadOperation);

    public IEnumerable<string> MyProperty
    {
        get { return _myProperty.Value; }
    }
}

class MyService
{
    public static IEnumerable<string> TimeConsumingLoadOperation()
    {
        Thread.Sleep(5000);
        return new List<string>();
    }
}
```

Drawing from part of CA1024:

Properties should behave as if they are fields; if the method cannot, it should not be changed to a property.

This makes a lot of sense to me. I wouldn't expect that accessing property could result in a noticeable delay, and the code above would be clearer if MyProperty was retrieved in a method call instead. To that end, are there situations where lazy loading a property (as opposed to using a method) would be appropriate?

答:
Yes, of course lazy loading a property is very often appropriate, since you only suffer the penalty once, while from that moment on retrieving the value of the property is instantaneous.

Someone might argue that it is unacceptable to suffer the loading penalty on the first query without expecting it; the reply to this is that on the contrary, it is unacceptable to suffer the preloading penalty during startup, (at which time we are usually overburdened any way,) by loading the value of a property that we are not even sure whether it will ever be queried.

Things become a bit more complicated if you consider scenarios where the property is not just load-once-query-forever-after, but load-infrequently-query-frequently.  How infrequent must the slow loading be to be balanced out by the frequent fast querying before it becomes necessary to advertise the potential slowness of any query by turning the property into a method?  This is a question that you have to answer on a case by case basis.

Related: How Much Logic in Getters


问:
Best practice in helper/util methods: IoC container or static methods?
So I'm developing an application which is using an IoC container. Now I need some helper methods for array manipulation like set, get, etc. I see two possible ways to go:

Using a class with static methods.

Using a class with non-static methods registered in the IoC container.

When using method 1, it will look something like this:

```<code>$arr = array(); // Defining $arr
Arr::set($arr, 'a', 'b'); // Sets 'a' => 'b' in $arr
echo Arr::get($arr, 'a'); // Outputs 'b'
</code>```

And using method 2, I will first register a new Arr class in the IoC container and than using it would look something like this:

```<code>$arr = array(); // Defining $arr
$app['arr']->set($arr, 'a', 'b'); // Sets 'a' => 'b' in $arr
echo $app['arr']->get($arr, 'a'); // Outputs 'b'
</code>```

According to my benchmarks, method 1 is almost twice as fast as method 2. So in terms of performance method 1 is the way to go (I think). But, static methods are generally considered bad practice. So in terms of best practices, I should use method 2 to avoid static methods.

My question is if I should use the IoC container for helper methods or just use the static methods.

答:
Either way works.  Personally, I have written similar classes for things like array manipulation and math functions.  I used static methods because those things are true functions and do not operate on any encapsulated data, so it seemed to make more conceptual sense.  Plus, I didn't want to have to instantiate an object whenever I called one of those methods.

Some people say to avoid static methods.  It is true that they can have downside.  My personal opinion is that static methods are fine for things like utilities classes like what you seem to be writing.


问:
Finding the smallest sub-collection of sets that intersect to produce one element
I came up with a problem that's similar to the Set Cover problem but which I can't reduce down to any know problem. Does anyone have a good algorithm for solving this?

Given a set of elements U = {1, 2, ..., m} (the universe) and a collection S of n sets whose union equals the universe, what's the smallest sub-collection of S that intersects to produce a single element?

For example:

```<code>U = {1, 2, 3, 4, 5}
S = {{1, 4}, {2, 4}, {5, 2}, {1, 4, 5}}
</code>```

If we want the smallest intersection that produces 4, we would need to intersect at least 2 sets {1, 4} and {2, 4}.

The current naive algorithm I have to do this involves

Choose a set with the desired element

Iterate over all unchosen sets, try intersecting with them and see if the intersection produces less elements than the original set but still include the desired element

For those that intersect and reduce the number of elements, we repeat the process with the intersected set recursively until we've tried all sets or we're down to the element

For a concrete application: I'm trying to find the minimum set of CSS classes on an HTML element that will select only that element.

答:
Unfortunately your task is NP complete.

It is obvious that all sets from your solution should contain that single element (lets name it x). Also it is obvious that sets which doesn't contain x can't be included in the solution, so lets agree that they won't be present in S.

Lets create a new collection of sets S' = {S'k | Sk∈S | S'k = U\Sk} (i.e. every set consists of those items from universe that it didn't contain initially). In other words S'k defines the elements from U that will be removed from the intersection if we take Sk.

Now in order to solve the task you should select the minimum number of sets from S' which will cover U. Which is the definition of Set Cover problem you've mentioned in the beginning.


答:
There's nothing wrong with static methods. That's one of those things people say to sound smart and shocking, but the reality is much more subtle and can't be explained in the title of a blog post!

So why not static methods? Static methods can't be part of a contract. That means you can't implement more than one version and you can't swap those versions seamlessly behind the contract. Thing is, that might not matter. Would you ever need more than one version of a math min function? I hope not.

Be careful though, it's not always simple to decide if you need more than one version of a function. Take file manipulation. You should only ever need one version of those functions, right? Well, er, no. The problem shows up when you want to test your code. Say you have a little chunk of code you want to test and it has a dependency on a file function. Testing that code gets tricky if your system isn't set up exactly like the developer who originally wrote the test. Maybe you're missing a file or your file is slightly different or your file permissions are different or... It gets complicated pretty quick. Soon, you realize you're testing that stupid file function more than you're testing the function that depends on it. That's no good.

The solution is to provide a second version of the file function that's perfectly predictable. If it always behaves the same, you're free to test the code that depends on it without worrying that it might zig when you expect it to zag. Problem solved; but remember, you can't have two versions of a static method.

So back to your question. I'll say this:

Don't worry about performance until you've confirmed it's a problem. Beautiful, maintainable code is more important than performance at this point in the game.

Array manipulation is a perfect candidate for static methods. You'll never need more than one version because there should only be one way to do the tasks you're defining and there's no test-ruining dependencies on the file system, database, network, etc...

Title: Best practice in helper/util methods: IoC container or static methods?

Body:
So I'm developing an application which is using an IoC container. Now I need some helper methods for array manipulation like set, get, etc. I see two possible ways to go:

Using a class with static methods.

Using a class with non-static methods registered in the IoC container.

When using method 1, it will look something like this:

```<code>$arr = array(); // Defining $arr
Arr::set($arr, 'a', 'b'); // Sets 'a' => 'b' in $arr
echo Arr::get($arr, 'a'); // Outputs 'b'
</code>```

And using method 2, I will first register a new Arr class in the IoC container and than using it would look something like this:

```<code>$arr = array(); // Defining $arr
$app['arr']->set($arr, 'a', 'b'); // Sets 'a' => 'b' in $arr
echo $app['arr']->get($arr, 'a'); // Outputs 'b'
</code>```

According to my benchmarks, method 1 is almost twice as fast as method 2. So in terms of performance method 1 is the way to go (I think). But, static methods are generally considered bad practice. So in terms of best practices, I should use method 2 to avoid static methods.

My question is if I should use the IoC container for helper methods or just use the static methods.


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

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

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

Any feedback is greatly appreciated, thanks,

答:
I personally think its all about code re-usability.  Unless your doing fully custom things every single time, you should have a library of common use functions you can turn to.  I have a utils.php in which i have a whole "junkyard" of functions that i have used on previous projects.  Saves a TON of time when i have to do something similar.

Good luck and don't get discouraged.  I think we've all felt slow or "dumb" at times.  I know i have!

OwnerDisplayName: Nick Gotch

ContentLicense: CC BY-SA 2.5

Title: How to become a "faster" programmer?

OwnerDisplayName: Code Monkey


问:
Javascript Password Security
I am having difficulty with the answer provided here, but I couldn't understand how to implement it. My code is pretty much identical:

```<code><script language="javascript">
  function check(form) {
    if(form.userName.value == "User" && form.userPass.value == "averyobviouspassword") {
      window.open('testok/menu.html')
    }
    else {
      alert("Wrong Password Or Username")
    }
  }
</script>
</code>```

The problem with this is, if you were to Inspect Element on the page, you can plainly see the password. I'm not looking for other solutions like a SQL database, because this is just a test website. The solution I thought might work was this:

```<code>if(hash(enteredPassword) == storedHash)
</code>```

I just don't know how to implement it.

答:
The previous comments are all valid - but if you are just looking to do a bit of obfuscation, you can use base64 conversion functions atob() and btoa(). See below:

```<code><script language="javascript">
  function check(form) {
    if(form.userName.value == "User" && atob(form.userPass.value) == "j÷«Êï¬¥«,ÂÝ") {
      window.open('testok/menu.html')
    }
    else {
      alert("Wrong Password Or Username")
    }
  }
</script>
</code>```

This still uses your original password "averyobviouspassword".

Title: Javascript Password Security

Body:
The previous comments are all valid - but if you are just looking to do a bit of obfuscation, you can use base64 conversion functions atob() and btoa(). See below:

<script language="javascript">
  function check(form) {
    if(form.userName.value == "User" && atob(form.userPass.value) == "j÷«Êï¬¥«,ÂÝ") {
      window.open('testok/menu.html')
    }
    else {
      alert("Wrong Password Or Username")
    }
  }
</script>

This still uses your original password "averyobviouspassword".


问:
Identifying plagiarized jokes?
I'd like to be able to identify duplicate jokes posted on a website. I can build up a reasonably large database of previously-posted jokes, and then I'd like to look at each new joke as it comes in and pick out the most "similar" jokes from the database and spit them out, in order, if my confidence that the new joke is a duplicate is above some threshold.

I could look for exact character-by-character matches but that's not very interesting, and it would fail to catch "near duplicates." For example, I'd like the following two jokes to be considered the same:

Three men walk into a bar. The first one says "ouch!", the second one says "ouch!" The third one ducks.

Three men go to the bar. The first one says "ow!!!!", the second one says "OWW!!!!!", the third one ducks.

I imagine that many duplicate jokes involve names of characters being swapped out (i.e. a story about "Little Suzy" rather than "Little Johnny", or a joke about the president with "Barack Obama" swapped for "George Bush"), and I'd like those to be counted as the same joke, too.

I've been reading about plagiarism detection algorithms and none of them seem to quite fit, since they're geared towards much larger texts (the average joke is probably only a few dozen words), or they rely on some special property of academic work (i.e. citation analysis).

I found a paper at one point that I haven't been able to find again that I thought was an interesting that went something like this: collapse each text into a vector v such that if the ith word of your vocabulary appears n times, then the value of v_i is n. Then when you get a new joke, compute the distance between it and all of the other vectors you've built; the closest vector is the most likely source text. It explored a few different variations on the idea and compared their effectiveness.

This seems like a sensible idea to me, but I wonder if it won't be too sensitive to things like swapping out names, and I can't find the paper again to check the exact details of how they handled it.

I'm not sure how I could apply other machine learning techniques to this, like an SVM or neural net, since it's basically a classification problem over a few thousand different classes with no more than a handful of examples for each class. And there's no nicely-labeled dataset out there of duplicate jokes, as far as I know.

What's a reasonable strategy for this? Can anyone point me in the direction of useful literature?

(This project is purely for fun, and I'm just looking for some semi-reasonable approximation. I know that NLP in general is very hard to do and that a real, accurate answer to this would be virtually impossible.)

EDIT (in response to the dupe flag): as I've sketched out in some comments, I don't think that string edit distance is a great approach to this because:

it places inappropriate weight on swapping out things consisting of multiple characters that shouldn't change the meaning (names, for example)

it requires me manually figuring out a good threshold for a positive match; there's no dataset for me to test it on

it cares about order: I'd rather have something like a bag-of-word approach because a joke can be substantively the same while flipping around sentences

it's potentially expensive: whenever I see a new entry, I need to recompute its string edit distance against every single joke I've seen

it's not very interesting (I'm doing this for fun, after all) and I'm not convinced that there isn't an algorithm that better captures the notion of "joke similarity" than straight character-by-character comparison; if someone has evidence that string edit distance is the best tool for this job, I'm open to hearing that --- but that's not an argument provided by the answer linked in the dupe flag

答:
The simplest, naive algorithm to account for name changes skewing your edit-distance results would be to filter out proper names.

English is relatively easy to identify proper names and there are probably established algorithms and libraries out there for it. We used something similar using a Part Of Speech tagger a few years ago, but we were using (expensive) commercial packages. Again, there are probably other alternatives out there. Then, just identify proper names like Johnny or Suzy or Barack Obama, strip them out, and run an edit distance between what's left.

There are many ways to improve results based on that basic idea - try to identify common variations in duplicates and remove them as noise from your edit distance. For example, you can match and replace known synonyms with canonical representations so that "bar/pub" or "enter/go in/walk into" would be collapsed to one representation. Assuming typos and spelling mistakes are an option, a "Did you mean..." tagger could also help.

Title: Identifying plagiarized jokes?


问:
Are there CPUs that perform this possible L1 cache write optimization?
When the CPU with an L1 cache does a write, what normally happens is that (assuming that the cache line that it is writing to is already in the L1 cache) the cache (in addition to updating the data) marks that cache line as dirty, and will write the line out with the updated data at some later time.

One possible optimization would be to have the cache compare the contents of the write and the previous contents of the cache, and if they're the same, don't mark the line as dirty.  Because this might allow the cache to avoid write-backs on occasion, I can see how the CPU manufacturer might see this as worth the gates needed to do this logic.

My question: are there CPUs that perform this optimization?

Background as to why I'm asking: I'm writing some code that needs to have constant memory accesses; that is, someone who is able to listen into the behavior of the cache should not be able to deduce what I'm doing.  Some of my accesses are writes, and in the obvious way to implement this code, a lot of the writes will be writing the same data that's already there. I need to do the writes because, depending on the data, the data I'm writing may or may not be the same, and it's important to perform the same action regardless.  If the CPU optimizes by not actually writing a 'no-change-write', that would mean that the behavior of the cache would vary depending on what I'm doing, which would subvert my goal.

So, is there a CPU that tries to optimize writes in this way?

答:
From hours of searching, I wasn't able to find a CPU that uses this specific optimization. Most of the optimizations mentioned usually related to hit/miss with read/write operations and data access:

(pages 7 and )
https://cseweb.ucsd.edu/classes/fa14/cse240A-a/pdf/08/CSE240A-MBT-L15-Cache.ppt.pdf

However, that doesn't mean that this optimization can't be performed. In general, it is possible to programmatically access the size of a CPU cache line. It is also possible to access current values in cache registers - but it's somewhat dangerous to do so. If you access the wrong registers at a bad time, you could be tampering with ones related to a running program. Or you could inadvertently modify the contents of the lines you're trying to read.

Obtaining current value in register's cache

Furthermore, all of the theoretical solutions require some form of software implementation (assembler). The closest I've found relates to the ARM architecture, which appears to allow for cache manipulation. In addition to this, you would also need to know the size of a cache line for your desired CPU. You could carefully read the cache contents to a secondary location in memory, in line-sized increments, and compare it to data that is about to be written to the registers (or L1 cache lines, in this case).

Read CPU cache contents

From there, you could devise a software-based system that prevents identical rewrites. While this is a bit simplified, it is so because the solution has to be applicable for any CPU that exists.

Another possibility that I found related to Cache coherence:

Relevant passage from a Wikipedia article about acche coherence

The main point that caught my attention, in relation to this issue, was the Snarfing description:

It is a mechanism where a cache controller watches both address and data in an attempt to update its own copy of a memory location when a second master modifies a location in main memory. When a write operation is observed to a location that a cache has a copy of, the cache controller updates its own copy of the snarfed memory location with the new data.

In other words, there are possibly mechanisms already in place. It's just that they might not be used for the optimization you have suggested. You would have to implement software that performed the read/write comparison.


答:
Use static analysis tools.

They help you find more bugs at compile-time you would otherwise had to track down at runtime.

Especially when building multithreaded applications they are a REAL help.

Title: How to become a "faster" programmer?

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

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

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

Any feedback is greatly appreciated, thanks,

OwnerDisplayName: Helper Method


问:
Why is checking weird bit errors important in garbage collectors?
Here's a rough translation of a tweet by @xharaken.

"Space rays hit computers and mess up the bits in the memory" "hardware bugs mess up the bits in the memory" such things almost never happen, but they happen on a nearly daily basis when there are so many users using a software like [Google] Chrome. So the GC running inside Chrome has a function to check such bit anomalies.

The original:

「宇宙線が降ってきてメモリのビットが狂う」「ハードウェアのバグでメモリのビットが狂う」というのは非常にまれな現象だけど、Chromeくらいのユーザ数規模になるとけっこう日常的に起きます。なのでChromeで走ってるGCには、それらのビット異常を検知する機構をわざわざ入れてます。

I do understand that such weird errors happen, but I don't understand how or why checking such errors is important in writing a garbage collector.
What I thought was the job of garbage collectors have nothing to do with data integrity in the memory. Think about it - what a garbage collector does is it wipes data in the memory that are no longer referenced. Weird bugs caused by random memory bugs is not in a, at least, say, obvious way, related to a garbage collector's job.

答: In any complex, memory-intensive application, random bit errors can be hellishly hard to debug. Anything that helps you detecting them is very useful when your software is deployed to millions of devices. A garbage collector is simply the point where adding functionality to detect such errors produces the smallest overhead and has a good chance of succeeding, because it's the garbage collector's job to constantly inspect the application's memory anyway.


问:
Key / Value store development porting to modern C++
I am developing a database server similar to Cassandra.

Development were started in C, but things became very complicated without classes.

Currently I ported everything in C++11, but I am still learning "modern" C++ and have doubts about lot of things.

Database will work with Key / Value pairs. Every pair have some more information - when is created also when it will expire (0 if not expires). Each Pair is immutable.

Key is C string, Value is void *, but at least for the moment I am operating with the value as C string as well.

There are abstract IList class. It is inherited from three classes

VectorList - C dynamic array - similar to std::vector, but uses realloc

LinkList - made for checks and performance comparison

SkipList - the class that finally will be used.

In the future I might do Red Black tree as well.

Each IList contains zero or more pointers to pairs, sorted by key.

If IList became too long, it can be saved on the disk in a special file. This special file is kind of read only list.

If you need to search for a key,

first in memory IList is searched (SkipList, SkipList or LinkList).

Then search is send to the files sorted by date

(newest file first, oldest file - last).

All these files are mmap-ed in memory.

If nothing found, then key is not found.

I have no doubts about the implementation of the IList things.

What is currently puzzling me is following:

The pairs are with different size, they are allocated by new() and they have std::shared_ptr pointed to them.

```<code>class Pair{
public:
    // several methods...
private:
    struct Blob;

    std::shared_ptr<const Blob> _blob;
};

struct Pair::Blob{
    uint64_t    created;
    uint32_t    expires;
    uint32_t    vallen;
    uint16_t    keylen;
    uint8_t     checksum;
    char        buffer[2];
};
</code>```

"buffer" member variable is the one with different size. It stores the key + value.

E.g. if key is 10 characters, and value is another 10 bytes, the whole object will be sizeof(Pair::Blob) + 20 (buffer have initial size of 2, because of two null terminating bytes)

This same layout is used on the disk as well, so I can do something like this:

```<code>// get the blob
Pair::Blob *blob = (Pair::Blob *) & mmaped_array[pos];

// create the pair, true makes std::shared_ptr not to delete the memory,
// since it does not own it.
Pair p = Pair(blob, true);

// however if I want the Pair to own the memory,
// I can copy it, but this is slower operation.
Pair p2 = Pair(blob);
</code>```

However this different size is a problem on lots of places with C++ code.

For example I can not use std::make_shared(). This is important for me, because if I have 1M Pairs, I would have 2M allocations.

From the other side, If I do "buffer" to dynamic array (e.g. new char[123]), I will lose mmap "trick", I will have do two dereferences if I want to check the key and I will add single pointer - 8 bytes to the class.

I also tried to "pull" all members from Pair::Blob into Pair, so Pair::Blob to be just the buffer, but when I tested it, it was quite slow, probably because of copying the object data around.

Another change I am also thinking is to remove Pair class and replace it with std::shared_ptr and to "push" all methods back to Pair::Blob, but this will not help me with variable size Pair::Blob class.

I am wondering how I can improve on the object design in order to be more C++ friendly.

The full source code is here:

https://github.com/nmmmnu/HM3

答:
The approach I would recommend is to focus on the interface of your key-value store, so as to make it as clean as possible and as nonrestrictive as possible, meaning that it should allow maximum freedom to the callers, but also maximum freedom for choosing how to implement it.

Then, I would recommend that you provide an as bare as possible, and as clean as possible implementation, without any performance concerns whatsoever.  To me it seems like unordered_map should be your first choice, or perhaps map if some kind of ordering of keys must be exposed by the interface.

So, first get it to work cleanly and minimally; then, put it to use in a real application; in doing so, you will find what issues you need to address on the interface; then, go ahead and address them.  Most chances are that as a result of changing the interface, you will need to rewrite big parts of the implementation, so any time you have already invested on the first iteration of the implementation beyond the bare minimum amount of time necessary to get it to just barely work is time wasted.

Then, profile it, and see what needs to be improved in the implementation, without altering the interface.  Or you may have your own ideas about how to improve the implementation, before you even profile.  That's fine, but it is still no reason to work on these ideas at any earlier point in time.

You say you hope to do better than map; there are two things that can be said about that:

a) you probably won't;

b) avoid premature optimization at all costs.

With respect to the implementation, your main issue appears to be memory allocation, since you seem to be concerned with how to structure your design in order to work around problems that you foresee that you are going to have with respect to memory allocation. The best way to address memory allocation concerns in C++ is by implementing a suitable memory allocation management, not by twisting and bending the design around them.  You should consider yourself lucky that you are using C++, which allows you to do your own memory allocation management, as opposed to languages like Java and C#, where you are pretty much stuck with what the language runtime has to offer.

There are various ways of going about memory management in C++, and the ability to overload the new operator may come in handy. A simplistic memory allocator for your project would preallocate a huge array of bytes and use it as a heap.  (byte* heap.) You would have a firstFreeByte index, initialized to zero, which indicates the first free byte in the heap.  When a request for N bytes comes in, you return the address heap + firstFreeByte and you add N to firstFreeByte.  So, memory allocation becomes so fast and efficient that it becomes virtually no issue.

Of course, preallocating all of your memory may not be a good idea, so you may have to break your heap into banks which are allocated on demand, and keep serving allocation requests from the at-any-given-moment-newest bank.

Since your data are immutable, this is a good solution.  It allows you to abandon the idea of variable length objects, and to have each Pair contain a pointer to its data as it should, since the extra memory allocation for the data costs virtually nothing.

If you want to be able to discard objects from the heap, so as to be able to reclaim their memory, then things become more complicated: you will need to be using not pointers, but pointers to pointers, so that you can always move objects around in the heaps so as to reclaim the space of deleted objects.  Everything becomes a bit slower due to the extra indirection, but everything is still lightning fast compared to using standard runtime library memory allocation routines.

But all this is of course really useless to be concerned with if you don't first build a straightforward, bare-minimal, working version of your database, and put it to use in a real application.


问:
Sorting Array before looping : best practice
I was going through JBAKE code at

https://github.com/jbake-org/jbake/blob/master/src/main/java/org/jbake/app/Asset.java : 58

PFB the code.

Why are we sorting the array here?

```<code>    if (assets != null) {
        //TBD : Why is sorting required?
        Arrays.sort(assets);
        for (int i = 0; i < assets.length; i++) {
            if (assets[i].isFile()) {
                StringBuilder sb = new StringBuilder();
                sb.append("Copying [" + assets[i].getPath() + "]...");
                File sourceFile = assets[i];
                File destFile = new File(sourceFile.getPath().replace(source.getPath()+File.separator+config.getString(ConfigUtil.Keys.ASSET_FOLDER), destination.getPath()));
                try {
                    FileUtils.copyFile(sourceFile, destFile);
                    sb.append("done!");
                    LOGGER.info(sb.toString());
                } catch (IOException e) {
                    sb.append("failed!");
                    LOGGER.error(sb.toString(), e);
                    e.printStackTrace();
                    errors.add(e.getMessage());
                }
            }

            if (assets[i].isDirectory()) {
                copy(assets[i]);
            }
        }
    }
</code>```

答:
The comparator of File performs a lexicographic comparison of pathnames.  So, these files are sorted by name.  So, obviously, the author wants the filesystem tree to be traversed in alphabetical order, and not in any other order.  That's important, because according to the documentation of File.listFiles():

There is no guarantee that the name strings in the resulting array
    will appear in any specific order; they are not, in particular,
    guaranteed to appear in alphabetical order.

Java specifically refrains from giving such a guarantee because different host environments (i.e. Windows vs. Linux) are known to yield files in different orders, and sometimes even the host environment may refrain from giving any such guarantee.  On the other hand, sorting the files can be expensive, so the creators of Java decided to pass this uncertainty to the programmer, rather than imposing the extra overhead of sorting on every single java application out there.

So, the author wants to have a guaranteed order in which the files will be traversed.  This is useful:

When debugging, because the behavior of your program does not change from debug run to debug run due to factors that are beyond your control.  If you notice that file "aardvark.txt" was yielded first, and you try to see what happens when processing aardvark, and you set a breakpoint at some place and rerun, it is very annoying to discover that on the next run "zoology.txt" is processed first.

When testing, because you can create a temporary directory structure, create a tree of files in it, run your function on it, obtain the results in a new tree, and then make sure that the trees are exactly equal, without having to account for the fact that the trees may be equal but not-in-the-same-order.

In general, when writing software that you want to behave in a consistent way across different platforms.


答: For me, getting good productivity is having a clear idea about what you are trying to achieve, and how you will get there.</p>

Title: How to become a "faster" programmer?

OwnerDisplayName: mdma


问:
Why take a String input instead of int input for a number?
I'm a beginner in Java and I have a question from the "Head First Java" book. In the book, the author creates a method that takes the number as String and then convert it into int. Why do I need to take the input as a string and convert it rather than directly take it as an int. Below is he screenshot that I'm talking about.

答: Simply because the user's guess is most probably entered through a mechanism which only supports entering chars, or strings, but no other types.  For example, System.in.read() most GUIs have a text-input control, but not a numeric input control.  So, the user enters text in the control, and then the program must convert the text to a number.


答:
Writing to L1 cache is a very, very time critical operation.

Writing the exact same data back seems to be rather rare. An optimisation that speeds things up in this particular case isn't going to get a lot of speedup in total.

On the other hand, this optimisation requires a comparison of old data and new data on every single write to cache memory. What makes this worse, is that it requires that the data to be written has to be actually available at the time of the write!

That is usually not the case on a modern CPU. The data to be written may still be being calculated for example. The cache can still go ahead, load the cache line if needed, mark the cache line as modified and so on, even before the calculation is finished. All the book keeping can already be performed except for the actual modification of the cache line. If you want to compare newly written result and old cache line data, that isn't possible.

As an example, if you have C code a [i] = x / y; the division x / y takes an extraordinary long time to perform on most CPUs. However, most of the work needed to handle storing the result to a [i] has happened long before the division finishes; the only thing missing is the move of eight result bytes to the cache line. An operation flushing the cache line will automatically wait until the division is finished. An operation reading a [i] will likely be redirected to get the result straight from the divider.

Title: Are there CPUs that perform this possible L1 cache write optimization?

Body:
When the CPU with an L1 cache does a write, what normally happens is that (assuming that the cache line that it is writing to is already in the L1 cache) the cache (in addition to updating the data) marks that cache line as dirty, and will write the line out with the updated data at some later time.

One possible optimization would be to have the cache compare the contents of the write and the previous contents of the cache, and if they're the same, don't mark the line as dirty.  Because this might allow the cache to avoid write-backs on occasion, I can see how the CPU manufacturer might see this as worth the gates needed to do this logic.

My question: are there CPUs that perform this optimization?

Background as to why I'm asking: I'm writing some code that needs to have constant memory accesses; that is, someone who is able to listen into the behavior of the cache should not be able to deduce what I'm doing.  Some of my accesses are writes, and in the obvious way to implement this code, a lot of the writes will be writing the same data that's already there. I need to do the writes because, depending on the data, the data I'm writing may or may not be the same, and it's important to perform the same action regardless.  If the CPU optimizes by not actually writing a 'no-change-write', that would mean that the behavior of the cache would vary depending on what I'm doing, which would subvert my goal.

So, is there a CPU that tries to optimize writes in this way?


答: It depens on the actuall method you're using. If your using System.in.read() you need to convert to int, because read() reads bytes. But if you use a Scanner you can directly read as an int like int i = new Scanner(System.in).nextInt()

Title: Why take a String input instead of int input for a number?

Body: I'm a beginner in Java and I have a question from the "Head First Java" book. In the book, the author creates a method that takes the number as String and then convert it into int. Why do I need to take the input as a string and convert it rather than directly take it as an int. Below is he screenshot that I'm talking about.


问:
What actually happens in inheritance (java)?
```<code>class A {
    int a;
    int b;

    A(int i,int z) {
        a = i;
    }

    void abc() {
        System.out.println("a is " + a);
        System.out.println("b is " + b);
    }
}
</code>```

```<code>class B extends A {
    int a = 55;
    int b = 66;

    B() {
        super(14,13);
        super.b = 99;
    }
}
</code>```

```<code>public class test {
    public static void main(String[] argue) {
          B obj = new B();
          obj.abc();
    }
}
</code>```

output:

a is 14
b is 99
```

<p>I would refer to the above code for my questions. I don't have some basic OOP concepts vivid. Please clear my doubts regarding the following queries:</p>

<ol>
<li>What actually happened when class B extended A? Was the code from A copied pasted in B, each variable defined in A is now defined in B also and each method definition in A is now also in B?</li>
<li>As a class is just a template it lists the attributes that the object will have and the memory will only be allocated for each variable definition in a class when its object is made. Now if you say that when class B extended A the code from class A was not copied pasted into B i.e. the variables (int a, int b) originally defined in A their definitions are not pasted in class B.When the object of class B is created it allocates memory for variables 'a' and 'b' in class B, not of class A as their definition is not copied pasted in B. How can class B use the super class variables 'a' and 'b' even though the super class wasn't instantiated i.e. 'a' and 'b' don't exist in memory as class just defines a template. Does the compiler allocate separate memory portion for 'a' and 'b' on special consideration for class B as it extended class A? What about the method defined in the class A?</li>
<li><p>I was expecting obj.abc() (<em>the last statement in the main method</em>) to print:</p>

<pre class="lang-none prettyprint-override"><code>a is 55
b is 66
</code>```

as the method abc() was originally defined in class A and now class B inherits A so it inherited abc(). So if I called abc() with a reference of class B's object it should have printed the values of 'a' and 'b' in class B i.e.

 a is 55
 b is 66
```

答:
The output you describe does not correspond to the code you posted. It actually prints

```<code>a is 14
b is 99
</code>```

I'll get to why that's happening later. But first, I'll rewrite your code into a form that demonstrates the relationships typically found in inheritance:

```<code>public class A {
    int a;
    int b;

    A(int i, int z) {
        a = i;
        b = z;
    }

    void abc() {
        System.out.println("a is " + a);
        System.out.println("b is " + b);
    }
}

public class B extends A {
    B() {
        super(14, 13);
    }
}

public static void main(String[] argv) throws Exception {
    B obj = new B();
    obj.abc();
}
</code>```

Which gives the output

```<code>a is 14
b is 13
</code>```

This is inheritance in its simplest form, and has the following characteristics:

An instance of B contains two member variables, a and b. These variables are defined by class A.

When you invoke the method abc() on an instance of B, the JRE determines that the method is actually defined in class A. This is known as dynamic dispatch, and is one of the key features of inheritance.

Now a modification:

```<code>public class B extends A {
    B() {
        super(14, 13);
    }

    @Override
    void abc() {
        System.out.println("b is " + b);
        System.out.println("a is " + a);
    }
}
</code>```

Here I've overridden the method abc() in class B. When the JVM examines the class, it determines that B has defined the method, so invokes that implementation and does not look for an implementation in A.

Because B extends A, and the member variables a and b are not declared private by A, B has access to those variables. The output is:

```<code>b is 13
a is 14
</code>```

Another modification:

```<code>public class B extends A {
    B() {
        super(14, 13);
        super.b = 99;
    }

    @Override
    void abc() {
        System.out.println("b is " + b);
        System.out.println("a is " + a);
    }
}
</code>```

This highlights the construction chain. In an inheritance hierarchy, a class' constructor must call a superclass' constructor. So here, B() explicitly calls A(int,int), which implicitly calls Object().

The ordering is important: immediately after the call super(14,13), the value of member variable b is 13. However, the B() constructor changes that value, so the program prints

```<code>b is 99
a is 14
</code>```

I left the assignment as super.b = 99, but in this case it could also have been just been b = 99, because class B has access to the variable defined by A. However, this form becomes important in the next revision.

Let's bring this program one step closer to your original:

```<code>public class B extends A {

    int a = 55;
    int b = 72;

    B() {
        super(14, 13);
        super.b = 99;
    }

    @Override
    void abc() {
        System.out.println("b is " + b);
        System.out.println("a is " + a);
    }
}
</code>```

In this case I'm redefining the variables a and b within class B. These definitions shadow the definitions from class A. This is where it gets tricky -- and why, in production code, shadowing is a code smell.

There are now four variables in the class instance. Two are defined by class A, and two are defined by class B.

The code in class B will ignore the variables defined by A

Unless you explicitly prefix the variable reference with super

So, after construction, here are the four variables:

a, defined by A, with the value 14

b, defined by A, with the value 99

a, defined by B, with the value 55

b, defined by B, with the value 72

Note that the variable b defined by A was explicitly changed by B's constructor, after it was assigned by A's constructor, because B's constructor referred to it with super.

However, the definition of method abc() in B doesn't prefix those variables with super, so it will print the values of the variables defined by B:

```<code>b is 72
a is 55
</code>```

Last step: I remove the definition of function abc() from class B.

```<code>public class B extends A {

    int a = 55;
    int b = 72;

    B() {
        super(14, 13);
        super.b = 99;
    }
}
</code>```

Now the JRE again invokes the function defined in A. However, this implementation only has access to the two variables that are also defined by A; it has no knowledge of the two variables defined by B even though they exist within the object instance. So the output is

```<code>a is 14
b is 99
</code>```

Why isn't b 13? Because the constructor for B explicitly referenced the variable defined by A.

Anyone who feels the need to add references to specs should feel free to do so; I don't think they're necessary.

Body:
The output you describe does not correspond to the code you posted. It actually prints

a is 14
b is 99

I'll get to why that's happening later. But first, I'll rewrite your code into a form that demonstrates the relationships typically found in inheritance:

public class A {
    int a;
    int b;

    A(int i, int z) {
        a = i;
        b = z;
    }

    void abc() {
        System.out.println("a is " + a);
        System.out.println("b is " + b);
    }
}

public class B extends A {
    B() {
        super(14, 13);
    }
}

public static void main(String[] argv) throws Exception {
    B obj = new B();
    obj.abc();
}

Which gives the output

a is 14
b is 13

This is inheritance in its simplest form, and has the following characteristics:

An instance of B contains two member variables, a and b. These variables are defined by class A.

When you invoke the method abc() on an instance of B, the JRE determines that the method is actually defined in class A. This is known as dynamic dispatch, and is one of the key features of inheritance.

Now a modification:

public class B extends A {
    B() {
        super(14, 13);
    }

    @Override
    void abc() {
        System.out.println("b is " + b);
        System.out.println("a is " + a);
    }
}

Here I've overridden the method abc() in class B. When the JVM examines the class, it determines that B has defined the method, so invokes that implementation and does not look for an implementation in A.

Because B extends A, and the member variables a and b are not declared private by A, B has access to those variables. The output is:

b is 13
a is 14

Another modification:

public class B extends A {
    B() {
        super(14, 13);
        super.b = 99;
    }

    @Override
    void abc() {
        System.out.println("b is " + b);
        System.out.println("a is " + a);
    }
}

This highlights the construction chain. In an inheritance hierarchy, a class' constructor must call a superclass' constructor. So here, B() explicitly calls A(int,int), which implicitly calls Object().

The ordering is important: immediately after the call super(14,13), the value of member variable b is 13. However, the B() constructor changes that value, so the program prints

b is 99
a is 14

I left the assignment as super.b = 99, but in this case it could also have been just been b = 99, because class B has access to the variable defined by A. However, this form becomes important in the next revision.

Let's bring this program one step closer to your original:

public class B extends A {

    int a = 55;
    int b = 72;

    B() {
        super(14, 13);
        super.b = 99;
    }

    @Override
    void abc() {
        System.out.println("b is " + b);
        System.out.println("a is " + a);
    }
}

In this case I'm redefining the variables a and b within class B. These definitions shadow the definitions from class A. This is where it gets tricky -- and why, in production code, shadowing is a code smell.

There are now four variables in the class instance. Two are defined by class A, and two are defined by class B.

The code in class B will ignore the variables defined by A

Unless you explicitly prefix the variable reference with super

So, after construction, here are the four variables:

a, defined by A, with the value 14

b, defined by A, with the value 99

a, defined by B, with the value 55

b, defined by B, with the value 72

Note that the variable b defined by A was explicitly changed by B's constructor, after it was assigned by A's constructor, because B's constructor referred to it with super.

However, the definition of method abc() in B doesn't prefix those variables with super, so it will print the values of the variables defined by B:

b is 72
a is 55

Last step: I remove the definition of function abc() from class B.

public class B extends A {

    int a = 55;
    int b = 72;

    B() {
        super(14, 13);
        super.b = 99;
    }
}

Now the JRE again invokes the function defined in A. However, this implementation only has access to the two variables that are also defined by A; it has no knowledge of the two variables defined by B even though they exist within the object instance. So the output is

a is 14
b is 99

Why isn't b 13? Because the constructor for B explicitly referenced the variable defined by A.

Anyone who feels the need to add references to specs should feel free to do so; I don't think they're necessary.


问:
Is it normal to spend as much, if not more, time writing tests than actual code?
I find tests a lot trickier and harder to write than the actual code they are testing. It's not unusual for me to spend more time writing the test than the code it is testing.

Is that normal or am I doing something wrong?

The questions “Is unit testing or test-driven development worthwhile?”, “We are spending more time implementing functional test than implementing the system itself, is this normal?” and their answers are more about whether testing is worth it (as in "should we skip writing tests altogether?"). While I'm convinced tests are important, I'm wondering if my spending more time on tests than actual code is normal or if it's only me.

Judging by the number of views, answers and upvotes my question received, I can only assume its a legitimate concern that isn't addressed in any other question on the website.

答:
Is it normal to spend as much, if not more, time writing tests than actual code?

Yes, it is. With some caveats.

First of all, it is "normal" in the sense that most big shops work this way, so even if this way was completely misguided and dumb, still, the fact that most big shops work this way makes it "normal".

By this I don't mean to imply that testing it is wrong. I have worked in environments with no testing, and in environments with obsessive-compulsive testing, and I can still tell you that even the obsessive-compulsive testing was better than no testing.

And I do not do TDD yet, (who knows, I might in the future,) but I do the vast majority of my edit-run-debug cycles by running the tests, not the actual application, so naturally, I work a lot on my tests, so as to avoid as much as possible having to run the actual application.

However, be aware that there are dangers in excessive testing, and specifically in the amount of time being spent on maintaining the tests. (I am primarily writing this answer in order to specifically point this out.)

In the preface of Roy Osherove's The Art of Unit Testing (Manning, 2009) the author admits to having participated in a project which failed to a large part due to the tremendous development burden imposed by badly designed unit tests which had to be maintained throughout the duration of the development effort. So, if you find yourself spending too much time doing nothing but maintaining your tests, this does not necessarily mean that you are on the right path, because it is "normal".  Your development effort might have entered an unhealthy mode, where a radical rethinking of your testing methodology might be necessary in order to save the project.


答: Being excited about what you're doing is a great way to increase efficiency. Make sure it's fun!</p>

Title: How to become a "faster" programmer?

OwnerDisplayName: Jakob


问:
Use a hash character or a dot when referring to methods and fields in software documentation?
Assuming I have this class (Java code only for the sake of example):

class Person {
    private String name;

    public void setName(String name) {
        this.name = name;
    }
}
```

When I write documentation for this application and/or in-code documentation, what should I use to refer to a particular symbol, whether that be type, method or field?

When I describe a type, I usually say just "Person", keeping the casing of the type. When I refer to a method, I always do a dot: "Person.setName()". Depending on context, I might add the parameter list in order to be technically correct: "Person.setName(String)". I do not change the notation whether or not the method is static.

But field is a bit harder and I am not sure, despite being a programmer for many years now, that I have understood this part correctly. My heart want to always use a hash character: "Person#name". But I believe some of us use one or the other depending on context, whether or not the field is a static? And if so, which character to use when?

If dot is used, then the only disambiguation that exist between a field- and method reference is the presence of two parentheses, which in my mind is not enough. For example; "Person.name" is syntactically similar to "Person.setName()". But there's no commonality, in terms of special characters, between "Person#name" and "Person.setName()". Also, to my point.. is the fact that many of us are sloppy and actually refer to methods without parenthesis making the ambiguity absolute.

Also, if dot is supposed to be used in reference of a field, then some paragraphs of text will become rather awkward to read. For example:

Person is a class used in our application. A person has a Person.name.
  A person may also die.

Note that "name" is surrounded by a dot explosion. Versus:

Person is a class used in our application. A person has a Person#name.
  A person may also die.

All feed back is welcome. The answer that has a legit source or otherwise speak of community best practices is accepted. Me personally, I've googled but found nothing.

答:
Actually, it is not an opinion poll, there is a clear answer mandated by the guidelines for writing doc comments, and followed by IDEs.

From your sample code it is obvious that you are talking about Java.

You pretty much have to use '#', since that's what is mandated by the standard, (see Oracle: How to Write Doc Comments for the Javadoc Tool) and that's the only way your doc comment will be understood by your IDE, so that when you hover the mouse over a function call you can see a nice tooltip explaining to you what the function does, what parameters it accepts, and what value it returns.

Oracle's standard is a bit outdated, and we would very much like to see it replaced by something more modern, but until that happens, hashes are the way to go.


问:
MVC Set or Read View Components
I often see the follwing example when reading about MVC (example is in Java, but question adapts to all OOP languages):

```<code>class View{
    JTextField tfName;

    public String getTfNameText(){
        return this.tfName.getText();
    }

    public void setTfNameText(String text){
        this.tfName.setText(text);
    }
}
</code>```

And then in the Controller something like:

```<code>class Controller{
    View v = new View();
    ...
    public void actionPerformed(ActionEvent evt){
        v.setTfNameText("Button Clicked!");
    }
    ...
}
</code>```

But is this really considered good? Because as far as I understand for every GUI Component I have to add a get and set method and the Controller must know every of these methods, which makes it not very reusable. Can anyone tell me if this is the "correct"(i.e. an widely accepted) way of updating gui components in MVC?

Thanks in advance

答:
If your controller has to import javax.swing.JTextField you are doing it wrong.

You have a dependency on a package which your controller should know nothing about.

(Then your controller is not testable without integrating the UI, testing with a UI is a pain, etc etc.)

Title: MVC Set or Read View Components


答:
Mike Nakis wrote an excellent Java centric response.

For other languages, you need to follow the similarly established conventions.

Ruby, as an example, uses "::" for class  methods, and "#" for instance methods.

The point, however, is simply this: you are not writing the documentation for only your personal consumption, but for other programmers. Learn the conventions and standards for the language you are using and stick to them.

Title: Use a hash character or a dot when referring to methods and fields in software documentation?

Body:
Assuming I have this class (Java code only for the sake of example):

class Person {
    private String name;

    public void setName(String name) {
        this.name = name;
    }
}
```

When I write documentation for this application and/or in-code documentation, what should I use to refer to a particular symbol, whether that be type, method or field?

When I describe a type, I usually say just "Person", keeping the casing of the type. When I refer to a method, I always do a dot: "Person.setName()". Depending on context, I might add the parameter list in order to be technically correct: "Person.setName(String)". I do not change the notation whether or not the method is static.

But field is a bit harder and I am not sure, despite being a programmer for many years now, that I have understood this part correctly. My heart want to always use a hash character: "Person#name". But I believe some of us use one or the other depending on context, whether or not the field is a static? And if so, which character to use when?

If dot is used, then the only disambiguation that exist between a field- and method reference is the presence of two parentheses, which in my mind is not enough. For example; "Person.name" is syntactically similar to "Person.setName()". But there's no commonality, in terms of special characters, between "Person#name" and "Person.setName()". Also, to my point.. is the fact that many of us are sloppy and actually refer to methods without parenthesis making the ambiguity absolute.

Also, if dot is supposed to be used in reference of a field, then some paragraphs of text will become rather awkward to read. For example:

Person is a class used in our application. A person has a Person.name.
  A person may also die.

Note that "name" is surrounded by a dot explosion. Versus:

Person is a class used in our application. A person has a Person#name.
  A person may also die.

All feed back is welcome. The answer that has a legit source or otherwise speak of community best practices is accepted. Me personally, I've googled but found nothing.


答: The only clear thing I've found that works is to be free of distraction.  Which, in some environments, is impossible.  But being able to focus on the specific task and ONLY on that task will likely outweigh anything else.  Those context switches are really big time sinks.</p>

Title: How to become a "faster" programmer?

OwnerDisplayName: Joe


问:
Can someone give me a good definition of the term "business logic"?
I kind of get what it means, but not enough to explain it by myself. Is a key concept when talking about the MVC pattern and it's giving me problems to explain that concept too. </p>

答: The business logic is the part of the software where you implement algorithms to solve the problem that you're trying to solve with your program. It's the part where the real calculations happen. The other parts are for database access, exporting to different file formats, visual representations. </p>


问:
New Kind of VM Type
I have designed my own kind of vm, a sort of stack-register. It works by having a stack but instead of pushing and popping values, you simply store the values on the stack, and add together different addresses.

For example add would be #add addressTo, address1, address2. It might sound like a register machine, but everything is stored on the stack instead of register and local variables on the stack.

The reason I believe this is a good idea is because you don't have to load memory from and to registers, but have the advantages of a register machine. Is there a flaw in my design or why is it not already used or is it completely new which I doubt?

答:
Some early computers worked in the way you described, by addressing memory directly without any registers. The problem with this approach was that quite soon, accessing storage was much slower than the processor. Therefore, modern computer architectures use many levels of caching — from slow hard disks, RAM, various caches L3-L1, to the fast registers. Different architectures take this to different extremes. E.g. in MIPS, all instructions only operate on registers, and there are dedicated load and store instructions. In contrast, the x86 mov instruction is Turing-complete.

In VM design, we have more freedom when designing an architecture.

There are low-level VMs such as the LLVM that are concerned about being easily compilable to actual machine code. LLVM uses a kind of register architecture where each register can only be assigned once, but you have no restrictions on the number of registers. This makes it easy to produce efficient register allocations for actual CPUs. This LLVM representation also makes it easy to implement optimizations such as common-subexpression-elimination. The big disadvantage is that not having a built-in stack requires us to manually cope with memory when the local state does not fit into the available registers. Once your code actually writes to memory, it becomes much harder to optimize.

High-level register based VMs do exist (Parrot…), but are very uncommon.

There also are high-level VMs such as the Perl implementation. Here, instructions are extremely high-level such as “apply a regular expression” or “dispatch a method”. The Perl VM uses a stack architecture which simplifies a lot — compare “add the top two values on the stack” with “add registers A and B and store in C”. Code for a stack machine can be generated quite directly from an abstract syntax tree, but register allocations are more tricky (if you have a limited number of registers). More importantly, registers would not give us any advantages considering the Perl memory model — every value is represented as a pointer to a struct.

More low-level stack based languages do also exist, e.g. the class of concatenative languages (Forth, PostScript). Since operations always operate on the top few values of the stack, they can be composed quite flexibly. However, the stack is used as a semantic model here, not necessarily as an implementation strategy. For a low-level VM, stack machines are attractive because they have very compact object code (also why the JVM is stack based), since instructions do not need to store their argument locations. Unfortunately, they make it difficult to perform some optimizations, and can potentially require a lot of memory read/writes.

Your “hybrid” architecture (actually, more like a random access machine without any registers, that happens to have a stack) would certainly work, but it has none of the advantages of a register machine, being speed and relative optimizability. It also has none of the desirable qualities of a stack machine, since stack machines are more common in high-level VMs where direct memory access is not a thing, or in low-level scenarios where we need compact code.

Title: New Kind of VM Type


答:
In MVC, the Controllers are very application specific. So the Business Logic should stand inside the MODEL for reuse.

MVC could also mean that the "Controllers" encapsulate the business logic for the "Views". Views in this sense are skinny, Models can be skinny or fat (depending on the validations and data-specific calculations needed), and Controllers handle the functionality container by the Views (the when/where/how).

Title: Can someone give me a good definition of the term "business logic"?

Body: I kind of get what it means, but not enough to explain it by myself. Is a key concept when talking about the MVC pattern and it's giving me problems to explain that concept too. </p>


答:
"Business Logic"* is the part of your application which deals exclusively with the rules, processes, and algorithms that are necessary in order to solve the problem at hand (in other words, for the application to fulfill its raison d'être) without any concern whatsoever to peripheral concerns such as precisely how to receive input from the user, precisely how to present stuff to the user, precisely how to persist stuff to the database, and in general, without any concern to the nitty-gritty technical details of how to convince the machine to perform the low-level operations necessary in order to solve the problem.

* I prefer the terms "Application Logic" or "Domain Logic".


答:
I really enjoy listening to music while I program because I feel like it relaxes me and I can focus.

A comfortable chair. I don't ever use my school's computer labs to program because the office chairs are incredibly uncomfortable.

Eat something beforehand because nothing kills my motivation like nagging hunger.

Title: How to become a "faster" programmer?

OwnerDisplayName: Matt Phillips


答:
In layman's terms:

Imagine this: in the eighties you wrote a piece of software that allowed people to track their personal finance. That software showed reports in text-only mode in a phosphor screen and saved data to diskette files.

Later in the nineties you rewrote the program to work in Windows graphical environment. You had to make cool windows and dialog boxes and accept mouse input, and save the info into MS Access files. That part that does the financial tracking didn't change a lot, you had to re-write in another language, added some adjustments but the most part didn't change a lot.

Then in the  2010's you rewrote the app to be web based, and data were stored in a MySQL database in some cloud. The financial tracking part of the software didn't change in a dramatic way, it improved, maybe you had to translate it to a more modern dialect but taking good care not to break it. It continues to be based on the things your grandpa taught you before computers were mainstream. Your grandpa was an accountant and he knew how to do all the financial stuff with just pen, paper and a desktop calculator. The knowledge of your grandpa is what you put in your software to begin with in the form of rules, legal ordinances, workflow, algorithms, conditions etc.

Well, the part of the software that changed the least in all that evolution is what we call business logic. The part your grandpa knew how to do manually. We used to call it know how, business rules, etc. Now, in the software world, the part of the source code of a program that does the calculations and applies conditions and transformations if called business logic.

Business logic is presentation-agnostic and persistent-agnostic. Meaning it doesn't care whether a report is shown in an iPhone screen or printed to a dot matrix printer, and it also doesn't care whether the data is saved to files, to the cloud or has to be typed every time by an operator.


问:
What encoding are the HTTP status and header lines?
If I was going to write a parser for HTTP, would I be able to assume the encoding of the HTTP headers and status line? Until I read the charset or encoding header, how could I tell what the encoding type was? I am given the impression that these lines will always be in ASCII.

I guess I am confused how HTTP handles various encodings within the same stream of data. I am getting the impression the status line and the headers can be in a different encoding than the body. Even in the case where the body is made up of multipart form-data, it sounds like the body has a single encoding. Some clarification/explanation would go a long way.

答:
RFC 7230, the relevant part of the current version of the spec, is pretty clear and to the point:

3. Message Format

[…]

  A recipient MUST parse an HTTP message as a sequence of octets in an   encoding that is a superset of US-ASCII*. Parsing an HTTP message as a stream of Unicode characters, without regard for the specific encoding, creates security vulnerabilities due to the varying ways that string processing libraries handle invalid multibyte character sequences that contain the octet LF (%x0A).

This allows for (at minimum) using a conformant UTF-8 parser, because UTF-8 avoids encoding confusing ASCII-subset characters in its multibyte code units, so e.g. %x0A will always be correctly recognized as an actual LF character.

There's a further note that, once you've successfully parsed the basic message into its header key-value pairs plus the message body, you can begin parsing the pieces with a more relaxed or non-default approach, according to certain headers. This is especially useful with RFC 7231's Content-Type header:

3.1.1.1.  Media Type

HTTP uses Internet media types [RFC2046] in the Content-Type (Section 3.1.1.5) and Accept (Section 5.3.2) header fields in order to provide open and extensible data typing and type negotiation.

RFC2046 is all about extending MIME to message bodies, and has in turn a nice clear section on the Charset parameter:

4.1.2.  Charset Parameter

A critical parameter that may be specified in the Content-Type field for "text/plain" data is the character set.  This is specified with a "charset" parameter, as in:

```<code>Content-type: text/plain; charset=iso-8859-1
</code>```

It goes on to explain that other text/ media types should use the same charset semantics.

Note that Content-Encoding, Transfer-Encoding, and Content-Transfer-Encoding (obsolete) all refer to a very limited set of encodings for compression or chunking — not character sets.

*American National Standards Institute, "Coded Character Set -- 7-bit American Standard Code for Information Interchange", ANSI X3.4, 1986.

Body:
RFC 7230, the relevant part of the current version of the spec, is pretty clear and to the point:

3. Message Format

[…]

  A recipient MUST parse an HTTP message as a sequence of octets in an   encoding that is a superset of US-ASCII*. Parsing an HTTP message as a stream of Unicode characters, without regard for the specific encoding, creates security vulnerabilities due to the varying ways that string processing libraries handle invalid multibyte character sequences that contain the octet LF (%x0A).

This allows for (at minimum) using a conformant UTF-8 parser, because UTF-8 avoids encoding confusing ASCII-subset characters in its multibyte code units, so e.g. %x0A will always be correctly recognized as an actual LF character.

There's a further note that, once you've successfully parsed the basic message into its header key-value pairs plus the message body, you can begin parsing the pieces with a more relaxed or non-default approach, according to certain headers. This is especially useful with RFC 7231's Content-Type header:

3.1.1.1.  Media Type

HTTP uses Internet media types [RFC2046] in the Content-Type (Section 3.1.1.5) and Accept (Section 5.3.2) header fields in order to provide open and extensible data typing and type negotiation.

RFC2046 is all about extending MIME to message bodies, and has in turn a nice clear section on the Charset parameter:

4.1.2.  Charset Parameter

A critical parameter that may be specified in the Content-Type field for "text/plain" data is the character set.  This is specified with a "charset" parameter, as in:

Content-type: text/plain; charset=iso-8859-1

It goes on to explain that other text/ media types should use the same charset semantics.

Note that Content-Encoding, Transfer-Encoding, and Content-Transfer-Encoding (obsolete) all refer to a very limited set of encodings for compression or chunking — not character sets.

*American National Standards Institute, "Coded Character Set -- 7-bit American Standard Code for Information Interchange", ANSI X3.4, 1986.


问:
How to make git automatically merge increased build numbers?
I like having build numbers, just because it is cool. Please don't judge me :D.

However, I develop on two machines (one OS X and one Windows for portability checks). I have on both machines a script that automatically increases the build number every time I build the project. However, when doing changes on both machines without synchronising with git, this obviously gives a merge conflict, because the file holding the build number changed on both machines.

Is there a neat way to automatically merge these files? I would be cool if I could define a rule like:

If file buildnumber.txt has a merge conflict, run script auto-merge-build-number.sh and continue commit.

Where auto-merge-build-number.sh would be a simple script that parses the conflicted file and adds up the two numbers (found on the conflicting lines) and rewrites the file with that sum. I don't know if this is possible in git or not.

There is another way I can think of myself, but is less neat, imho: Keep track of a build counter file on every machine that is used only for that machine. If the project is build on that machine, the build counter file for that machine is increased. Afterwards, all files holding build numbers are added up together and the result is written to a file that is in the .gitignore. This way, there would be no merge conflicts by developing on multiple machines. Only merge conflicts by developing on multiple branches. I don't really like this approach because you need n+1 files to keep track of the total build number (when having n machines) and the script on every machine needs to be slightly different, to make it increase the right file.

Any thoughts on how this could be done?

答:
After searching some more, with different vocabulary, I found this post:
http://nuclearsquid.com/writings/git-tricks-tips-workflows/ (section about .gitattributes)

Basically, this solution is the solution I was looking for and consists of two steps:

Create a merge strategy (for example called: buildnumbermerge) using a scripting language of choice and put it in .git/config (see link for details).

Specify which file should use which merge strategy. I.e.: buildnumber.txt should have merge strategy buildnumbermerge by specifying that in .gitattributes with a line like this:

```<code>buildnumber.txt merge=buildnumbermerge
</code>```

See also:

https://stackoverflow.com/questions/23140240/git-how-do-i-add-a-custom-merge-strategy

Body:
After searching some more, with different vocabulary, I found this post:
http://nuclearsquid.com/writings/git-tricks-tips-workflows/ (section about .gitattributes)

Basically, this solution is the solution I was looking for and consists of two steps:

Create a merge strategy (for example called: buildnumbermerge) using a scripting language of choice and put it in .git/config (see link for details).

Specify which file should use which merge strategy. I.e.: buildnumber.txt should have merge strategy buildnumbermerge by specifying that in .gitattributes with a line like this:

buildnumber.txt merge=buildnumbermerge

See also:

https://stackoverflow.com/questions/23140240/git-how-do-i-add-a-custom-merge-strategy


答:
Firstly, can you get away with using commits instead of builds?

git describe or git rev-list --count --first-parent HEAD can give you something like a revision number. Be careful if you have branches, it will do the wrong thing, but so will any build number.

Secondly, if you insist on using build numbers you want a custom merge driver.

See the documentation here: http://git-scm.com/docs/gitattributes#_defining_a_custom_merge_driver

Basically, you should be able to write a script to do whatever you want during merging.

Title: How to make git automatically merge increased build numbers?

Body:
I like having build numbers, just because it is cool. Please don't judge me :D.

However, I develop on two machines (one OS X and one Windows for portability checks). I have on both machines a script that automatically increases the build number every time I build the project. However, when doing changes on both machines without synchronising with git, this obviously gives a merge conflict, because the file holding the build number changed on both machines.

Is there a neat way to automatically merge these files? I would be cool if I could define a rule like:

If file buildnumber.txt has a merge conflict, run script auto-merge-build-number.sh and continue commit.

Where auto-merge-build-number.sh would be a simple script that parses the conflicted file and adds up the two numbers (found on the conflicting lines) and rewrites the file with that sum. I don't know if this is possible in git or not.

There is another way I can think of myself, but is less neat, imho: Keep track of a build counter file on every machine that is used only for that machine. If the project is build on that machine, the build counter file for that machine is increased. Afterwards, all files holding build numbers are added up together and the result is written to a file that is in the .gitignore. This way, there would be no merge conflicts by developing on multiple machines. Only merge conflicts by developing on multiple branches. I don't really like this approach because you need n+1 files to keep track of the total build number (when having n machines) and the script on every machine needs to be slightly different, to make it increase the right file.

Any thoughts on how this could be done?


问:
How do you define stateless methods while keeping them together in Objective-C?
I have a few methods which are stateless:

```<code>loginWithEmail: password: completion:
signUpWithEmail: password: completion:
resetPasswordForUsername: completion:
</code>```

Currently what I do is I create a class which holds these stateless methods:

```<code>@interface KGAccountService : NSObject

+ (void)//method1
+ (void)//method2
+ (void)//method3

@end
</code>```

I then would call the methods from some place like a view controller that a user is using to log in.

```<code>[KGAccountService loginWithEmail:@"email" password:@"password" completion:{
    // user logged in or error
}];
</code>```

A co-worker of mine is pretty adamant that even though the class only houses class methods and does not need any sort of state, it should be implemented using the singleton pattern and be called through that:

```<code>@interface KGAccountService : NSObject

+ (instanceType)sharedInstance;
- (void)//method1
- (void)//method2
- (void)//method3

@end
</code>```

Then:

```<code>[[KGAccountService sharedInstance] loginWithEmail:@"email" password:@"password" completion:{
// user logged in or error
}];
</code>```

I don't see the point in implementing the singleton pattern when it is not needed and my co-worker was not able to get his point of view through to me.  Is there a design pattern which I am missing and is my way of creating a class to house stateless class methods correct?

答:
There is no reason to wrap stateless global functions as class methods unless you just want to make the name of the function longer... And if that's all you want to do, then just make the name longer. For this reason, I strongly recommend against the approach you are considering. It accomplishes nothing useful.

As for your co-worker's idea, if you are just going to preface every call to these functions with [[KGAccountService sharedInstance] ... (or [[KGAccountService alloc] init] ...) then again, you aren't accomplishing anything but making the name of the function longer.

Now if you plan on injecting the object into other objects so you can easily replace it with a different object then your co-worker's idea has some merit... Except the sharedInstance method becomes superfluous. Don't bother putting it in.

And then there is the point that @gnasher729 made... It doesn't feel right for these methods to be stateless. They are likely connected by some underlying state in some way (just based on the names of the functions.)


问:
What really is the "runtime environment"?
This is a very basic question but is something I've never completely understood and recently, when studying .NET Core and ASP.NET 5 I felt the need of a more complete understanding of the topic.

Reading the article Introduction to the Common Language Runtime (CLR) there we find the following piece of text:

Every program has a surprising number of dependencies on its runtime environment. Most obviously, the program is written in a particular programming language, but that is only the first of many assumptions a programmer weaves into the program. All interesting programs need some runtime library that allows them to interact with the other resources of the machine (such as user input, disk files, network communications, etc). The program also needs to be converted in some way (either by interpretation or compilation) to a form that the native hardware can execute directly.

Now, this idea of runtime environment seems to be very basic, but still very important, not just when working with .NET but when dealing with programming in general. It seems to be a general concept which is quite important to understand.

Until today I always had one intuitive and simple understanding about it: runtime environment is the environment on which the code will run. But this is a quite loose way to understand it. There is probably much more to it as can be infered from the above text.

In that setting: what really is the runtime environment in general? Not just for .NET, but in programming in general, what is the runtime environment? Is it just something conceptual or is it some piece of software, like the CLR for .NET? In summary, how should we properly understand the idea of runtime environment?

答:
what really is the runtime environment in general?

Yes, your broad understanding "runtime environment is the environment on which the code will run" is about as specific as that term will be defined.

Not just for .NET, but in programming in general, what is the runtime environment?

Effectively, the hardware your code is running on. The OS your code is running on. The libraries made available to your code when running. The environment variables that your code has access to. The locale your program is running in. (If applicable) the JIT compiler/byte-code interpreter/garbage collector/sandbox/app server your code is running on.

In summary, how should we properly understand the idea of runtime environment?

It varies from job to job. Many programs don't care very much at all, because it doesn't really matter. Some programs need to care a great deal, because the platform their running on doesn't do a great job at abstracting away hardware differences, or because the code will vary its behavior depending on the environment it's running in.

But as a term, knowing "runtime environment" generally refers to the intermediary "stuff" needed to run managed languages like C# and Java is sufficient.