Programming thread

  • 🐕 I am attempting to get the site runnning as fast as possible. If you are experiencing slow page load times, please report it.
I thought programming was already like a game. If it's too hand-holding, like trying to be educational, it will suck. Otherwise, why not learn real programming, I hear it's like playing a game only re-compiles don't cost quarters.

Programming is all about freedom. So why the fuck would I want some one telling me what I should be doing with this code-by-numbers bull shit.
I think when people don't know where to start they need some direction, it's just that these specific platforms can often be the wrong one.
Programming is more than freedom. It's magical. I always find it sad that academia teaches "practical" things and not a mixture of Scheme, Smalltalk, SML and Haskell. Programming is not about computers and not a science. *hat tips Jerry*
 
So having become incredibly annoyed with React V5, I have given up with JS/TS entirely and moved to Dart/Flutter. Holy hell, is it better. There is so much less fuckery and legacy shit going on. Documentation can be a bit shit, and trying to unlearn JS behaviours is tricky, but so far I am loving it.

BLoC is pure sex, and is making things far, far simpler. I don't want to ever be a front end developer, but it's so much less painful than the alternatives I've used.

When do my programming socks arrive?
 
So having become incredibly annoyed with React V5, I have given up with JS/TS entirely and moved to Dart/Flutter. Holy hell, is it better. There is so much less fuckery and legacy shit going on. Documentation can be a bit shit, and trying to unlearn JS behaviours is tricky, but so far I am loving it.

BLoC is pure sex, and is making things far, far simpler. I don't want to ever be a front end developer, but it's so much less painful than the alternatives I've used.

When do my programming socks arrive?
Can Dart/Flutter compile into a client only js-app like React?
---
Edit: I answered my own question. It does look like Dart can replace the parts of a web app that are done in JS, but it's similar to using vanilla JS and not something like react.
<https://dart.dev/tutorials/web/fetch-data>
Code:
 intAsJson = querySelector('#intAsJson') as TextAreaElement;
  doubleAsJson = querySelector('#doubleAsJson') as TextAreaElement;
  boolAsJson = querySelector('#boolAsJson') as TextAreaElement;
  stringAsJson = querySelector('#stringAsJson') as TextAreaElement;
  listAsJson = querySelector('#listAsJson') as TextAreaElement;
  mapAsJson = querySelector('#mapAsJson') as TextAreaElement;
Its just like using document.getElementById("intAsJson");.

Flutter seems like a more traditional way to write GUIs. It looks a little bit like Java Swing to me. Type definitions look good. The constructor syntax where the parameters automatically become this.class_var is something I've wanted in Python3, so it's good.

What attracted me to ReactJS was that you can compose HTML alongside JS and Components, so you do very litter work if any out side of a JSX file. Even CSS can be import right into JSX or you can create a JSON that mimics CSS values. Now after some experience, I too think JSX in retarded and a horrible community of developers. Certain things feel very much like a JS DSL with React stuff; nicer then JS. Having to interact with normal JS-HTML stuff interrupts the DSL. Inevitably it starts to feel like a Frankenstein's monster, where you still can't say, 'make this div centered both horizontally and vertically'. Much of the style added by React is better then plain JS, but simple things that HTML-JS-CSS have never done well don't get fixed at all. React is only a marginal improvement over JS.
---
Edit:
After looking at Flutter I've decided against it. Flutter's widgets are composed like a serialisation format, so it's like returning a JSON document, heavily nested, with all the formatting and styling for a page. At least components are very composable and easy to refactor and don't look like JSON hell.
 
Last edited:
So having become incredibly annoyed with React V5, I have given up with JS/TS entirely and moved to Dart/Flutter. Holy hell, is it better. There is so much less fuckery and legacy shit going on. Documentation can be a bit shit, and trying to unlearn JS behaviours is tricky, but so far I am loving it.

BLoC is pure sex, and is making things far, far simpler. I don't want to ever be a front end developer, but it's so much less painful than the alternatives I've used.

When do my programming socks arrive?
Log in to the intranet and you'll see the form you need to fill out under your account info section. You'll have to select whether you want the trans flag colors or the traditional rainbow stripes, and you'll also need to solve a simple programming puzzle in Kotlin. Note that your performance in this fun mandatory exercise will be noted on your permanent record.
 
