+ 8

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.

22nd Apr 2021, 4:11 PM
Sajjad Jafari
Sajjad Jafari - avatar
16 Respostas
+ 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.
22nd Apr 2021, 5:26 PM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 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.
22nd Apr 2021, 5:28 PM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 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.
22nd Apr 2021, 5:58 PM
Sajjad Jafari
Sajjad Jafari - avatar
+ 2
「HAPPY TO HELP」I have a long way ahead dont I, haha. Thanks.
22nd Apr 2021, 6:00 PM
Sajjad Jafari
Sajjad Jafari - avatar
+ 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. 🤗👍
24th Apr 2021, 7:15 AM
🇺🇸 Anatoli🇧🇪🇪🇺 ,
🇺🇸 Anatoli🇧🇪🇪🇺 , - avatar
+ 1
Wow nice
23rd Apr 2021, 6:05 PM
Amila Herath
Amila Herath - avatar
+ 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 🤞😃
24th Apr 2021, 10:45 AM
🇺🇸 Anatoli🇧🇪🇪🇺 ,
🇺🇸 Anatoli🇧🇪🇪🇺 , - avatar
+ 1
Ok
24th Apr 2021, 10:46 AM
Sampson Larbi
Sampson Larbi - avatar
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.
24th Apr 2021, 3:24 PM
Ashwak
Ashwak - avatar
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() );
24th Apr 2021, 7:55 PM
zemiak
- 1
Hlello sir
23rd Apr 2021, 2:18 PM
tusher
tusher - avatar
- 1
it can be shorted somehow
24th Apr 2021, 9:21 AM
Mohamed Elmustafa Abdalla
Mohamed Elmustafa Abdalla - avatar
- 1
I want to know how to code
24th Apr 2021, 10:34 AM
Sampson Larbi
Sampson Larbi - avatar
- 1
Hi im new to coding can someone help me
24th Apr 2021, 1:57 PM
Alec Tormey
- 1
I am vary confused
24th Apr 2021, 3:03 PM
BOSS CODER
24th Apr 2021, 7:39 AM
LearNer
LearNer - avatar