As developers, we've all wasted hours, staring at a piece of code, trying to decipher its purpose, and wondering what the original author was think...
For further actions, you may consider blocking this person and/or reporting abuse
Other that JSDoc/TSDoc I am in full agreement. Excellent article.
But I would also mention that consistency in naming is important. Establish a style guide and define your terms so everyone is on the same page.
For example, consider these terms. What do they mean and how do you choose between them:
get
,find
,make
,create
,choose
,select
,send
,submit
,save
, etc.Perhaps
get
means you are retrieving a single value from an API andlist
means you are retrieving a set of values. Then every function that retrieves a single value should be namedget<noun>
and the corresponding multiple values function,list<noun>s
, e.g.,Great point, thanks! I've added a new section that covers this:
Establish Consistency and Set Expectations in a Team
.Incredible advice. Im currently dealing with a codebase where there are several functions, addItem, saveItem, createItem all in the same app service and they all actually do the same thing with slight variations.
Yep. The secret to programming is a) to break the problem down into small, easily-solvable mini-problems, then solve those in the simplest, clearest, and most consistent way, then compose the small solutions back into the overall solution. Clear style guides followed faithfully and invariably are essential, but how many companies bother? Save a penny, lose a pound.
All of this boils down to one simple thing: reduce cognitive load. Reduce cognitive load. Reduce cognitive load.
To this end, it pays also to apply design principles to your code. For example: proximity, alignment, contrast, repetition (consistency), whitespace, etc. I discuss this and more on Craft Code as well as here on Dev.to.
ss
Self documenting code is a fine standard to follow, but you should still have code comments. I've worked with multiple developers who said "my code is self documenting so why comment?" 🤣 Then when they leave, nobody knows WHY they were doing the things they did, regardless of how self documenting the code is.
Please comment your code as well.
The need for comments in code is the lack of tests. Comments rot very fast. Uncle Bob has excellent videos on the matter.
Agreed, but IMHO, only when there is a potential "why?" question that needs to be answered.
I usually use linters as a great way to enforce this. Generally speaking, I only ever set linters to error if the code doesn't follow standards.
If the person has a reason to break standards, they need to put in a linter comment to allow the code that breaks the pattern and they are required to provide a reason that they are breaking the lint pattern. For those that can't be linted, we handle in PR. If you're doing something that is not obvious and you can't make it obvious, then you must provide a comment clarifying the issue and why you're doing it that way.
Otherwise, you should only ever use doc comments.
Comments should never describe what you are doing, nor how, only why and only when it's not obvious.
Exactly. The code can only tell you what it does. Code comments are there to tell you what it's SUPPOSED to do.
Very nice work, Matt! Thanks for the post.
Cool, nice article @mattlewandowski93 !
Nice one, but I suggest for you to include using of strongly typed IDs.
Thanks, great suggestions I'll add it in
Wow.. Really interesting, thank u mate❤️
Good article, though I would disagree with using Enums in Typescript. The thing is that they are kind of funky and don't translate in and out of the underlying type well. It can better be expressed as:
`export const PaymentStatus {
Pending = 'pending',
Completed = 'completed',
Failed = 'failed',
} as const;
export type PaymentStatus = (typeof PaymentStatus)[keyof PaymentStatus];
`
I would urge caution about breaking a large function into too many smaller functions just for the sake of making it smaller. It can lead to too much abstraction that becomes less readable because you are trying to follow the code around separate, spread out methods and files etc.
It's a great explanation of the principle of "create a meaningful standard language", which is one of the principles that programmers should follow.
However, there are still missing parts. In particular, there is a need to add meaningful comment statements next to the codes and comment notations.
Comment expression notations provide the user with mobility for immediate intervention on faulty points.
Markdown Literate programming that don't break the syntax of any programming language
Live preview in Notepad++
"Java JEP 467: Markdown Documentation Comments" uses my original method on 2019-02-17
Great article! I think there are a lot of good ideas and valid points. I am not a fan of (2.) though - it seems like taken right out of the "Clean Code" book. It's a pain jumping from function to function to function, and you most likely lose context. I would like a bigger function where the entire context is visible and ready for me to read.
Recommending the use of TypeScript Enums is an interesting take I don't often see. 😅
I think Enums have their place in typescript, even if you can use types most of the time. I listed types first, as I do prefer types for most cases. Here is an example of where an enum would shine over a type though.
with a type
With the enum in this case, we can utilize the bidirectional mapping. Which allows us to use the single Enum to determine the status and code of different scenarios.
Ultimately, I think that enums are a lot more verbose, making them more self-documenting.
HttpStatus.OK
orHttpStatus.BadRequest
is very intuitive.Nice. A shine perspective.
this is good advice, thanks. It surprises how one still finds codebases with any lack of common sense... :)
Good summary. Why you shouldn't use enums in TS though...
Interesting, thanks for sharing. I’ve never considered how enums were compiled.
Awesome! Nice one bruv
Excellent points but you lost me after at point 4. Is still don't like the TS compilation step. It slows down development. For me at least.
TS is just an example. The idea extends to any strongly typed programming language.
Great suggestions. I am following more than 90% of the above suggestions.