---
Edit:
After looking at Flutter I've decided against it. Flutter's widgets are composed like a serialisation format, so it's like returning a JSON document, heavily nested, with all the formatting and styling for a page. At least components are very composable and easy to refactor and don't look like JSON hell.
Faie enough, although I would suggest that if you're worrying about nesting depth, something's gone a bit wrong with the structure, and it's worth decomposing it into more child widgets.

It's also interesting how React seems to encourage you to switch out components with if else ladders, and flutter doesn't. E.g AppBars - React seems to infer that switching the content in the main area is the best option for navigation, whereas flutter says to just to create a customised appbar and use it wherever you need one. I'm sure either approach can be done on either platform, but it's interesting how each has its 'preferred' option
 
Does anyone here know of any good places to discuss programming that aren't:
  1. Full of bitter autists (regular autists are fine)
  2. Full of trannies
  3. Full of the kind of people who care about Codes of Conduct
  4. Full of people who speak horrible English
Aside from this wonderful thread of course.

I may be a programmer, but dealing with fellow programmers has made me hate computers and anyone who uses them for any reason. I don't know why it's such a uniquely awful community.
Facebook of all places.

Also discovering the Rust community is hilarious. All of the stack overflows are Q: "I got this code working, but it feels like there should be a better way to do this"
Rust Community "No, that's the best way to do that."
 
Faie enough, although I would suggest that if you're worrying about nesting depth, something's gone a bit wrong with the structure, and it's worth decomposing it into more child widgets.

It's also interesting how React seems to encourage you to switch out components with if else ladders, and flutter doesn't. E.g AppBars - React seems to infer that switching the content in the main area is the best option for navigation, whereas flutter says to just to create a customised appbar and use it wherever you need one. I'm sure either approach can be done on either platform, but it's interesting how each has its 'preferred' option
It seems like React strongly preferences explicit references where Flutter has more creative ways with Dart to do implicate references with stuff like keys and parent theme providers, Theme.of. In React it can get annoying and repetitive to have to copy the same props to child components over and over again.

The examples that had me concerned about nesting where 'let's build website ui in 10 minutes' youtube videos. They must have made unrealistic compromises to just go the visual components.

I've only started using React since my current project. I might be using in wrong. You tell me.
JavaScript:
import React from 'react';
import axios from 'axios';
import Strings from './strings.js';
import Theme from 'theme';

const DISPLAY_STORAGE = "StorageKey";
const SEARCH = "search-id";

function RadioButtons(options, storage)
{
    const [read, controls] = Strings.StringsFrom(options, storage);
   
    const buttons = controls.map(
    ([opt, is, set]) => {
        return (
        <label>
            <input type="radio"
               name="type"
               onChange={set}
               checked={is}
               value={opt}/>
            {opt}
        </label>
        );
    }
    );

    return [read, buttons];
}

export default function Controls(props)
{
    const [display, display_buttons] = RadioButtons(
    Object.keys(props.options),
    DISPLAY_STORAGE
    );
    function display_type(data)
    {
    return props.options[display](data);
    }

    const area = (
    <div id={SEARCH}>
        search controls
        <form>
        {display_buttons}
        <input type="text"/>
        <button onClick={e => e.preventDefault()}>
            Search!
        </button>
        <Theme.ThemeOptions default_theme={props.theme}/>
        </form>
    </div>
    );
    return [display_type, area];
}
 
So I do specialized programming in industry (which transfers over in some ways but not others) and I'm trying to get better at general-purpose programming for my own edification.

I built a basic little arithmetic calculator in Python using Tkinter for a GUI. I ended up having to use global variables to store my operator and operands because functions called by button clicks can't return anything for use by the rest of the program.

Now I'm told that global variables are the devil and I shouldn't use them if I can reasonably avoid it, but I don't see any good alternative even in this simple academic exercise with 100-ish lines of code. Even sites like Stack Overflow recommend using global variables in such cases, but it seems very weird to have to break the "rules" for something as trivial as clicking a GUI button.

