Live data from Hacker News

Want cleaner code? Use the rule of six

davidamos.dev

231–240 of 352 posts

Re: Want cleaner code? Use the rule of six

#231
Now I know where Rust got some of its syntax from...

As an aside, when I see samples like this, it makes me itchy. I hope and assume that they're being used as made-up snippets just to illustrate a point, and aren't being lifted from an actual codebase.

Because... ugh... isn't it obvious? Attacker-controlled input such as URLs should never be manipulated with naive string processing! Always use a proper parsing library. Not to mention that complexities of URL encoding, character escapes, etc...

The problem is that the author is using abstractions at the wrong level, with or without his fixes. The correct solution would be something like:

    var uri = new Uri( "http://foo/demo?test=a&blah=b%20c" );
    var map = System.Web.HttpUtility.ParseQueryString( uri.Query );
    
    Console.Out.WriteLine( "is blah equal to 'b c'?\n{0}", map["blah"] == "b c" );
The above example is C#, but similar code can be written in any language. It's simple, direct, and doesn't violate the "rule of six". It can be read like English:

1. Construct a URI from a given string.

2. Parse the query part of the URI into a map.

3. Test if the 'blah' value in the query is "b c" as expected, with the escaped space decoded properly.

The example of how to apply the "MORF" rule in the article still has low-level operations involved, which doesn't make the code more readable. It doesn't describe the intent, which is the key thing to writing code that doesn't need comments every second line.

Re: Want cleaner code? Use the rule of six

#232
post #224

Earlier quoted context omitted.

> That’s nice, but it just highlights that the coding style isn’t sufficiently intelligible by itself. We can’t and shouldn’t rely on a static analyzer to be active to make code intelligible everywhere code is displayed (pull requests, diffs, ...). Yes, we absolutely should rely on that. Why would programmers, who are basically tool-builders, reject tools that help us write and analyze our programs? This is not the 1…

I’m all for IDEs for writing software, but not as a prerequisite for reading source code, because the latter happens in all kinds of different contexts. E.g. a software like GitLab shouldn’t require an integrated static analyzer for any or all programming languages. That would only increase the barrier to entry for all software that happens to display source code. If source code can’t be read and understood as-is any…

Surely you must understand that the functionality provided by the tools will impact how the code is written, tested, reviewed, and deployed.

There is no ideal world where we can use tools to write code and it would be perfectly readable for people who don't use those tools.

Embrace the modern ecosystem, or build something better, or take a time machine back to the 1970s where there were no IDEs, source control, or CI/CD. Do you really think that would be better?

Re: Want cleaner code? Use the rule of six

#233

Now I know where Rust got some of its syntax from... As an aside, when I see samples like this, it makes me itchy . I hope and assume that they're being used as made-up snippets just to illustrate a point, and aren't being lifted from an actual codebase. Because... ugh ... isn't it obvious? Attacker-controlled input such as URLs should never be manipulated with naive string processing! Always use a proper parsing lib…

… and Ruby got it from Smalltalk. :)

Re: Want cleaner code? Use the rule of six

#234
post #213

Earlier quoted context omitted.

> Naming things is hard Naming is hard unless you have a sensible concept hierarchy, and then naming is easy. Concept hierarchies are hard, unless you have a sensible system model, and then concept hierarchies are easy. System models are hard, unless you have domain expertise, and then system models are easy.

Not everything is a neat concept though. Look at the Porter stemming algorithm for example. It simply does not decompose into something that is easy to put names on.

> A consonant will be denoted by c, a vowel by v. A list ccc... of length greater than 0 will be denoted by C, and a list vvv... of length greater than 0 will be denoted by V. Any word, or part of a word, therefore has one of the four forms: CVCV ... C CVCV ... V VCVC ... C VCVC ... V

Looks fine to me.

Re: Want cleaner code? Use the rule of six

#235

What the author is missing is that easy to read/reason/understand about is within the context of making a change to the code to fix a bug, add a feature or make some non-functional improvement to it. This is what most of the "easy to read" articles forget. Show me why it is easier to fix a bug, add a feature or make a non-functional improvement to the code with their style than without. For example, if you've extract…

If you are unit testing you should not have to worry about tweaking a function and it breaking everywhere else.

> If you are unit testing you should not have to worry about tweaking a function and it breaking everywhere else.

"should" is a word loaded with authority.

Why?

