Wed Oct 17 - Avoiding Code Smells by Walter Bright

jmh530 john.michael.hall at gmail.com
Wed Nov 7 16:04:27 UTC 2018


On Wednesday, 7 November 2018 at 13:15:03 UTC, Dennis wrote:
> [snip]
>
> "Hello, MrHaltingProblem here!
>
> We obviously need a @noloops attribute. My co-workers are 
> constantly writing buggy loops with wrong loop conditions so I 
> NEED the extra protection. You're asking for a justification? 
> Trust me, any decent programmer knows the importance of this. 
> You are just too stubborn to see all the bugs that the lack of 
> this attribute causes. Asking for real world examples?
> ```
> class Plane {
>   void doLandingProcedure() {
>     // lots of code
>     while (1) {} // OOPS!
>     // lots of code
>   }
> }
> ```
>
> There! Now let's discuss the benefit of such a feature in D."
>
> This doesn't look like a compelling argument, so D programmers 
> would probably suggest making more use of ranges instead.

I get what you're saying here, but just to play devil's advocate, 
this feature could be added broadly to D without breaking any 
code. In particular, the @disable attribute could be extended to 
1) take some input, in this case a keyword (foreach, while, goto, 
etc) but potentially also symbols (I'm thinking mainly function 
names here), and 2) @disable's behavior could be extended so that 
it works with like the protection attributes in terms of being 
able to use it as in @disable(keyword) { } or @disable(keyword):. 
For a start, something like @nogc and @nothrow are really just a 
special cases of @disable(GC) and @disable(Exception), or 
something.

Also, the idea could be improved if there was some easy way to 
combine multiple attributes (there might be, but I don't know off 
the top of my head).

Where I'm coming from is that these attributes aren't really 
based on any primitives. They are rather ad hoc. For instance, 
even this proposal would need additional work to enable @safe and 
pure to be based on primitives. What if someone wanted to make 
@disable(template) and have that enforced throughout their code? 
What if the decision not to include textual macros in D were 
instead set up as a default for @disable(textual_macros)?




More information about the Digitalmars-d-announce mailing list