I don't discount the possibility that I'm just retarded and taking a fundamentally wrong approach to what I'm doing. Does anybody have any insight on this?
 
  • Thunk-Provoking
Reactions: naffatune
So I do specialized programming in industry (which transfers over in some ways but not others) and I'm trying to get better at general-purpose programming for my own edification.

I built a basic little arithmetic calculator in Python using Tkinter for a GUI. I ended up having to use global variables to store my operator and operands because functions called by button clicks can't return anything for use by the rest of the program.

Now I'm told that global variables are the devil and I shouldn't use them if I can reasonably avoid it, but I don't see any good alternative even in this simple academic exercise with 100-ish lines of code. Even sites like Stack Overflow recommend using global variables in such cases, but it seems very weird to have to break the "rules" for something as trivial as clicking a GUI button.

I don't discount the possibility that I'm just retarded and taking a fundamentally wrong approach to what I'm doing. Does anybody have any insight on this?
Disclaimer: I don't like Python, and it's been a while since I've done desktop/mobile UI stuff. And by "arithmetic calculator" I assume you mean something similar to a basic pocket calculator that does addition, subtraction, division and multiplication, though I think these ideas will work with more complex things as well.

So what you basically need is something to store state. Pushing the buttons will add information (numbers and operations corresponding to the buttons pushed) to the state storage. Pushing the "equals" button will kick off a process which takes that state, does the calculation, and then prints the result to the "screen." Let's call this container to store state OperationStore. We are only going to instantiate one of those - a singleton.

Code:
let opStore = new OperationStore()

Now we're also going to add event listeners to the calculator buttons which put state into the store. When we instantiate it, we're going to give it a reference to opStore to do its stuff to.
Code:
class CalculatorButton extends UIButton {
  private OperationStore opStore
  private String buttonLabel
  private UILabel screen

  // Hey, look, it's dependency injection!
  func init(OperationStore opStore, UILabel screen, String label) {
    this.opStore = opStore
    this.label = label
    this.screen = screen
  }

  func onPush() {
    this.opStore.pushOperation(label)
    screen.setText(screen.getText().append(label))
  }
}

let screen = new UILabel("0")
window.addControl(screen)

let button1 = new CalculatorButton(opStore, screen, "1")
window.addControl(button1)
let button2 = new CalculatorButton(opStore, screen, "2")
window.addControl(button2)
…

So, wait. We're just passing off opStore to everything. Is that really better than a global variable?

Well, yes. Let's take the "screen" of the calculator, for example. It doesn't need to have a copy of opStore since it will never need to access it. So no code related to the "screen" will have access to opStore. But the parts that do have to access it can do so.

For a clearer example, say you added an MP3 player to your application. The MP3 player would need to have access to opStore, since it just plays audio files, and it would have its own buttons to do so. Yes, it doesn't make much sense for an application to both play MP3s and work as a calculator, but hopefully it illustrates other cases where several parts, but not all parts, of an application might need to share a bit of data, and it makes sense to isolate the data away from those parts that don't need it.

Just to finish out the example, here's how the "equals" button of the calculator might work. I was lazy and put the logic that actually does the calculation there too - maybe that's a good idea, and maybe it's not.

Code:
class equalsButton extends CalculatorButton {
  func onPush() {
    Float total = opStore.doCalculation()
    screen.setText(total.toString())
  }
}

Now it's time for others to tell me how shitty this structure is and how some other approach is better.
 
Also discovering the Rust community is hilarious. All of the stack overflows are Q: "I got this code working, but it feels like there should be a better way to do this"
Rust Community "No, that's the best way to do that."
This was my exact experience learning Golang and having code cluttered with if err != nil
 
So I do specialized programming in industry (which transfers over in some ways but not others) and I'm trying to get better at general-purpose programming for my own edification.

I built a basic little arithmetic calculator in Python using Tkinter for a GUI. I ended up having to use global variables to store my operator and operands because functions called by button clicks can't return anything for use by the rest of the program.

Now I'm told that global variables are the devil and I shouldn't use them if I can reasonably avoid it, but I don't see any good alternative even in this simple academic exercise with 100-ish lines of code. Even sites like Stack Overflow recommend using global variables in such cases, but it seems very weird to have to break the "rules" for something as trivial as clicking a GUI button.

