{"id":1239,"date":"2022-05-01T10:16:13","date_gmt":"2022-05-01T09:16:13","guid":{"rendered":"https:\/\/www.muratyaman.co.uk\/blog\/?p=1239"},"modified":"2022-05-01T11:45:21","modified_gmt":"2022-05-01T10:45:21","slug":"prettier-or-not","status":"publish","type":"post","link":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/2022\/05\/prettier-or-not\/","title":{"rendered":"Prettier or not"},"content":{"rendered":"<p><strong>Prettier<\/strong> or not: you decide!<\/p>\n<p>Here is a piece of <strong>TypeScript<\/strong> code before and after using prettier. IMHO: if you want to <strong>ruin<\/strong> your code in a second, use prettier! I did not even use very long lines of code, and set the limit to the recommended 80 characters!<\/p>\n<p>BEFORE:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.muratyaman.co.uk\/blog\/wp-content\/uploads\/2022\/05\/prettier-before-478x480.png\" alt=\"\" class=\"aligncenter size-medium wp-image-1253\" width=\"478\" height=\"480\" srcset=\"https:\/\/www.muratyaman.co.uk\/blog\/wp-content\/uploads\/2022\/05\/prettier-before-478x480.png 478w, https:\/\/www.muratyaman.co.uk\/blog\/wp-content\/uploads\/2022\/05\/prettier-before-240x240.png 240w, https:\/\/www.muratyaman.co.uk\/blog\/wp-content\/uploads\/2022\/05\/prettier-before-768x772.png 768w, https:\/\/www.muratyaman.co.uk\/blog\/wp-content\/uploads\/2022\/05\/prettier-before.png 771w\" sizes=\"auto, (max-width: 478px) 100vw, 478px\" \/><\/p>\n<p>AFTER:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.muratyaman.co.uk\/blog\/wp-content\/uploads\/2022\/05\/prettier-after-480x478.png\" alt=\"\" class=\"aligncenter size-medium wp-image-1254\" width=\"480\" height=\"478\" srcset=\"https:\/\/www.muratyaman.co.uk\/blog\/wp-content\/uploads\/2022\/05\/prettier-after-480x478.png 480w, https:\/\/www.muratyaman.co.uk\/blog\/wp-content\/uploads\/2022\/05\/prettier-after-240x240.png 240w, https:\/\/www.muratyaman.co.uk\/blog\/wp-content\/uploads\/2022\/05\/prettier-after-768x765.png 768w, https:\/\/www.muratyaman.co.uk\/blog\/wp-content\/uploads\/2022\/05\/prettier-after.png 783w\" sizes=\"auto, (max-width: 480px) 100vw, 480px\" \/><\/p>\n<p>If your <strong>programmers<\/strong> cannot write code properly\/dev-friendly, you have more serious <strong>issues<\/strong> than <strong>linters<\/strong>, code <strong>formatters<\/strong> and their settings!<\/p>\n<p>If you want to write code in a <strong>dictatorship<\/strong>, continue using prettier!<\/p>\n<p>Writing code is <strong>art<\/strong>, like <strong>painting<\/strong> on <strong>canvas<\/strong>: until we see <strong>computers<\/strong> \/ <strong>robots<\/strong> painting like humans, I will not let my team&#8217;s code be ruined by code formatters!<\/p>\n<p><a href=\"https:\/\/www.npmjs.com\/package\/prettier\">https:\/\/www.npmjs.com\/package\/prettier<\/a><\/p>\n<p>To me, it&#8217;s just another unnecessary piece of over-engineered tool.<\/p>\n<p>Think about it: it is like someone always stopping you while you are talking, and correcting everything you say.<\/p>\n<p>People should spend their time on better things, life is short :)))<\/p>\n<p>You can agree with your team and possibly follow <strong>industry standards<\/strong> as much as possible and document it &#8211; not a long document &#8211; like ESLint with &#8220;recommended&#8221; settings. ESLint can give you warnings that you may choose to act on them. Readable code is much more important because it reduces time required to onboard new members, transfer knowledge, maintain code and fix bugs. Any waste of developer&#8217;s time is additional cost for the company &#8211; we can avoid easily. At the moment, IMHO, tools like prettier are not good enough.<\/p>\n<p><a href=\"https:\/\/www.npmjs.com\/package\/eslint\">https:\/\/www.npmjs.com\/package\/eslint<\/a><\/p>\n<p>Another excuse is <strong>consistency<\/strong> which is great but there should be room for exceptions as well esp. when we are integrating multiple systems with different styles, and we could write less code and comprehensible code in one function just by avoiding <strong>input\/output adaptation<\/strong> &#8211; that adaptation is usually excused because we do not like <strong>PascalCase<\/strong>, <strong>camelCase<\/strong> or <strong>lower_snake_case<\/strong> etc. BTW, I do not <strong>twitch<\/strong> when I see lines of code which has both styles :))) Input\/Output adaptation will also degrade system <strong>performance<\/strong>.<\/p>\n<p>I think this topic <strong>(Writing Beautiful Code)<\/strong> is one of the important aspects to consider, if one wants to increase his\/her &#8220;<strong>indispensability<\/strong>&#8221; score in the company. (Please note that I have worked for companies where people do not share their knowledge.)<\/p>\n<p>We also know that code linters and formatters will not <strong>rename<\/strong> our <strong>variables<\/strong>, <strong>constants<\/strong>, <strong>functions<\/strong> and <strong>models<\/strong>, yet. They will not make very long and chained <strong>expressions<\/strong> shorter and more meaningful. But if we write code that is easy to read and understand, everyone will enjoy their job and excel at it. Even the code will not require too many <strong>comments<\/strong>.<\/p>\n<p>Here is a quick one:<\/p>\n<pre lang=\"ts\">\r\nif (calculateAge(new Date(u.dob)) > 18 && u.ccode === 'GB' && (u.status === 'pending') && (u.tac === 1)) {\r\n  u.status = 'active';\r\n  save(u);\r\n}\r\n<\/pre>\n<p>vs<\/p>\n<pre lang=\"ts\">\r\nif (user.canRegister()) user.register(); \/\/ much simpler\r\n\r\n\r\n\r\nclass User {\r\n  canRegister(): boolean {\r\n    return this.isAccountPending()\r\n      && this.hasAgreedTermsAndConditions()\r\n      && this.isLegallyAdult();\r\n  }\r\n  register(): boolean {\r\n    this.status = UserStatusEnum.Active;\r\n    return this.db.update(this);\r\n  }\r\n  isAccountPending(): boolean {\r\n    return this.status === UserStatusEnum.Pending;\r\n  }\r\n  hasAgreedTermsAndConditions(): boolean {\r\n    return this.tac !== 0;\r\n  }\r\n  age(): number {\r\n    return calculateAge(new Date(this.dob));\r\n  }\r\n  countryCode(): string {\r\n    return this.ccode;\r\n  }\r\n  isLegallyAdult(): boolean {\r\n    return this.age() >= countryAgeLimit[this.countryCode()];\r\n  }\r\n}\r\nconst countryAgeLimit = {\r\n  GB: 18,\r\n}\r\nenum UserStatusEnum {\r\n  Pending,\r\n  Active,\r\n}\r\nfunction calculateAge(dob: Date): number {\r\n  const diffInMs  = Date.now() - dob.getTime();\r\n  const ageAsDate = new Date(diffInMs);\r\n  return Math.abs(ageAsDate.getUTCFullYear() - 1970);\r\n}\r\n<\/pre>\n<p>I hope that helps.<\/p>\n<p>Enjoy coding! \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>prettier or not: you decide! Here is a piece of TypeScript code before and after using prettier.<\/p>\n","protected":false},"author":2,"featured_media":1255,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[228,227,201],"class_list":["post-1239","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology","tag-formatters","tag-linters","tag-typescript"],"_links":{"self":[{"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1239","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=1239"}],"version-history":[{"count":18,"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1239\/revisions"}],"predecessor-version":[{"id":1272,"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1239\/revisions\/1272"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/media\/1255"}],"wp:attachment":[{"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=1239"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=1239"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=1239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}