Live data from Hacker News

Want cleaner code? Use the rule of six

davidamos.dev

291–300 of 352 posts

Re: Want cleaner code? Use the rule of six

#291

I don't necessarily agree with the step of putting the code in a separate function; that often works, but just as often makes it so that the code can't be read top-to-bottom anymore which hurts readability. In this case there's, I think, a better alternative; the equivalent-ish code in Ruby for the example code here would be something like this: values = s .partition('?')[-1] .split('&') .map { |key_value| key_value.…

I build most of my classes that way in python.

    class Motor:
        def __init__(self):
            self.max_speed = 10.0
            self.clockwise = True
            self.controller = Controller()

        def set_max_speed(self, max_speed: float=10.0) -> 'Self':
            self.max_speed = max(0.0, max_speed)
            return self

        def start(self) -> 'Self':
            self.controller.start()
            return self

        etc..

Then you can use it as follows:

    motor = Motor().set_max_speed(5.0).start()
There is nothing stopping you from building iterators that work the same way

Re: Want cleaner code? Use the rule of six

#292
post #82
post #36

Earlier quoted context omitted.

Once I read something along the lines of “every programmer goes through that phase were we wants to show how clever he is, by writing whole programs in one line. Until he understands how stupid that is”. I do not have the source, regrettably.

I was taking my first multi-threaded resource allocation course when I first ran into the famous Kernighan quote. > “Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” It clicked and instantly disabused me of the notion that smart people write code that's any smarter than the minimum requir…

Kernighan wrongly assumed that to do something twice as hard, you need someone twice as clever. Most probably you need someone just as clever and twice the time.

It's a brilliant quote, though.

Re: Want cleaner code? Use the rule of six

#293
post #283

This is why setting an arbitrarily short max line length matters. And consequently why auto-formatters suck. A short line length, while yes imperfect, forces complex lines to be decomposed into individual concepts. And it allows the code to read like a book rather than . Ultra-wide monitors be damned. And auto-formatters suck because they don’t split concepts onto individual lines. They can’t. They just mangle code a…

Agreed, auto-formatters have the single purpose of making code on screen visually more readable for humans, but seem to not consider that humans are not machines. There is no regard for "visual code density", no attempt to use vertical alignment to highlight similarities and differences between consecutive lines.

To be fair considering such visual details is a complex task and probably hell to implement.

Re: Want cleaner code? Use the rule of six

#294
post #239

Earlier quoted context omitted.

>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 mak…

Your example breaks down the moment you put it back in context. SocialSecurityNumber works because it's going to be used across multiple subcontexts. The same reason Vector2 works over using (x, y) tuples everywhere.

I'm specifically mentioning one specific place. This now requires you to go to a different file to see what's up, for every time you need HighlySpecificModelWithOnlyStringAndInt, and most naming doesn't help provide context. All that's happening here is moving vertical navigation to navigating different files. It's a remnant of a time people wanted more specification than object[], but valuetuples didn't exist yet (C#).

And even then, SSN is a pretty well known abbreviation. So your example is also far too favorable compared to most extreme yet real examples.

Re: Want cleaner code? Use the rule of six

#295

Earlier quoted context omitted.

It would presumably be evident from the context in which the `map` call appears what its purpose is.

And yet, I know python and have 15 years of professional experience coding, and (in the limited time I wanted to spend on it), I still don't exactly know what he was trying to accomplish. So congrats if you got it, but I'm sticking with my claim that it's fairly non-obvious. I understand that it's pulling out the last three elements. I have no idea why it's pulling out the last three elements, what purpose that serve…

That's precisely what we should expect from the context of the map call, but that context is outside the scope of the example.

Re: Want cleaner code? Use the rule of six

#296
post #56

I don't necessarily agree with the step of putting the code in a separate function; that often works, but just as often makes it so that the code can't be read top-to-bottom anymore which hurts readability. In this case there's, I think, a better alternative; the equivalent-ish code in Ruby for the example code here would be something like this: values = s .partition('?')[-1] .split('&') .map { |key_value| key_value.…

One issue with functional pipelines is that the reader has to keep track of what the types and the data are on each line. It’s fine for 2-3 lines, but it can get non-obvious quite quickly. Assigning intermediate points to named variables can be appropriate, or indeed factoring portions of the pipeline out into separate functions.

I think it is compounded in the article exemple by the flow of the code being non obvious. The map applies to code in its last argument. The lambda is the last action and the url parsing will happen first but is last in the line.

I have far less issue with the piping operator in Ocaml (|>) which works exactly like a shell pipe because the code is in sequential order and that makes a huge difference.

Python like JS weren’t designed as functional languages and it shows in their syntax. Still grateful for the added functionality however.

Re: Want cleaner code? Use the rule of six

#297

Earlier quoted context omitted.

> My opinion is that... You make it sound like you came up with that all by yourself

Nah, I stand on the shoulders of generations of developers, just like everyone else here. I didn’t claim my opinion was novel, just that it’s mine. I hope others share my opinion, because I’d find codebases that fit my criteria easier to maintain than many other types. Also, do you agree or disagree with any of the ideas I put forth?

I don't disagree, I just think it's only right to credit the original authors when you use their exact phrasing.

Re: Want cleaner code? Use the rule of six

#298
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…

Draconic

Re: Want cleaner code? Use the rule of six

#299

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 can write code that's easy to understand, and it is also good for the computer to run.

One thing is not in contradiction with the other.

It could lower the reusability of the code, by not having many abstractions, but it will be easy to understand, concise, and it will do what it was written for very well.

Re: Want cleaner code? Use the rule of six

#300
Counterpoint: I'm sure most of us wrote far shorter sentences when we were learning our first (human) language, or perhaps even subsequent ones; yet now I suspect we can all read and write sentences with dozens of words.

Yet when it comes to programming languages, the majority of "advice" seems to be about absolute dumbing-down and propagating an attitude of "it's too hard, you can't possibly learn, just give up"?

I've always wondered about this dichotomy. Some languages like the APL family appear to have gone far into the "it's a language, to be learned like any other" territory, while more "mainstream" ones are drifting further in the opposite "don't even bother trying harder" direction.

Kernighan's Lever: http://www.linusakesson.net/programming/kernighans-lever/ind... (look at the rest of his site; he has clearly leveraged that attitude with great success)

(I looked at the one-line example in Python and, despite having very little experience in the language, it was actually faster to read and understand as a whole than the 3-line version.)

Post reply on HN