I don't discount the possibility that I'm just retarded and taking a fundamentally wrong approach to what I'm doing. Does anybody have any insight on this?
Counterpoint: global variables are fun and easy and were good enough for assembly programmers, who were the Odin of programming. Anyone who says global variables are bad are Loki-worshipping cultists.

Seriously though, the only downside is that they're always taking up memory, but unless you absolutely abuse the unmitigated shit out of them nobody will ever notice or care. They're also significantly easier to code around than having ten million throwaway variables that daisy chain together into a spiderweb of dumbassery simply because convention says so.

"Global variables bad" is baby's first alpha nerding and should be treated with derision and pity. Just don't use them when it's obviously unnecessary and you'll be fine.
 
Last edited:
Seriously though, the only downside is that they're always taking up memory, but unless you absolutely abuse the unmitigated shit out of them nobody will ever notice or care. They're also significantly easier to code around than having ten million throwaway variables that daisy chain together into a spiderweb of dumbassery simply because convention says so.
Are you seriously suggesting using global variables in place of locals? What in hell is wrong with you? Are you an unfrozen caveman? Make no mistake, asm programmers used globals this way due to technical limitations, it certainly wasn't by choice. In what world is a global variable more more readable than a nicely scoped local variable? "Where's this variable accessed from?" "Anywhere!" Beyond that, global state is an exceptionally terrible idea in our asynchronous world, and in any modern context globals should be used for constants and scarcely anything else.

Stack allocations are basically free bro, the 80s were forty years ago and you don't need to be a miser about your 128 bytes of designated stack space.


Now I'm told that global variables are the devil and I shouldn't use them if I can reasonably avoid it, but I don't see any good alternative even in this simple academic exercise with 100-ish lines of code. Even sites like Stack Overflow recommend using global variables in such cases, but it seems very weird to have to break the "rules" for something as trivial as clicking a GUI button.
Given that your program only has "one UI" it may be reasonable to make the UI a piece of global state. It's probably fine for a simple example like this, but generally I would recommend collecting all of your global UI data into a class, and creating an interface through which to mutate it.

Python:
# I haven't used python in like 7 years so YMMV

class CalcState: # holds the state of the calculator
    sum = 0
    operand = 0
    op = ""
    hasOperand = False
   
    def AddOp(self, op:str): # pop the previous operation, and add a new operation
        match op:
            case "+":
                pass
            case "-":
                pass
            case "*":
                pass
            case "/":
                pass
            case _:
                raise ValueError("No such operator")
        if self.hasOperand:
            self.Calculate()
        self.op = op

    def AddDigit(self, digit:int): # add a digit to our operand
        self.operand = self.operand * 10 + digit
       
        self.hasOperand = True
   
    def Clear(self): # clear the calculator
        self.sum = 0
        self.operand = 10
        self.op = ""
        self.hasOperand = False
       
    def Calculate(self): # calculate the result of sum op operand
        match self.op:
            case "":
                self.sum = operand
            case "+"
                self.sum = self.sum + operand
            case "-"
                self.sum = self.sum - operand
            case "*"
                self.sum = self.sum * operand
            case "/"
                self.sum = self.sum / operand
       
        self.operand = 0
        self.hasOperand = False
        self.op = ""
       
    def FormatState(self):
        if self.op == "" and self.hasOperand:
            return str(self.operand)
        else:
            return str(self.sum) + " " + self.op + " " + str(self.operand)
   
   
class ProgramState: # holds the calculator state and the ui components
    global calc = CalcState()
   
    global enterButton = Button("Enter", lambda: calc.Calculate()) # psuedocode, I don't know any python gui libs
    global clearButton = Button("Clear", lambda: calc.Clear())
    global plusButton = Button("+", lambda: calc.AddOp("+"))
    global minusButton = Button("-", lambda: calc.AddOp("-"))
    #...
   
    global zeroButton = Button("0", lambda: calc.AddDigit(0))
    global oneButton = Button("1", lambda: calc.AddDigit(1))
    #...
   
    global screen = Label(lambda: calc.FormatState())

