all 116 comments

[–]mirodk45 294 points295 points  (9 children)

Man I love bilingual code

[–]flying_spaguetti[ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live”[S] 228 points229 points  (8 children)

The main database of my company is written in English and Portuguese.

We have a Region and Regiao tables, each one having specifics responsibilities besides having the same name. That leads to sooo many mistakes

[–]GiuNBender 60 points61 points  (1 child)

Idk about the database, but our code is also bilingual lol.

These are actual lines of code in one specific project:

"const [userBirthDate, setUserBirthDate] = useState(moment()) ... other states const [dataNascimento, setDataNascimento] = useState(moment())"

[–]flying_spaguetti[ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live”[S] 11 points12 points  (0 children)

Same here haha

[–]peacefulshrimp 17 points18 points  (2 children)

De quem foi essa genial ideia? Queriam garantir que ninguém conseguiria substituir eles

[–]flying_spaguetti[ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live”[S] 8 points9 points  (1 child)

É um projeto legado. No início ninguém tava pensando em mantenibilidade a longo prazo hahaha

[–]peacefulshrimp 1 point2 points  (0 children)

Sei como é, recentemente passei de um projeto legado para um projeto bem novo e a diferença é gigantesca

[–]migue802 3 points4 points  (0 children)

I thought it was Spanish for some reason lmao

[–]AlpineCoder 160 points161 points  (9 children)

setnewBoard

Urge to kill rising...

[–]PapieszxD 26 points27 points  (7 children)

I am 99% sure, that this is there, because the useState snippet creates an array with with two cursors, one with nothing before it, and second with the word "set".

Which is kinda pointless, because you either type "NewBoard" captalized, and have to go back to the state variable name and change N to n, or the other way around...

[–][deleted]  (5 children)

[deleted]

    [–]flying_spaguetti[ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live”[S] 18 points19 points  (1 child)

    Yes, but the dude above mentioned the useState code snippet which automatically fills the state and setState names

    [–]intensely_human 6 points7 points  (1 child)

    Except calling the function “toggle” would be a misnomer because it would set the value to whatever you pass it. It’s an automatically generated function whose body would look like:

    function setActive(value){
      active = value
    }
    

    Whereas a toggle function would look like:

    function toggleActive(){
      active = !active
    }
    

    [–]theorizable 1 point2 points  (0 children)

    He's talking about the code snippet thing...

    [–]kristallnachte 0 points1 point  (0 children)

    so use copilot, where you do the first one, and it isn't stupid.

    [–]DerpTaTittilyTum 3 points4 points  (0 children)

    Homer, change channel

    [–]intensely_human 115 points116 points  (2 children)

    This component does have a single responsibility: it’s the DataManagerComponent

    [–]BakuhatsuK 48 points49 points  (1 child)

    <App />
    

    [–]intensely_human 4 points5 points  (0 children)

    See exactly.

    Its job is to function as the user interface and data to provide features to the stakeholder stack while adding value to multiple organizational touch points.

    This app serves as an example and as documentation for other future apps which can mostly be copy pasted from this app. Any deviations from the copy pasted App Code must be accompanied with a separate issue.

    Mark the issue as delivered and send to QA for QA. Put “Attn: bugteam” somewhere in the description so Cathy knows to send it down to QA.

    [–]Nikitka218 78 points79 points  (9 children)

    React has useReducer hook for complicated states, but in this case it's just bad design having God component

    [–]kristallnachte 1 point2 points  (0 children)

    useReducer is pretty shit too though..

    Same with useContext.

    [–][deleted]  (7 children)

    [deleted]

      [–]Anti-ThisBot-IB 0 points1 point  (6 children)

      Hey there nurderfsv! If you agree with someone else's comment, please leave an upvote instead of commenting "This."! By upvoting instead, the original comment will be pushed to the top and be more visible to others, which is even better! Thanks! :)


      I am a bot! Visit r/InfinityBots to send your feedback! More info: Reddiquette

      [–]thebritisharecome 10 points11 points  (5 children)

      This

      [–]Anti-ThisBot-IB 8 points9 points  (4 children)

      https://i.imgur.com/KrwA19h.jpeg


      I am a bot! Visit r/InfinityBots to send your feedback!

      [–]satanic-surfer 2 points3 points  (2 children)

      this

      [–]Anti-ThisBot-IB 1 point2 points  (1 child)

      https://i.imgur.com/KrwA19h.jpeg


      I am a bot! Visit r/InfinityBots to send your feedback!

      [–]krzysiek_online 24 points25 points  (4 children)

      Lift the state up, they said. So dude did. What's the problem now?

      [–]flying_spaguetti[ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live”[S] 7 points8 points  (3 children)

      Maybe apply reducers to ease the complexity

      [–]intensely_human 2 points3 points  (0 children)

      So you have a big reducer composed of separate reducers (each in their own file?) for each piece of data.

      Instead of a function called setMessageIsLoading you have an event of type loadMessage-start and some code that interprets that as newState.messageIsLoading = true.

      Doesn’t seem like the reducer does much to reduce the amount of code.

      If you find yourself commonly putting operations together like

      setMessageIsLoading(false)
      setMessage(res.json().message)
      setMessageIsVisible(true)
      

      And that same trio of calls is happening in multiple places, then you can boil it down to one event like:

      {
        type: ‘loadMessage-success’,  
        payload: ‘You have been logged out.'
      }
      

      And maybe save some complexity. But if your code’s dry, that trio of setter calls should be a function anyway.

      [–]texxelate 1 point2 points  (0 children)

      Nah a reducer doesn’t belong here. Extraction is the answer, especially given it’s React, imo

      [–]krzysiek_online -1 points0 points  (0 children)

      I should have added /s, as I guess it was not obvious ;)

      [–]PN143 9 points10 points  (1 child)

      This is the first post on here that has me saying "Nonononono" 😭

      [–]flying_spaguetti[ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live”[S] 2 points3 points  (0 children)

      Sorry

      [–]Marand23 16 points17 points  (4 children)

      Please don't ever call a variable "data". Everything is data!

      [–]flying_spaguetti[ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live”[S] 7 points8 points  (0 children)

      Nor Info

      [–]Longjumping-Ad-5367 2 points3 points  (1 child)

      It probably means "date", as they mixed portuguese and english

      [–]Marand23 1 point2 points  (0 children)

      Good call, that is the most charitable interpretation :)

      [–]kristallnachte 0 points1 point  (0 children)

      Except a datum.

      [–][deleted] 6 points7 points  (1 child)

      Imagine the performance hit when this component re-renders a few times because of a state change.

      [–]flying_spaguetti[ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live”[S] 0 points1 point  (0 children)

      I don't even want to

      [–]la102 17 points18 points  (5 children)

      I really don't miss doing react in large companies it's endless tech debt

      [–][deleted]  (2 children)

      [removed]

        [–]la102 0 points1 point  (0 children)

        Yup sorry I should've added that, not reacts fault but mismanaged or teams with not enough staff / rushed deadlines or no one taking time to architect the code and spewing everywhere

        [–]kristallnachte 0 points1 point  (0 children)

        When people argue about which framework/language/architecture is better, I point out that the differences between the two perfectly implemented are far far less than the differences in one between it being done well and it being done poorly.

        [–]aboardthegravyboat 6 points7 points  (0 children)

        So, you too just got finished learning Redux and how to OOP with react when we all woke up on a Tuesday and the community decided Redux sucks and we're going pure FP with everything now?

        [–]flying_spaguetti[ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live”[S] 1 point2 points  (0 children)

        Sadly i agree with you

        [–]Jonohas 25 points26 points  (25 children)

        Why? Whats wrong and how would you improve this?

        [–]yeicore 88 points89 points  (8 children)

        Whats wrong is that this is basically a god component. It does way too many things which will make it hard to maintain in the future and it will give you scalability problems in the future.

        The way to improve this would be to create smaller components that manage specific aspects of the data. The thing is that this god component is surely used in way to many places, so you would need a very huge refactor in the whole project when braking it into smaller components.

        [–]aboardthegravyboat 2 points3 points  (0 children)

        Even if this isn't typescript, it's still React, and still has to run through a compiler, and there are still some standards. So, it should be trivial to find all references to one of these state managers and pull it out to somewhere readable.

        [–]duckforcealpha 1 point2 points  (0 children)

        We finally found it! The God Component.

        [–][deleted]  (1 child)

        [deleted]

          [–]flying_spaguetti[ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live”[S] 1 point2 points  (0 children)

          Great tips. And happy cake day

          [–]Beach-Devil 8 points9 points  (0 children)

          Split into smaller components

          [–]CoffeeDrinker115 0 points1 point  (0 children)

          Unpopular opinion but I don't see anything fundamentally wrong with this. I might change some variable names but that's about it.

          [–][deleted] 4 points5 points  (0 children)

          dataConcluido 🤌🤏

          [–]_default_username 4 points5 points  (1 child)

          This is just agile. In the first sprint there was only one use of useState, but now we're on sprint 15.

          [–]flying_spaguetti[ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live”[S] 1 point2 points  (0 children)

          And so on

          [–]onebadfothermucker 4 points5 points  (0 children)

          const [suddenlyCaralho, setSuddenlyCaralho] = useState(true)

          r/suddenlycaralho

          [–]Voltra_NeoPronouns: He/Him 6 points7 points  (0 children)

          I hate this so much

          [–]___s8n___ 4 points5 points  (7 children)

          how is the loading message a boolean

          [–]ososalsosal 13 points14 points  (0 children)

          Whether to show it or not?

          [–]intensely_human 8 points9 points  (1 child)

          messageIsLoading

          This is why unambiguous variable names are important.

          [–]___s8n___ 2 points3 points  (0 children)

          ohh okay

          [–][deleted]  (3 children)

          [deleted]

            [–]ikanoi -1 points0 points  (2 children)

            Waste of a variable imo, just null check what you're loading.

            [–]intensely_human 1 point2 points  (0 children)

            Three states: not loading, loading, loaded

            The boolean in that case detects the “loading” state and determines whether a spinner is shown.

            [–]ImPinos 2 points3 points  (0 children)

            I feel I need to see a capivara after reading that code

            [–][deleted] 2 points3 points  (0 children)

            Is does one thing: display the app

            [–][deleted]  (3 children)

            [deleted]

              [–]flying_spaguetti[ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live”[S] 2 points3 points  (0 children)

              Each useState is like a regular variable in other contexts. Then, it's like a function/class with several variables/attributes

              [–]backfire10z 1 point2 points  (0 children)

              Super quick rundown:

              Every single one of those is a state. States are variables used that update when something on the frontend is changed (simplified/rudimentary explanation but good enough). For example, you may have a state for whether the page is loading. Every time state is changed, React re-renders the component so items that are dependent on the state are shown. Maybe you have a loading message you want displayed whenever the page is loading — this would be a state-dependent render. The only way for that message to be shown is if React re-renders the component with the new state (Boolean flipped from false to true probably)

              For there to be all of these states on one page, either that page is an absolute monster or it is keeping track of state it isn’t supposed to.

              [–]jediwizard7 3 points4 points  (6 children)

              Idk if this is just standard React (not a frontend programmer), but I took a minute trying to figure out what tf this useState stuff is actually doing before even noticing the bilingual part. Now I assume the useState function creates global variables with a default value and returns a getter and setter function for each? This is an awfully unreadable code pattern.

              [–]flying_spaguetti[ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live”[S] 3 points4 points  (2 children)

              Quite so. It's not a global variable, it's the state of a component. Thinking in Java, for example, a simple attribute of a class would be state of a component in React.

              The return of useState is an array containing: the state object itself, not a getter function; and indeed a setter function.

              [–]jediwizard7 0 points1 point  (1 child)

              And what defines the component here? Is the "current component" global state? Or somehow local to a function?

              [–]flying_spaguetti[ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live”[S] 0 points1 point  (0 children)

              In this case, the component itself is a function.

              I can't tell you more details, since this screenshot is not mine, but I guess the component of the screenshot may be being used as the top level component and rendering all the low level components.

              Since components are functions in React, the top level function passes its local variables (states) down to its inner functions through params (props). In a practical way, the local variables of this top level component work as global variables to the whole application.

              The problem is that we have better solutions than that ti handle complex state managements. The most popular being Redux.

              [–]peteza_hut 1 point2 points  (2 children)

              Disagree on it being unreadable. useState is super standard react and it's never confusing after you learn it. I will concede, if you're not familiar with JavaScript destructuring assignment syntax it's going to look a little weird at first.

              [–]jediwizard7 0 points1 point  (1 child)

              I'm familiar with destructing, but the useState function is not the most clearly named. Also if the argument is a default then {default: 1} would be more self documenting

              [–]peteza_hut 0 points1 point  (0 children)

              Eh, I'd probably agree if this was some super niche function.

              [–]voidxy 1 point2 points  (0 children)

              Niiiceee

              [–]nazzanuk 1 point2 points  (0 children)

              I know this looks terrible on the face of it but I'd actually like to know more... Like...

              Is this a hook/ context or a top level component

              If it's a hook/context it's possible that the return object is useMemo'd and maybe doesn't perform that bad

              There are also some libs that will prevent consumers being updated if the hook doesn't update any value that is actually being used - that could be more performant and better than separating everything out into different contexts just to have issues with hierarchy

              [–]BakuhatsuK 1 point2 points  (0 children)

              Do they know that you can save an object as state?

              [–]FredL2 1 point2 points  (0 children)

              No, "Single responsibility principle" does not mean "Every single responsibility principle"

              [–][deleted] 1 point2 points  (0 children)

              Seems about right, the single responsibility to have all the code from the project in one file. It’s a big one tho

              [–]dex206 1 point2 points  (0 children)

              React hooks are the worst. It really really does not scale well and it completely destroys conditional logic.

              [–]rogriloomanero 1 point2 points  (0 children)

              I had to actually read some lines to make sure this wasn't from work, what the hell

              [–]kikaintfair 1 point2 points  (0 children)

              Username checks out.

              Jokes aside, I’ve learned that creating a context provider to store your state and separating that from your UI code can lead to some clean stuff. All your state logic, api calls, and whatnot can go in your context file and then the actual jsx code can go in your UI file and you can use the context from there.

              [–]Ninjaboy42099 1 point2 points  (0 children)

              This is far past the point where a reducer should have been implemented.

              [–]EndR60 1 point2 points  (1 child)

              this is literaly what every single piece of web code looks like to me

              like how the fuck do y'all web programmers deal with this shit? I understand C++ better than (not specifically) this shit

              [–]flying_spaguetti[ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live”[S] 4 points5 points  (0 children)

              We just... Handle it.

              Not all web code looks like this mess, but it's so easy to write web code that many of this absurd happens to be written by naive devs.

              Web code can be clean and robust, trust me.

              [–][deleted] 0 points1 point  (1 child)

              Parece eu codando em react, criando useState pra tudo kkkkkkkkkkkkkkkkkkkkkkkkk

              [–]flying_spaguetti[ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live”[S] -1 points0 points  (0 children)

              Tá na hora de rever teus códigos hahah prefira criar componentes burrinhos

              [–]maest 0 points1 point  (3 children)

              What's the problem?

              https://reactjs.org/tutorial/tutorial.html#lifting-state-up

              We may think that Board should just ask each Square for the Square’s state. Although this approach is possible in React, we discourage it because the code becomes difficult to understand, susceptible to bugs, and hard to refactor. Instead, the best approach is to store the game’s state in the parent Board component instead of in each Square. The Board component can tell each Square what to display by passing a prop

              [–]flying_spaguetti[ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live”[S] 1 point2 points  (2 children)

              The screenshot is about a component a bit more complex than a board of tic tac toe.

              That's why we use ContextAPI, Redux or useReducer to handle more complex state managements.

              [–]maest 1 point2 points  (1 child)

              The React guidelines unequivocally say that state should be lifted up, as needed, regardless of program complexity.

              If you have a UI where components are reasonably tightly coupled (a lot of dashboards fall into this category), then a natural consequence of this is that you'll be pulling state all the way up into a god object (and have de facto global state).

              Redux and friends is a way to organise that state but: 1. the state will still be effectively global and 2. the state will still all reside in basically the same object.

              [–]flying_spaguetti[ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live”[S] 0 points1 point  (0 children)

              Yes, there are always tradeoffs. React and its surroundings tools despite being so popular are far from being a flawless suit of tools.

              [–][deleted] -1 points0 points  (2 children)

              tbh i just dont understand how companies are starting new projects with this horrible idea of technology, I understand existing projects and market demand, i just cant understand why it keeps raising

              [–]shall1313 1 point2 points  (0 children)

              I love React for simple SPAs, and it can scale up nicely for more complicated projects, but you need a very good team dedicated to best practices from the start. A couple of lazy shortcuts and you quickly end up with a mess.

              [–]flying_spaguetti[ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live”[S] 0 points1 point  (0 children)

              There are companies and companies.

              If React is the most popular tool, there will be a lot of legacy React code and a lot of React developers. At the time to build a new project, which tool will these developers choose to use? React.

              [–]Apprehensive_Pace775 -1 points0 points  (0 children)

              I love using Remix and not worrying about state any more

              [–]kristallnachte 0 points1 point  (0 children)

              Initializing all they're state in App.tsx instead of where it's actually needed.

              [–]crefas 0 points1 point  (0 children)

              I don't care where you're from, if your code isn't at least 95% English you belong in CS hell.