News for hackers. Stuff that matters.
Re:The most copied StackOverflow snippet of all time is flawed by Freak_NL on Wednesday December 04, 2019 @05:40AM (#21701116)
It's a shame that including unit tests makes the snippet non-trivial to copy quite fast.

Rust has a nice solution for this though: tests can also be embedded in documentation comments:

    /// Adds one to the number given.
    ///
    /// # Examples
    ///
    /// ```
    /// let arg = 5;
    /// let answer = my_crate::add_one(arg);
    ///
    /// assert_eq!(6, answer);
    /// ```
    pub fn add_one(x: i32) -> i32 {
        x + 1
    }
From: https://doc.rust-lang.org/book/ch14-02-publishing-to-crates-...

More languages could adopt that idea, and a good StackOverflow answer would include those tests in the snippet. StackOverflow might even automatically run the tests and add a passing/failing badge!

Re:The most copied StackOverflow snippet of all time is flawed by gimboland on Wednesday December 04, 2019 @06:04AM (#21701210)
In python since 1999: https://groups.google.com/forum/#!msg/comp.lang.python/DfzH5... :-)

(Though not in the stdlib til v2.1, April 2001)

Re:The most copied StackOverflow snippet of all time is flawed by LorenPechtel on Wednesday December 04, 2019 @04:38PM (#21707385)
What I would really like is for unit tests to become full fledged features of a language. Any object can contain a Test method (which would be static), this method contains all the unit test code for that object. Select "Run tests" from your compiler, it compiles everything and goes through calling any Test methods it found but the main entry point is never called. A release compile doesn't link the Test methods, nor any method marked [Test] (support functions only needed for testing.)
Re:The most copied StackOverflow snippet of all time is flawed by jeremyfranco on Wednesday December 04, 2019 @11:22AM (#21703495)