Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for #
bob.ts
bob.ts

Posted on

     

#"/t/javascript">#javascript#frontend#pattern#antipattern

pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
punund profile image
punund
  • Pronouns
    an example of a pronoun would be "neither" or "one another"
  • Joined

What real anti-pattern in your code is mixing types, and this has nothing to to with!!. Your function may receive anObject, aString, or anull, and had no notion of it. Another anti-pattern istypeof for such cases.

This is solved with anything from the Maybe monad to typing your data correctly:

constdata1={image:'bob.jpeg',error:null}constdata2={image:null,error:'Bob was not found'}constdata3={image:null,error:'Wrong authentication'}
Enter fullscreen modeExit fullscreen mode

In the real word,!! is not needed for other reasons.
!! converts from "falsy" tofalse, but it has no value by itself, as any JS context expecting a boolean, does it for you:

// if and ternaryif(2)'this is true'if('')'this is false'[]?'any array is true':newError('must never happen')0?'zero is not true':'it is false'
Enter fullscreen modeExit fullscreen mode
// logical operators2&&true// ==  true0||1||2//  == 1, obviouslytrue&&{}==true
Enter fullscreen modeExit fullscreen mode

So in your original codeif(image) is strictly equivalent toif(!!image).

CollapseExpand
 
radulle profile image
Nikola Radulaški
  • Location
    Belgrade, Serbia
  • Joined

TS would have helped here a lot.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

bob.ts
REAL, FUN, GEEK who is passionate about Front-End!
  • Location
    Columbus, OH, USA
  • Education
    BS of Computer Science, Univ. of Dayton
  • Work
    Senior Solutions Developer II at Leading EDJE
  • Joined

More frombob.ts

[SCARY] Visual Regression Testing
#devops#testing#playwright#frontend
CSS' only-child instead of Conditional Logic
#webdev#css#development#frontend
I took a Bootcamp Fullstack Course
#webdev#javascript#beginners#learning
DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp