News for hackers. Stuff that matters.
Re:The memory safety problem isn't bad coders by wheelie_boy on Tuesday February 12, 2019 @12:57PM (#19145805)
In general, in favor of as many correctness and other checks at compile time as possible. Make tools as powerful as possible. I really liked this tweet:

"What if...

- your programming language required you to write useful docs,

- using those docs, it checked your program for mistakes,

- it even used the docs to speed up your program,

- this feature already exists!

And what if it was called static typing."

- https://twitter.com/DWvanGeest/status/1092095822559358976

Re:The memory safety problem isn't bad coders by mhink on Tuesday February 12, 2019 @02:17PM (#19146533)
Although I mostly agree with you, it's worth noting that static typing only goes so far. When I worked in a large-scale Java codebase, it always seemed like half the code only existed in order to "work around" the type system. (Don't even get me started on Spring, which might as well be a whole additional language on TOP of the actual application code.) I'm perfectly willing to grant that might just be a Java thing, but still, it was a huge and constant frustration.

Because of the above, types really don't seem like "useful docs" to me. I get extremely irritated when a library links to its API documentation and it's all just autogenerated lists of methods which proudly state their parameter and return types but say absolutely nothing about what they do.

That's one major thing I've always appreciated about the Ruby and Javascript ecosystems: because they can't rely on type information, most libraries go out of their way to describe how they work in depth.

I'm willing to grant that I didn't have a ton of experience with Java before moving on to other languages, and so I could very well be wrong... but one thing that definitely worries me about Typescript is the increasing Java-fication of browser languages.

Now, all that being said, I gave Crystal [0] a spin the other day and it's a freaking dream. Ruby, but with types, and compiled to LLVM? YES PLEASE.

0: https://crystal-lang.org/

Re:The memory safety problem isn't bad coders by bunderbunder on Tuesday February 12, 2019 @02:53PM (#19146887)
I am beginning to suspect that Java is almost singlehandedly responsible for the prevailing opinion that static typing doesn't do anything useful and mostly gets in the way. Which is sad, because I'm also hard-pressed to think of a worse poster child for static typing than Java. It's like a stereotypical bureaucrat, asking you to fill out a mountain of paperwork before it can proceed to do a whole lot of nothing useful.

There is better out there. Sadly, for the longest time, the best examples didn't receive much interest outside of academia, on account of languages like Java being so entrenched. But it is worth spending some time with a member of the ML family to get a taste of what static typing could have been like.

Re:The memory safety problem isn't bad coders by Karrot_Kream on Tuesday February 12, 2019 @04:30PM (#19147723)
Really? There's a ton of boilerplate in Haskell that goes into working around the type system (to the point where people have made alternate Preludes to avoid having to explicitly specify the goop), and I can't count the number of times I've had to massage types in Scala for no other reason than to push a class into a certain cats monad. Lets not get started on error types in Rust. I'm a fan of static typing, but let's stop this incessant drumbeat of propaganda saying that typing is free, and the pain comes from no-true-scotsman implementations.
Re:The memory safety problem isn't bad coders by apta on Wednesday February 13, 2019 @12:17AM (#19150630)
> of a worse poster child for static typing than Java.

C, golang

Re:The memory safety problem isn't bad coders by jplayer01 on Tuesday February 12, 2019 @03:18PM (#19147121)
Java is really the worst example for a typing system. It's basically the way you shouldn't do it. There are much better type safe languages like Haskell, Erlang, Rust, etc., even just Kotlin because it drops so much of the boilerplate and excessive unnecessary verbosity, while retaining the static typing and its benefits.
Re:The memory safety problem isn't bad coders by bunderbunder on Tuesday February 12, 2019 @04:08PM (#19147509)
Kotlin is an improvement insofar as it does a decent job of resolving insanity like the "object-oriented-but-you-have-to-cross-your-fingers-behind-your-back-when-you-say-it" type system. But it's still not a great example for static typing, insofar as you're still working with Java's half-baked type semantics where the set of types the language understands is a superset of the set of types that are expressible on the underlying platform. Meaning that the set of types that can exist is quite a bit (I'm guessing uncountably infinitely) larger than the set of types that you can non-contortively deal with when doing generic programming.

