Ask HN: What code do you consider as clean code?
obviously this is something debatable and subjective but there's always some common basis that everyone agrees on. do you have any good examples of code you wrote or saw on Github that you consider as state of the art code? what makes it so great? if you are reading someone else's code how do you want it to be written/structured (preferably JS examples)
16 comments
[ 3.6 ms ] story [ 49.9 ms ] threadI am really curious what this is because I have been with 4 organizations now and despite all doing things very differently, they insist that they are doing things according to "industry standard."
I have even had a few cases of two senior devs reviewing my PR at the same time and going back and forth to tell me to revert what the other dev had me put in, with both coming from prestigious companies.
things that developer do not agree on include choosing between (object-oriented, procedural or functional code)
the debate of isolating small bits of code regardless of the paradigm used. how many "code components" can be created without making the follow references game so crazy that you can't even understand the code flow and where you even started reading the code. are small code duplicates really that bad or over componentizing is a bigger issue that tries to solve the first one?
things like that...
I didn't work for large company before so was wondering if you saw any consensus/ agreement patterns between senior developers in these type of debatable things especially when writing JS because of it's huge flexibility.
As a senior guy reviewing code I’ve asked for reversion of stuff i made the dev put in. On more than one occasion. Nobody’s perfect.
I admit that after a few cases of that I realized I needed to rethink code reviews and became more mindful of distinguishing between things that really needed to change, things needing explanation, and things inspiring overzealous pedantry. Made code reviews much less time consuming.
// GOOD
let fullname = 'John Smith'
// BAD
let f = 'John Smith'
If this is a variable in a 20LOC func, f is completely sufficient, easier to type, easier to read.
In the other extreme, names can also be too meaningful: ´ThingMessageRelayAdministratorMiddlewareComposer` or `storage_access_administrator_storageshelf_listview` are jacked full of meaning, and about as useful as a hammer without a handle.
Not sure how "easier to type" is relevant - aside from Notepad pretty much every editor offers some completion feature. I'm definitely a fan of longer identifiers, though abbreviated where it makes sense and using short words where possible.
In a 20 LOC function, that takes me about a second, if that long.
> aside from Notepad pretty much every editor offers some completion feature
It's still easier to type `f` than `fi` + leadkey + N or whatever the editor uses. Bonus points if the first prediction is wrong, or ambivalent.
And the more important reason is "easier to read", especially in long expressions;
is much easier to read than Finally, not only the name itself, but also it's length should convey meaning. If I see a variable like `currentElement` in my code, I immediately know that it probably lives beyond the scope it is defined in. If I see a variable named `c` I know that it is a short lived throwaway.People get in a lot of trouble when they try to write descriptive names for everything.
It was one of the most interesting things in the Lenat/Guha book on Cyc that people get fooled by labels that look like natural language. If you really have a database of unique concepts and try to attach meaningful labels you are going to have an increasingly hard time when n goes past 1,000,000. Wikipedia does about as well as can be done. There's something to say about lowering your cognitive load and using names like "that", for instance
but I was asking if there's anything to keep in mind when building large complex code.
- how long or short are your functions?
- what do you comment?
- can you actually use only pure functions?
- how large are your files?
- how do you manage 3rd party dependences or do you don't use any?
- how do you mange bundle size and enforces code quality?
- when to break a function or break a file into smaller parts?
- do you create functions that call only other functions or each function should have some procedural code?
giving code examples of a well written code snippet would be so helpful!
https://en.wikipedia.org/wiki/Code_Complete
but it seems it focuses a lot on TTD/unit testing and integration testing not my thing.
I hate testing and never understood why they created this most complex todo list in the first place?
just write what you are going to do it and don't worry no need to check everyday if the code still exist or not.
why run an automated check every single day of the same code. not to mention the time wasted on mocking and creating fake data.
sorry a bit off topic but I really hate tests! would need to understand why some still defend TTD...
Clean and consistent formatting, detailed comments, sensible modularization -- and it works well
don't you find that comments are taking a lot of space more than the actual code or do you think this is what a takes to avoid confusion. I found that single line comments are much better, compact and enough to explain what is happening as opposed to those multiline comments that take so much space.