# each unit is self contained, the ways in which each unit interacts with the other are clear
# duty is also seperated, CalcState manages the state of the calculator, while ProgramState manages the UI
 
Last edited:
Given that your program only has "one UI" it may be reasonable to make the UI a piece of global state. It's probably fine for a simple example like this, but generally I would recommend collecting all of your global UI data into a class, and creating an interface through which to mutate it.
Cool, I'll play around with this.

I saw suggestions elsewhere online to use a class to deal with the UI, but every tutorial out there seems to give such unhelpfully trivial examples of classes ("Dog" and "Cat" are members of the class "Animal", etc.) that I couldn't figure out how they'd actually be applied in a real program.
 
  • Like
Reactions: Marvin
Cool, I'll play around with this.

I saw suggestions elsewhere online to use a class to deal with the UI, but every tutorial out there seems to give such unhelpfully trivial examples of classes ("Dog" and "Cat" are members of the class "Animal", etc.) that I couldn't figure out how they'd actually be applied in a real program.
In your case you can just subclass your Tkinter application or window class. It's pretty easy:
Code:
class MyRoot(Tk):
   def __init__(self, *args, **kwargs):
      super().__init__(*args, **kwargs)
      # your own shit
 
  • Informative
Reactions: Matt Damon
Is Delphi useful in any way?
Delphi as in the Pascal dialect? Fuck no.

Maybe they were useful once upon a time, but those languages are actually dead (as opposed to the 'undead' that COBOL, Fortran and Common Lisp are, where for all intents and purposes people should've moved on but they still have nonzero market share due to technical debt and autism).

ETA: Just to expound on this a little bit: there's literally no point to learning Delphi/Pascal in current year. There's no software out there still written in Delphi that you can make bank by knowing how to program in it like there is for COBOL and Fortran. There's nothing remotely fun or different or zany about using the language like there is for Common Lisp or Smalltalk or Prolog or other interesting relics from the past. Nobody these days is even going to know what Pascal is, and the ones that do know will ask why you didn't just use C or C++ instead.
 
Last edited:
Delphi as in the Pascal dialect? Fuck no.

Maybe they were useful once upon a time, but those languages are actually dead (as opposed to the 'undead' that COBOL, Fortran and Common Lisp are, where for all intents and purposes people should've moved on but they still have nonzero market share due to technical debt and autism).

ETA: Just to expound on this a little bit: there's literally no point to learning Delphi/Pascal in current year. There's no software out there still written in Delphi that you can make bank by knowing how to program in it like there is for COBOL and Fortran. There's nothing remotely fun or different or zany about using the language like there is for Common Lisp or Smalltalk or Prolog or other interesting relics from the past. Nobody these days is even going to know what Pascal is, and the ones that do know will ask why you didn't just use C or C++ instead.
Somehow Delphi is ranked #14 on the TIOBE Index, and even went up from #19 last year, which I think says more about how worthless that index is than how worthwhile a language is to learn.
 
Somehow Delphi is ranked #14 on the TIOBE Index, and even went up from #19 last year, which I think says more about how worthless that index is than how worthwhile a language is to learn.
Screenshot 2022-01-22 at 16-04-21 index TIOBE - The Software Quality Company.png

More people using Delphi than Ruby? (Or using Groovy than Ruby, for that matter? And how the fuck did Groovy even make it to top 10 last year?)

I'm pressing X to doubt, yeah.
 
Delphi as in the Pascal dialect? Fuck no.

Maybe they were useful once upon a time, but those languages are actually dead (as opposed to the 'undead' that COBOL, Fortran and Common Lisp are, where for all intents and purposes people should've moved on but they still have nonzero market share due to technical debt and autism).

ETA: Just to expound on this a little bit: there's literally no point to learning Delphi/Pascal in current year. There's no software out there still written in Delphi that you can make bank by knowing how to program in it like there is for COBOL and Fortran. There's nothing remotely fun or different or zany about using the language like there is for Common Lisp or Smalltalk or Prolog or other interesting relics from the past. Nobody these days is even going to know what Pascal is, and the ones that do know will ask why you didn't just use C or C++ instead.
Lazarus is actually pretty nice and actively developed.
Might use it next time I need to do some quick GUI.
 
Back