Scala gets around this by attempting to extend the type system in deeply incompatible ways. Which mostly works, as long as you don't care about compatibility.

Clojure gets around this through a combination of dynamic typing and Rich Hickey including a section titled "I Don't Need Any Sour Grapes" in every lecture he delivers in public.

Of the three, TBH, I think Clojure has the best approach. You can't build proper static typing on top of the JVM without breaking compatibility with the rest of the Java ecosystem. At which point, why bother with the JVM at all?

Re:The memory safety problem isn't bad coders by jplayer01 on Tuesday February 12, 2019 @04:18PM (#19147600)
You're right, yes. Thanks for elaborating.
Re:The memory safety problem isn't bad coders by apta on Wednesday February 13, 2019 @12:18AM (#19150638)
> the worst example for a typing system

Not nearly as C or golang.

Re:The memory safety problem isn't bad coders by jplayer01 on Wednesday February 13, 2019 @02:20AM (#19151095)
Well, we were talking about static/strong typing. Java is a bad example for static typing because of all its issues, which shouldn't be taken as proof that static typing is bad, especially when there are plenty of great languages that do use it well.
Re:The memory safety problem isn't bad coders by apta on Friday February 15, 2019 @11:06AM (#19171970)
> Java is a bad example for static typing because of all its issues,

So are C and golang, even more so arguably.

Re:The memory safety problem isn't bad coders by gowld on Tuesday February 12, 2019 @02:48PM (#19146842)
It seems strange to say that something (types) isn't worth anything more than nothing just because it isn't everything (complete documentation)
Re:The memory safety problem isn't bad coders by acdha on Tuesday February 12, 2019 @02:55PM (#19146911)
You should consider overhead and completeness. If people spend lots of time writing boilerplate and coding around the type system but aren't saving a corresponding amount of time, there's a fair cost to that, especially if this means end up writing different classes of bugs but not really fewer of them. (This is basically an enterprise Java bingo card)

There's a certain argument that this is the sign of an inadequate type system: you're getting the overhead but it's not rich enough to be self-documenting or provide really rich analysis tools.

Re:The memory safety problem isn't bad coders by yxhuvud on Tuesday February 12, 2019 @03:42PM (#19147297)
Types gain you some things, but there is an overhead, both of expressibility and of terseness. The costs (and gains for that matter) are different for different languages, and also different languages are used for different things, which make different trade-offs worthwhile.

This means that the trade-off Crystal give could be way better than the one Ruby gives, despite Ruby giving a better trade-off compared to Java (for some use cases). For other problems Rust, Go or C could give a better trade-off.

It is not a discussion about if types, all other things equal, are good or not. The reason behind this is simple: Not all other things are equal and not all type systems are equal.

Re:The memory safety problem isn't bad coders by coldtea on Tuesday February 12, 2019 @03:45PM (#19147315)
>Although I mostly agree with you, it's worth noting that static typing only goes so far. When I worked in a large-scale Java codebase, it always seemed like half the code only existed in order to "work around" the type system.

I don't think that's fair.

I'd say that "half the code only existed in order to" use patterns and OO hierarchies for the sake of it.

Java can be programmed as lightly as Python or as a full-on J2EE monstrosity. The difference is cultural ("idiomatic") not brought about by types.

Re:The memory safety problem isn't bad coders by Sohcahtoa82 on Tuesday February 12, 2019 @08:20PM (#19149501)
> Java can be programmed as lightly as Python or as a full-on J2EE monstrosity. The difference is cultural ("idiomatic") not brought about by types.

I wish I had the money to burn to buy a billboard in Silicon Valley to broadcast this.

I find that the extreme majority of the hate Java gets isn't really about Java itself, but the extreme adherence to the worst paradigms and idioms I've ever seen in the programming world.

Stop writing so many classes. I'm sure everyone here has seen FizzBuzz Enterprise Edition [0]. Sure, it's a parody, but a lot of truth is said in jest. There's too much Java code that looks like FizzBuzz EE, and there's no damn reason for it.

> I'd say that "half the code only existed in order to" use patterns and OO hierarchies for the sake of it.

