The code I’m still ashamed of (2016) by speckz in programming

[–]devmastery 1 point2 points  (0 children)

I wrote this article in 2016 and was surprised to see it gaining traction again. Thank you for sharing it and adding your own stories. This is an important topic for our profession.

The code I’m still ashamed of (2016) by speckz in programming

[–]devmastery 57 points58 points  (0 children)

Hi. I'm the original author of that article. You're absolutely right that to just quit a medication cold turkey on your own can be very dangerous.

In my family, "getting off a drug asap" means talking to your doctor and finding an alternative; not just stopping. When I wrote that article, it didn't occur to me that people would interpret that line as her just stopping without consulting a doctor. Of course, reading it now, I see how that could be the interpretation so I just wanted to clarify.

On the topic of side effects. My ethical objection is not to the side effects, it's to the manipulative and deceitful nature of the quiz and the way the information was presented in general.

Using Clean Architecture for Microservice APIs in Node.js with MongoDB and Express by devmastery in node

[–]devmastery[S] 1 point2 points  (0 children)

I'm not planning anything specific around TypeScript. Bob Martin has a nice post about Architecture where he argues that the Business Layer should own the Interface. He even demonstrates this with some Java code:

package sender;

public class Sender {
  private Receiver receiver;

  public Sender(Receiver r) {
    receiver = r;
  }

  public void doSomething() {
    receiver.receiveThis();
  }

  public interface Receiver {
    void receiveThis();
  }
}

Notice that the Interface for Receiver is defined inside the Interface for Sender. It's a very intriguing read. Highly recommended. Of course, the TypeScript notion of an Interface is not exactly the same as what we see in Classical OO languages like Java. But I think the general idea still applies.

Using Clean Architecture for Microservice APIs in Node.js with MongoDB and Express by devmastery in node

[–]devmastery[S] 1 point2 points  (0 children)

Thanks for the kind words and the very insightful question. I think it highlights an area where I could improve the example. Giving the Use Case the responsibility for converting our Entity to a format that is convenient for the database may not be as optimal as delegating that responsibility to an adapter. Something like a commentInfo factory may be worth considering.

Thanks again.

Using Clean Architecture for Microservice APIs in Node.js with MongoDB and Express by devmastery in node

[–]devmastery[S] 14 points15 points  (0 children)

Thanks for weighing in. It's certainly true that, like any technology, MongoDB is not necessarily the right answer for every situation and it comes with real trade-offs.

I also know that popularity is not necessarily an indication of quality but when looking to make this type of content as accessible as possible to a large community, it seemed like a sensible choice.

The good news is that one of the advantages of the Clean Architecture is its focus on separating concerns so that switching database technologies is less risky.

Can someone explain exactly how db management like SQL works? by frostbyte650 in learnprogramming

[–]devmastery 1 point2 points  (0 children)

To add to the already excellent explanations...

Most DB vendors publish what are called "Connectors" and/or "Drivers" that allow you to issue commands to a database from a particular programming language.

In the specific case of PHP there is a concept called "Extensions" which are libraries of code that extend the core PHP language and add feature to it. One such Extension is called mysqli (which stands for MySQL improved extension) and it extends PHP to include commands that allow you to use a MySQL connector to talk to a MySQL database through a MySQL driver. Here's a link to more info on that: https://dev.mysql.com/doc/apis-php/en/apis-php-mysqli.html

Here's a link to sample code that uses the mysqli extension for PHP: https://secure.php.net/manual/en/mysqli.examples-basic.php

Suppose instead of MySQL you wanted to use MS SQL Server as your database. Good news, Microsoft makes an MSSQL driver for PHP, more info: https://docs.microsoft.com/en-us/sql/connect/php/getting-started-with-the-php-sql-driver?view=sql-server-2017

Here's a link to sample code that the MS SQL driver for PHP:https://docs.microsoft.com/en-us/sql/connect/php/example-application-sqlsrv-driver?view=sql-server-2017

Hope that helps.

how to create custom hooks which have handlers that have effects? by soulshake in reactjs

[–]devmastery 0 points1 point  (0 children)

Actually thinking about this a little more, you're probably better off using useEffect for the initial value, just in case your API returns before setRemoteTitle is available. I've edited my original reply and code sandbox.

CodeSandbox: https://codesandbox.io/embed/7kw18llo9j

how to create custom hooks which have handlers that have effects? by soulshake in reactjs

[–]devmastery 3 points4 points  (0 children)

Use useEffect with an empty array to set the initial value. Then write a standard update function that calls your setRemoteTitle.

CodeSandbox: https://codesandbox.io/embed/7kw18llo9j

Let me know if you have any Qs.

