Is this normal to have a code this long?! | SoloLearn: Learn to code for FREE!

+9

Is this normal to have a code this long?!

Hello newbie here, I'm creating a prototype banking app for challenge. is this normal to have this much code in my main for a simple transaction?! { Bank bank = new Bank("Sina"); bank.addBranch(); bank.createCustomer(); bank.getBranch().get(bank.findbranch("beheshti")).getCustomer().get(bank.getBranch().get(bank.findbranch("beheshti")).findCustomer("Sajjad")).getTransaction(12.5,true); System.out.println(bank.getBranch().get(bank.findbranch("beheshti")).getCustomer().get(bank.getBranch().get(bank.findbranch("beheshti")).findCustomer("Sajjad")).getAmount() ); bank.getBranch().get(bank.findbranch("beheshti")).getCustomer().get(bank.getBranch().get(bank.findbranch("beheshti")).findCustomer("Sajjad")).getTransaction(12.5,false); System.out.println(bank.getBranch().get(bank.findbranch("beheshti")).getCustomer().get(bank.getBranch().get(bank.findbranch("beheshti")).findCustomer("Sajjad")).getAmount() ); } or I'm doing it wrong! The code works perfectly but the look of it is...LOL.

4/22/2021 4:11:44 PM

Sajjad Jafari

19 Answers

New Answer

+11

Well if that's how the Bank class works then it's fine. Conventionally with chaining, it's best to have each (chain)link in a new line e.g. bank.getBranch() .get(...) .find(...) for better reading. As for length, doesn't seem bad for a beginner code; you shouldn't worry much about length (as a beginner) but rather functionality and code duplication

+6

「HAPPY TO HELP」 I have to disagree. Code readability is very important and this is an example of how it shouldn't be. A very easy improvement for readability is the use of temporary variables to brake the code down into smaller parts. If they are used multiple times it will also act as a cache and improve performance. In this case you could introduce variables for bank.findbranch("beheshti") or bank.getBranch().get(bank.findbranch("beheshti")) or even bank.getBranch().get(bank.findbranch("beheshti")).getCustomer().get(bank.getBranch().get(bank.findbranch("beheshti")).findCustomer("Sajjad")) Or best perhaps for all of those. I also liked to write incredibly long lines and I think others also do it as a form of competition. Compact code can be a good thing, but only to a certain point. Readability should be paramount and a major concern, especially(!) for beginners.

+6

Benjamin Jürgens "is this normal to have this much code in my main for a simple transaction?!" I read it as "is this too much code/words/letters to have in my main function just for (calculating) a transaction?" not the chain itself. Yes the chain is horrible I agree that's why I said to make each call in a new line. If I misread it, I agree with your statement

+5

I don't know your class structure / method return types, but I want to make a wild guess: Maybe you can shorten the first long line to this: bank.findbranch("beheshti").findCustomer("Sajjad").getTransaction(12.5,true); Although I still would expect the result of getTransaction to be stored in a variable and used for output in the next line. If getBranch, getCustomer, geTransaction return lists, they should reflect that by using plural, e.g. getBranches etc.

+4

Benjamin Jürgens I do like to make detours and write shorter codes. I'm currently new to world of programming.. Thank you for your advices I'll make sure to take them into account when I code.

+3

「HAPPY TO HELP」I have a long way ahead dont I, haha. Thanks.

+2

Sajjad Jafari Yes haha welcome to programming!

+2

Sajjad Jafari as Senior programmer I can tell you only one thing. Try to order your code in logical bits that's easy to read and if it contains a bug it will be much easier to debug and find the problem. There are free editors made for coding try one of those things will go smoothly and show you the way. Remember no excellent coder has been falling from the heavens yet so we all had to learn a way for a good readability of the code. Good luck from my dude and keep up the good work. As a tip look at the structure of other coders it will set you on the right track. 🤗👍

+1

Wow nice

+1

Sampson Larbi that's quite simple just start one of the courses here available at SoloLearn and you're on your way. Good luck 🤞😃

+1

0

Follow SOLID principles to make your code more elegant and scalable. Single Responsibility principle applies here for your case. Read and apply it to enhance your code at standard level.

0

use variables instead, eg Bank bank = new Bank("Sina"); bank.addBranch(); bank.createCustomer(); var branch = bank .findbranch("beheshti"); var customer = branch.findCustomer("Sajjad"); customer.getTransaction(12.5, true); System.out.println(customer.getAmount() ); customer.getTransaction(12.5, true); System.out.println(customer.getAmount() );

-1

Hlello sir

-1

it can be shorted somehow

-1

I want to know how to code

-1

Hi im new to coding can someone help me

-1

I am vary confused

-7

https://code.sololearn.com/WG6TD1Vi1XMT/?ref=app