If you believe a unit tests is for turning an impure function into a pure function (so you can just test what it's doing and no other effects), then in many cases tweaking will break existing unit tests. If the function exists, it's assumed it's used by more than the tests for it. Changing the signature or even the internal dependencies necessarily breaks the known contracts with other units.

Re: Want cleaner code? Use the rule of six

#236

Earlier quoted context omitted.

'However, to even begin to understand it you have to read an article from the original writer' - or just know APL? If you know APL, it's clear.

While I'm not going to go collect a bunch of APL programmers to confirm this (where would one even find them?), I highly doubt that claim. Knowing a language doesn't mean dense code is suddenly obvious. This is a silly example, but years ago I wanted to prove that you could write a non-trivial program in python using a single expression (because python's lambda only allows you to use expressions, not statements). And…

You don't need a bunch - you need 1. I am an APL programmer, and I can tell you 100% for certain that anyone who knows any APL would find that easy to read. (Want to find more though? Go to one of these: https://aplwiki.com/wiki/Chat_rooms_and_forums)

Knowing a language absolutely does mean 'dense' code is obvious (fwiw that APL code is not at all dense, you can do go a lot shorter). It's comparable to chinese characters or something like that. It only looks dense/unreadable to someone who doesn't know it.

Re: Want cleaner code? Use the rule of six

#237
post #224

Earlier quoted context omitted.

I’m all for IDEs for writing software, but not as a prerequisite for reading source code, because the latter happens in all kinds of different contexts. E.g. a software like GitLab shouldn’t require an integrated static analyzer for any or all programming languages. That would only increase the barrier to entry for all software that happens to display source code. If source code can’t be read and understood as-is any…

Surely you must understand that the functionality provided by the tools will impact how the code is written, tested, reviewed, and deployed. There is no ideal world where we can use tools to write code and it would be perfectly readable for people who don't use those tools. Embrace the modern ecosystem, or build something better, or take a time machine back to the 1970s where there were no IDEs, source control, or CI…

I disagree, because it reduces the freedom of choice and variety of tools available for software development, and increases the dependency on bespoke tooling. Using a more sophisticated word processor shouldn’t reduce the readability of the text produced. If anything, IDE features should be helping to increase the readability of the resulting source code, instead of promoting a coding style that decreases it.

This is about decoupling. The same way one should be able to freely pick a VCS and CI/CD system of choice, one should also be free to pick the IDE, or any other system concerned with source code, of choice. Those should remain loosely coupled, and should each be easy to replace. The more requirements you impose on how source code has to be marked up for intelligible display, the more you restrict the choice of tools it can usefully work with, and the more laborious it becomes to create new adequate tools in that field.

Re: Want cleaner code? Use the rule of six

#238

My opinion is that maintainable code is written first for reading by humans and second for executing by computers. Unless I'm writing throwaway prototype code (famous last words, lol), I try to write code such that I will be able to figure out what my intention was 6-18 months from now when I'm staring at a piece of code in a panic trying to debug a production issue. That doesn't mean I'm going to get it right when I…

> My opinion is that...

You make it sound like you came up with that all by yourself

Re: Want cleaner code? Use the rule of six

#239
post #108

Reminded me of 'Object Calisthenics' by Jeff Bay. Basically an exercise for a toy project where you adhere to 9 rules: 1. Only One Level Of Indentation PerMethod 2. Don’t Use The ELSE Keyword 3. Wrap All Primitives And Strings 4. First Class Collections 5. One Dot Per Line 6. Don’t Abbreviate 7. Keep All Entities Small 8. No Classes With More Than Two InstanceVariables 9. No Getters/Setters/Properties https://william…

>Wrap All Primitives And Strings Gah. I've seen the other side of this, a few people far too trigger happy to make FivePlusVeryLongNounVO/DTO for every little thing, and it gave me some new appreciation towards tuples and primitives. Sometimes you really don't want to go into another new file for an object type which is used in only one specific place. Especially with >Don’t Abbreviate Meaning the variable name will…

> Gah. I've seen the other side of this, a few people far too trigger happy to make FivePlusVeryLongNounVO/DTO for every little thing, and it gave me some new appreciation towards tuples and primitives. Sometimes you really don't want to go into another new file for an object type which is used in only one specific place.

The rules are an exercise for a toy project. Like all similar 'rules' they are just hints to make you think. When done with the exercise, and you see a string with a social security number in a production code, you may consider creating a dedicated SocialSecurityNumber class. The class will guarantee a well formed social security number according to official rules. The class may even offer Area, Group and Serial parts of the social as separate fields. The class may decide to use a string or integers internally, but that would never be exposed to the class consumers. All the code that uses SocialSecurityNumber will not have to guess whether string is valid, if it has dashes etc. The same reason you use built-in types like an Integer (as oppose to a tuple of 4 bytes or 32 bits).

Re: Want cleaner code? Use the rule of six

#240

Earlier quoted context omitted.

> the code can't be read top-to-bottom The idea of the technique is to split out code at a different level of abstraction with a clear name communicating what it does, while hiding the details of the how, because you don't need to care about that detail at all to fully grok the code in the calling function. Where this breaks down is when the code you're trying to split out is not at a different level of abstraction,…

Each function becomes something new that needs to stick in your brain. Someone that applies "MORF" to their code winds up nearly inventing their own language in the file that they're writing. All that takes up more memory when you're reading their code, because due to leaky abstractions the actual implementation of whatever the function name that you replace it with is often important. I have an actual track record o…

I agree, one of my favorite ide functions is the inline button. Because all too often that supposedly clear name turns out to be not clear at all.

Perhaps it would not be quite as bad if there was a clear distinction between "this is a function for reuse" and "this is a function that's naturally just elaborate code folding", but that does not really exist. Just stick to single-assignment variables and keep their scopes short and enjoy the good parts of sequential code.

Post reply on HN