I really wish I could understand why so many Java programmers feel like you can't just write a class. Nope, you have to make an Interface, with an abstract class that implements it, followed by your Implementation class that extends the abstract. But directly calling that constructor is verboten! You need to create a Factory class (With associated abstract class and interface, of course!) that returns instances!

[0] https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpris...

Re:The memory safety problem isn't bad coders by goalieca on Tuesday February 12, 2019 @08:43PM (#19149607)
Java itself makes things a pain. Compare establishing a tls connection with a client certificate in python and in java. Guess which language has a secure one line way to do it.
Re:The memory safety problem isn't bad coders by coldtea on Wednesday February 13, 2019 @03:44AM (#19151494)
>Compare establishing a tls connection with a client certificate in python and in java. Guess which language has a secure one line way to do it.

Nothing in Java prevents a Java network lib from doing it in one line. In fact many third party network libs do just that.

(And Python had more or less a similar unwieldy network lib, which is why they now say "Requests is suggested" on their own standard lib documentation: https://docs.python.org/2.7/library/urllib.html ).

So again it's not the language (syntax + semantics), it's what's considered "idiomatic" and what's prevalent.

Re:The memory safety problem isn't bad coders by pjmlp on Wednesday February 13, 2019 @08:13AM (#19152747)
Still it is possible to write J2EE on any language (wrote J2EE on purpose).

I have it seen done in Clipper, C, C++ and I bet even Go will one day have its Go2EE, it just needs a bit more enterprise love.

Re:The memory safety problem isn't bad coders by Sohcahtoa82 on Wednesday February 13, 2019 @02:46PM (#19156206)
> Still it is possible to write J2EE on any language

Right. The difference is that for some reason, writing Java in the J2EE style seems to be the default, and rather than stopping the practice, people choose to hate the language instead, as if they think the J2EE style is required by the language.

Re:The memory safety problem isn't bad coders by pjmlp on Wednesday February 13, 2019 @03:51PM (#19156874)
Yep, I love how Java bashers get to deploy micro-services at scale, using Docker containers, managed via Kubernetes, with DSLs written via Terraform APIs.
Re:The memory safety problem isn't bad coders by nickpsecurity on Tuesday February 12, 2019 @02:39PM (#19146755)
Formal specifications can do all that if you build thetoing for given language and platform. Static typing is a subset of formal specifications. I suggest using the other phrase so they find more interesting stuff. Meyer's Design-by-Contract is an easy one you can do in basically any language.
Re:The memory safety problem isn't bad coders by mort96 on Tuesday February 12, 2019 @02:50PM (#19146859)
Having used a bunch of libraries which seem to think that their source code with types (or auto-generated docs based on the types) is enough to be considered documentation... source code with types or auto-generated docs based on types isn't enough to be considered documentation.
Re:The memory safety problem isn't bad coders by risubramanian on Tuesday February 12, 2019 @01:20PM (#19146030)
The problem with that reasoning is that nearly every language with static typing also has some kind of type inference. You'll just wind up with a bunch of autos/vars.
Re:The memory safety problem isn't bad coders by wvenable on Tuesday February 12, 2019 @01:26PM (#19146073)
Type inference is perfectly ok; it's still statically type checked and all the parameters and return-types are properly typed.

Type inference takes away the main argument against static typing: That static typing requires you type in a bunch of useless obvious types.

Re:The memory safety problem isn't bad coders by Verdex on Tuesday February 12, 2019 @01:44PM (#19146221)
The problem with eating pizza is nearly every restaurant with pizza also has some kind of ice cream. You'll just wind up with a bunch of people eating ice cream and pizza all over the place.

Like, type inference is pretty widely regarded as a definite good. Additionally, technologies like row polymorphism make it possible to have statically typed and inferred duck typing. To me it's hard to hear this as anything other than mission accomplished. We made a static language that looks and feels like a dynamic one.

If you don't like type inference, you'll need to offer additional explanation as to why it is bad. Because otherwise you're statement only sounds like the one I made above. It sounds weird because one doesn't necessitate the other AND because they're both widely considered good.

Re:The memory safety problem isn't bad coders by chongli on Tuesday February 12, 2019 @01:41PM (#19146197)
Haskell has very powerful type inference but it also lets you ask the compiler "what type is this expression?" There even exists tooling that lets you automatically insert inferred type annotations. Sometimes the type it infers is more general than what you wanted, so you get the opportunity to fix it in a way that makes sense to you.
Re:The memory safety problem isn't bad coders by staticassertion on Tuesday February 12, 2019 @03:54PM (#19147383)
Similarly, the intellij plugin for Rust will reveal implicit type annotations.
Re:The memory safety problem isn't bad coders by noir_lord on Tuesday February 12, 2019 @01:24PM (#19146053)
Which isn't bad if the type inference is good.

Nearly all the type errors I see are things like

    var foo = "Hello World";

    bar = foo + 10;
That should scream it's head off.
Re:The memory safety problem isn't bad coders by hombre_fatal on Tuesday February 12, 2019 @02:12PM (#19146492)
To be fair, that's valid in Typescript because it simply uses Javascript's coercion rules.

You could imagine it having built-in rules for JS' coercions:

    string + number -> string
    number + string -> string
Maybe your snippet is provocative to some people, but in real code it would quickly fail once you actually use `bar` and were wrong about your assumptions.

At which point it's similar to languages with `Int + Float -> Float` and `Short + Long -> Long` in that perhaps you wish those operators weren't defined but it's at least consistent. And you'd find the error once you've passed those results to a function that expected Int or Short.

Re:The memory safety problem isn't bad coders by pas on Tuesday February 12, 2019 @02:31PM (#19146679)
It's not the fall, but the stop that kills you. So in case of TS we don't know what was the intent.

Maybe it was this.

function suffixWith10(s: string) { return s + 10; }

But if the intent can be discovered by analysis of usages of the variable later in the code, then TS will scream loudly.

Re:The memory safety problem isn't bad coders by chc on Tuesday February 12, 2019 @02:18PM (#19146545)
This is more of a knock against the compromises TypeScript's wacky type system has to make for JavaScript compatibility than anything else. It's not possible in a language that was really designed for strong static typing like Haskell or Rust.
Re:The memory safety problem isn't bad coders by msla on Tuesday February 12, 2019 @02:52PM (#19146877)
The kind of errors I'm interested in preventing are more like this:

    var foo = 5;

    var bar = 100;

    baz = foo + bar;
Why is that bad? Well... what do 5 and 100 mean in that program? Did you just add age in years to pixels from edge? How do you know?

Appending an integer to a string is comparatively sensible next to adding age in years to pixels from edge.

Re:The memory safety problem isn't bad coders by jakevn on Tuesday February 12, 2019 @03:32PM (#19147217)
Then you should not use a general numeric when you really should be using Pixel and Age types.
Re:The memory safety problem isn't bad coders by msla on Tuesday February 12, 2019 @05:37PM (#19148368)
My broader point is that low-level representation doesn't matter nearly as much as semantics, and yet by default type systems are obsessed with low-level representations.

Appending a number to a string is a perfectly sensible thing to do when you're displaying information to a user, yet adding two numbers or concatenating two strings could be completely senseless and proof that the development process has gone off the rails.

My dream type system is strict about semantics and only handles representation in specific circumstances. The simplest way to talk about this is in terms of units of measure: Height is a type. Whether it's in inches or centimeters is a representation. Whether that representation is a number or a string is another representation. Keeping track of whether you're showing height-in-inches or height-in-centimeters to a user is useful, but burdening your mind with whether this height-in-inches is a string or a number right now when the runtime can just as easily convert between the two is senseless, and fretting over height-in-inches versus height-in-centimeters adding person-height to shoe-sole-height to get height-in-shoes is similarly senseless.

Re:The memory safety problem isn't bad coders by jakevn on Wednesday February 13, 2019 @08:54PM (#19159012)
Your dream type system is out there, yet not manifest in any popular or somewhat-popular language.

I've yet to see a strictly/strongly typed language that makes this easy to do. Through some combination of plugins, esoteric languages, macros, or boilerplate, sure.

We're decades behind what is possible due to the practical.

Re:The memory safety problem isn't bad coders by coldtea on Tuesday February 12, 2019 @03:47PM (#19147331)
Some languages don't care either way, and will let you add those types.
Re:The memory safety problem isn't bad coders by Sohcahtoa82 on Tuesday February 12, 2019 @08:22PM (#19149508)
> Why is that bad? Well... what do 5 and 100 mean in that program? Did you just add age in years to pixels from edge? How do you know?

You know because you should be using sensible variable names that make it obvious.

Re:The memory safety problem isn't bad coders by scarface74 on Tuesday February 12, 2019 @01:30PM (#19146104)
Type inference for static languages are just syntactic sugar.

The IDE and the compiler will both scream if you try something like

var x = “John Smith”;

x = 5;

Re:The memory safety problem isn't bad coders by PurpleBoxDragon on Tuesday February 12, 2019 @02:42PM (#19146788)
In C# var is always a place holder for the actual type which the compiler only accepts if it can figure out the type. If you do anything to the var that you can't do to the actual type it will still give you a compile time error.
Re:The memory safety problem isn't bad coders by chc on Tuesday February 12, 2019 @02:14PM (#19146510)
Languages with type inference still require you to give enough information for a type to actually be inferred. Type inference isn't an escape valve for the things named in the GP comment, it's just a shorthand.
Re:The memory safety problem isn't bad coders by coldtea on Tuesday February 12, 2019 @03:46PM (#19147321)
That's still good. In that case the language even writes the documentation for you!
Re:The memory safety problem isn't bad coders by jschwartzi on Tuesday February 12, 2019 @01:27PM (#19146078)
I don't really use auto unless I'm interfacing with some templatized nightmare body of code where the typename is very difficult for my tiny human brain to interpret. Using "auto" is usually a code smell, because it means your type system is too complex for you to reason about.
Re:The memory safety problem isn't bad coders by scarface74 on Tuesday February 12, 2019 @01:40PM (#19146184)
This is a valid, strongly typed C# object:

var foo = {bar = 1, baz = “Hello”, boo = “World”};

You get auto complete help and compile time type checking.

foo.bar = “Goodbye”;

Won’t compile. What would it buy you to not use ‘var’ and create a one time use class/struct?

Re:The memory safety problem isn't bad coders by maxxxxx on Tuesday February 12, 2019 @01:50PM (#19146276)
I use auto almost always. Makes the language "feel" more dynamic but you still have type safety.
Re:The memory safety problem isn't bad coders by gmueckl on Tuesday February 12, 2019 @03:06PM (#19147003)
It would make me ask why you need a one time use struct at all and probably remove it.
Re:The memory safety problem isn't bad coders by scarface74 on Tuesday February 12, 2019 @03:47PM (#19147327)
More complicated example:

var seniorMales = from c in customer where c.age > 65 && c.Sex == “male” select new {c.FirstName, c.Lastname, c.Age}

foreach(var customer in seniorMales) { Console.WriteLine(customer.Firstname + “ “ + customer.Lastname); }

Why would I create a class/struct for that use case?

Side note: this is why I find ORMs in most languages besides C# useless. Here, “customers” can represent an in memory List, a Sql table or a MongoDB collection and the LINQ expression will be translated appropriately to the underlying query syntax.

The “ORM” is integrated into the language and yes anyone can write a LINQ provider.

Re:The memory safety problem isn't bad coders by gmueckl on Tuesday February 12, 2019 @06:00PM (#19148574)
This us one of the cases where I would want you to write an explicit type for seniorMales. Reasoning about the LINQ expression is just complex enough that by not constraining its type to your expectations you can easily obscure a mistake in your thinking that would otherwise show up in the data types.
Re:The memory safety problem isn't bad coders by scarface74 on Tuesday February 12, 2019 @06:16PM (#19148703)
How so? You couldn't pass the anonymous type to another method or return it so the locality would be strong and the anonymous type is just a strongly typed POCO. Creating a class doesn't add anything semantically.

How do you feel about deconstructuring that is available in most languages?

Re:The memory safety problem isn't bad coders by maxxxxx on Tuesday February 12, 2019 @07:22PM (#19149150)
I think the anonymous type is much better. It's type safe, you can easily see how it's defined but you don't pollute the rest of your code with one-off classes that make only sense within the function.

If I had to review the code and saw an explicit type I would recommend an anonymous type.

Re:The memory safety problem isn't bad coders by gmueckl on Tuesday February 12, 2019 @07:57PM (#19149379)
It is not better. I don't know what type FirstName or Age is. Can Age be negative? Is FirstName an array of char or a string? Can FirstName be more than 10 characters long? It could be a double value for all I can tell from that line of code!
Re:The memory safety problem isn't bad coders by scarface74 on Tuesday February 12, 2019 @08:09PM (#19149444)
The select statement is projecting the Customer type from either an external data source or an in memory source. That information wouldn’t be encoded into the POCO whether or not it was anonymous.

Changing select new {...} to Select new SeniorPeople {...} wouldn’t give you any more information. At most if I was writing that as part of a repository class I would be sending you back an IEnumerable<SeniorPeople>.

If you were to send me an expression to use in the where clause you would send me an

<Expression<Func<SeniorPeople,bool>>

You have no idea what that expression is going to be translated to until runtime.

Either way, you are just working with non concrete Expression Trees that could be transformed into IL, SQL, a MongoQuery etc. All of the constrainsts would be handled by your data store.

You don’t even know before hand whether you are iterating over an in memory list, or streaming from an external data source.

I could switch out Sql Server for Mongo and you as the client wouldn’t be any the wiser.

And we haven’t even gotten to the complication if the result set was the result of a LINQ grouping operation.

Re:The memory safety problem isn't bad coders by gmueckl on Tuesday February 12, 2019 @08:30PM (#19149554)
You are comstructing an elaborate strawman that does nothing to work in your favor. If anything, yur essay just makes it clear that it is impossible to glean the data type from the LINQ expression itself.

Yet, any code using its result will contain implicit assumptions about that data. Therefore, in order to maintain type safety, the expected type of the return value must he stated.

That type must also be independent from whatever LINQ does internally to produce that return value. Otherwise, the LINQ providers wouldn't be as exchangeable as you claim.

Re:The memory safety problem isn't bad coders by scarface74 on Tuesday February 12, 2019 @08:54PM (#19149659)
You also included information about “constraints” like is it a positive or negative number and string length. That wouldn’t be encoded in the type. Why would I go look up the types either way? I’m using a strongly typed language, the IDE would tell me that anyway. While I am writing code, I see a red dot showing me immediately if I’m using the type incorrectly. Any assumptions that were incorrect, I would get immediate feedback.
Re:The memory safety problem isn't bad coders by gmueckl on Tuesday February 12, 2019 @10:34PM (#19150148)
Not necessarily. Is Age unsigned or not? This constrains the possible range of values. In other contexts, there is a lot more information inferred by the integer type used.
Re:The memory safety problem isn't bad coders by maxxxxx on Tuesday February 12, 2019 @09:46PM (#19149914)
Do you actually work with C#, LINQ and generics?
Re:The memory safety problem isn't bad coders by gmueckl on Tuesday February 12, 2019 @10:14PM (#19150047)
Yes. I am quite familiar with them.
Re:The memory safety problem isn't bad coders by maxxxxx on Tuesday February 12, 2019 @10:30PM (#19150115)
Then tell me how you can model in C# a type where a property can’t be negative or a certain length.
Re:The memory safety problem isn't bad coders by gmueckl on Tuesday February 12, 2019 @11:55PM (#19150530)

    class Foo {
      public uint Bar { get; set; }

      private char[] _baz;

      public char[] Baz { get { return _baz;} }

      public Foo() { _baz = new char[5]; }
    }
This is cheating a bit on the array part. But the length of that char array is set in stone and cannot be changed from the outside. However, the Bar property uses a standard C# datatype.
Re:The memory safety problem isn't bad coders by maxxxxx on Wednesday February 13, 2019 @06:27AM (#19152249)
I honestly thing that you haven't done any real work with LINQ or you are imagining problems that don't exist. The char array is not usable as string or we have to go back to 0 terminated strings like C.
Re:The memory safety problem isn't bad coders by gmueckl on Wednesday February 13, 2019 @02:27PM (#19155968)
I am not imagining problems that don't exist in the application domains I work on.

If your environment is so relaxed that aspects like signedness of an integer don't matter, so be it. But then you are also far removed from a domain where software correctness is non-negotiable.

Re:The memory safety problem isn't bad coders by scarface74 on Thursday February 14, 2019 @09:40AM (#19162151)
It’s not aspects “like signednes” that’s the only constraint that you could embed in your defined POCO and still use as a select projection that would survive a LINQ expression that was turned into an expression tree. Your data store would enforce the other constraints. Your C style character array as a string wouldn’t work with either LINQ nor any of the standard library.

Are you really trying to go back to the bad old days with Microsoft C/MFC/WIN32/COM where you had over a dozen ways to represent a string that you had to convert to and from depending on which API you were using?

Re:The memory safety problem isn't bad coders by scarface74 on Wednesday February 13, 2019 @12:51AM (#19150773)
And your class wouldn’t work as part of the LINQ example. EF for example wouldn’t know how to translate it.
Re:The memory safety problem isn't bad coders by maxxxxx on Tuesday February 12, 2019 @08:21PM (#19149503)
How do you want to model this in C#? Write a full class with all kinds of checking for a single LINQ operation? That will be a very bloated codebase.
Re:The memory safety problem isn't bad coders by maxxxxx on Tuesday February 12, 2019 @01:48PM (#19146267)
Not necessarily. The types may just have very long names. In C++ for iterating through STL container auto is a godsend. The types are very straightforward and easy to reason about but just long.

for (auto it = s.begin(); it != s.end(); it++) {

is much easier to write than

for (vector<int>::iterator it = s.begin(); it!=s.end(); it++) {

Re:The memory safety problem isn't bad coders by jschwartzi on Tuesday February 12, 2019 @02:03PM (#19146411)
I try to avoid using STL because it A) throws exceptions, B) has really convoluted type names, and C) dynamically allocates memory. These are all things you avoid in the embedded space.
Re:The memory safety problem isn't bad coders by maxxxxx on Tuesday February 12, 2019 @02:22PM (#19146586)
Makes sense for embedded.
Re:The memory safety problem isn't bad coders by gowld on Tuesday February 12, 2019 @02:51PM (#19146870)
Any use of templates has the same problem, including templates that are used to generate highly efficient static constants and non-branching-at-runtime code.
Re:The memory safety problem isn't bad coders by llukas on Tuesday February 12, 2019 @03:15PM (#19147083)
Example?

