4 ms·
Smalltalk: bank move: 100 from: checkingAccount to: savingsAccount Java: bank.move(100, checkingAccount, savingsAccount)
by cwp 7y ago
Smalltalk:
bank move: 100 from: checkingAccount to: savingsAccount
Java:
bank.move(100, checkingAccount, savingsAccount)
- htk 7y agoLooks similar to Objective C. Verbose but clearer.
- zozbot234 7y agoIsn't this just mixfix syntax? It's simply a matter of convenience and perhaps idiomatic convention.
- cessor 7y agoAlso Java: bank.move(new Amount(100), new FromAccount(checkingAccount), new ToAccount(savingsAccount)); If you are really trying, you will be able to convey enough information so that your fellow programmer. will understand what you are trying to accomplish. Unfortunately, in practice, even Java Experts will convey to you that this style is wrong, classes and objects are " too expensive", primitive values are fine, and that this is too verbose.* I learned about this style from Yegor Bugayenko's Book "Elegant Objects", go check it out. https://www.elegantobjects.org/ https://www.elegantobjects.org/ *) Yes, Java is verbose; not trying to defend that. I am very happy about keyword arguments in Python, for example. But, as soon as you have classes, ctors, objects, you can use them to add rich semantics into your code. Works with functional languages and other, better typesystems, too.
- overgard 7y agoIs there an explanation that doesn’t require buying a $40 book? I searched the site and couldn’t find anything
- edflsafoiewq 7y agoOf course you can, but you can't simply hand-wave away the verbosity. What a language makes easy is usually more important than what it makes possible. In Python, adding from=from_account, to=to_account to clarify a call is basically free; you don't have to change the callee and you don't have to change any other call sites.
- xwowsersx 7y ago> What a language makes easy is usually more important than what it makes possible. very well said
- trophycase 7y agoI wouldn't write it either way. I would write something like bank.processTransfer(new Transfer(100, checkingAccount, savingsAccount)) where the transfer object is immutable.
- xwowsersx 7y agoCool, but the SmallTalk example requires a lot less squinting which is nice.
- dnautics 7y agoI think you don't have enough factories. bank.move(new Amount(100), AccountChannelFactory.create(AccountChannelFactory.CHECKING_CHANNEL, checkingAccount), AccountChannelFactory.create(AccountChannelFactory.SAVINGS_CHANNEL, savingsAccount));
- Stratoscope 7y agoIntelliJ has a option called "Inlay Hints" that would display the Java example like this, with the parameter names dimmed slightly: bank.move( move: 100, from: checkingAccount, to: savingsAccount ) This option is also available for many of the other languages supported by the various JetBrains IDEs.
- xwowsersx 7y agoI hate those and always turn them off haha
- trophycase 7y agoIMO the java is more readable. Parenthesis clearly delimit the scope of the function call. How would smalltalk handle nested function calls?
- edflsafoiewq 7y agoThink like arithmetic. 2 times: (3 plus: 4).
- cwp 7y agoYou can use parenthesis around any expression, as in most languages. eg something like: bank balanceOf: (bank findAccount: #savings) However, parens aren't necessary when a nested message takes no arguments, or uses a binary selector. eg: bank withdraw: 3 + 4 from: user checkingAccount There are 3 messages here. 1. 3 + 4 (execute method + with argument 4) 2. user checkingAccount (getter with no arguments) 3. bank withdraw: <result of #1> from: <result of #2>
- xwowsersx 7y agoNice
- pacala 7y agoPython: bank.move(100, from=checkingAccount, to=savingsAccount)