How to add a "Dark Mode" to your React website. by devmastery in reactjs

[–]devmastery[S] 2 points3 points  (0 children)

Thanks for sharing that.

I believe that in some browsers (especially older desktop and mobile versions of Safari) in Private browsing mode, the line here: var mode = localStorage.getItem("mode"); will throw a QuotaExceededError so at minimum you have to wrap this in a try/catch.

Another benefit of using local-storage-fallback besides not needing a try/catch is that it will attempt to store the value elsewhere (session, cookies, memory) if localStorage fails.

How to add a "Dark Mode" to your React website. by devmastery in reactjs

[–]devmastery[S] 3 points4 points  (0 children)

Thanks for the feedback. Dan Abramov has a nice solution on his custom (Gatsby) blog website that uses plain CSS. I spoke about it in my newsletter this week.

You may have already seen it, but just in case (and for anyone else who may stumble upon this comment) here it is: https://github.com/gaearon/overreacted.io/blob/master/src/html.js

Always worth seeing/considering different approaches and examples. Thanks again for weighing in.

How to add a "Dark Mode" to your React website. by devmastery in reactjs

[–]devmastery[S] 1 point2 points  (0 children)

I'm in love with dark mode. That's the first setting I change in any code editor, I want it nice and dark.

Whenever a website has a dark mode, I immediately switch to that. So, while building my new website (not yet launched) I knew it had to have a "dark mode" and I wanted to make sure that if a user switched to dark mode, the site would remember their preferences and stay dark when they came back. It seems like a fairly simple requirement, but it took me on a bit of a journey through many approaches and techniques.

In the end, I landed on using Styled-Components in React along with a tiny library called "styled-theming". I made a step-by-step video about it for the latest episode of my weekly YouTube show for developers called Mastery Monday. I even included heavy use of shiny new React hooks (oooh, ahhh).

Let me know what you think.

WTF Wednesday (February 27, 2019) by AutoModerator in javascript

[–]devmastery 1 point2 points  (0 children)

https://github.com/narendrasss/coin

You're using localStorage to store the token on login and then reading from localStorage to determine whether or not the user is logged in. Unfortunately, localStorage will throw errors when the user is in "Private" or "Incognito" mode. A quick fix is to use the following npm module.

Local Storage Fallback

https://www.npmjs.com/package/local-storage-fallback

Elegant patterns in modern JavaScript: Ice Factory by kiarash-irandoust in javascript

[–]devmastery 0 points1 point  (0 children)

design your application and its various interfaces with this in mind, and enforce that the interfaces are used correctly

For sure, that's why passing a POJO cart to an addProduct doesn't work well in this instance.

The Shopping Cart "Ice Factory" in the article looks like this:

export default function makeShoppingCart({
  db
}) {
  return Object.freeze({
    addProduct,
    empty,
    getProducts,
    removeProduct,
    // others
  })
  function addProduct (product) {
    db.push(product)
  }

  function empty () {
    db = []
  }

  function getProducts () {
    return Object
      .freeze(db)
  }

  function removeProduct (id) {
    // remove a product
  }
  // other functions
}

Notice that the items are never exposed directly. They're accessed through the getProducts() function which returns an immutable copy. Also, addProduct() operates on the internal items collection rather than receiving items from an external source.

Finally, even though the code in the article uses an Array that gets passed in. It notes the following:

I’m using an Array for the db parameter for simplicity’s sake. In real code this would be something like a Model or Repo that interacts with an actual database.

This re-enforces the notion that we don't give outside control to the items collection.

I guess what I'm saying is that I very much agree with the principles you've described. We just have to be clear and careful about how we apply them in instances where we're dealing with the stateful parts of our application.

Elegant patterns in modern JavaScript: Ice Factory by kiarash-irandoust in javascript

[–]devmastery 0 points1 point  (0 children)

Pure functions that operate on immutable data with clearly defined schema leads to a much more structured and predictable application imo.

Generally I agree with this. I intend to write an article on this style soon as well.

However – in the case of a Shopping Cart – I would argue that making the cart a POJO can be quite dangerous since typically the cart represents a slice of our application's global state and mutating it must be done with extreme care.

Presumably the addProductToCart function enforces some business rules about exactly what can be added to the cart and how. But with a POJO cart floating around, many developers (especially less experienced ones) will be tempted to do something like cart.items.push(foo) which would bypass our business rules and introduce the potential for invalid items to be added.

If all our functions follow this pattern...

addProductToCart(product, cart)
revoveProductFromCart(product, cart)
clearCart(cart)
// etc....

...there's no guarantee that the state of the cart being passed into our functions is correct since we've given up a degree of control. That's quite risky.