Templates (template metaprogramming) help to avoid exceptions. You can check invariants first and jump into efficient code.

Re:The memory safety problem isn't bad coders by llukas on Tuesday February 12, 2019 @03:16PM (#19147094)
or almost like python:

for (auto &element: s){

even better with structured bindings:

for (auto [key, value]: map){

Re:The memory safety problem isn't bad coders by jcelerier on Tuesday February 12, 2019 @03:33PM (#19147225)
why we can't have nice things, in one comment.

auto is not a code smell on the contrary, it is used when your type system is easy enough to reason about that you don't need to write the actual type.

Re:The memory safety problem isn't bad coders by jschwartzi on Tuesday February 12, 2019 @05:00PM (#19148023)
If it's so easy to reason about, why can't you just state the type of the variable? Doing so helps me understand what's popping out from the rvalue in assignment so that I can follow what your code is doing.

If you're worried about spending time changing type names during a refactor, look at it instead as an opportunity to evaluate the correctness of the code in the context of your replacement type. Use of the auto keyword avoids doing that and as a result enables you to create new and exciting bugs in your code.

Re:The memory safety problem isn't bad coders by jcelerier on Wednesday February 13, 2019 @09:30AM (#19153313)
> If it's so easy to reason about, why can't you just state the type of the variable?

because : - easy to reason about and easy to type are two differents things - naming everything increases mental load

> Use of the auto keyword avoids doing that and as a result enables you to create new and exciting bugs in your code.

to the contrary, porting a lot of my code to use almost-always-auto did actually remove bugs in the form of silent type conversions happening - e.g. std::pair<std::string, int> instead of std::pair<const std::string, int> (yay memory allocations), bad fp conversions...