Jump to content

Programming Brevity and Style

namarino

When it comes to writing code, is the goal the least number of lines possible? Let me give you an example. If you wanted to take in user input with a scanner, which is better practice?

Scanner input;
int userInput;

input = new Scanner(System.in);
userInput = input.nextInt();

//or

Scanner input = new Scanner(System.in);
int userInput =- input.nextInt();

I've always wondered this. Stylistically, in my opinion, I prefer the first one. That's just how I learned it initially. But for those of you who have been doing it for a long time or might even make of living writing code, which is the more accepted way?

Link to comment
Share on other sites

Link to post
Share on other sites

The goal isn't the least number of lines. Readability and consistency are bigger factors. Style guides can be useful for keeping everyone in sync so you don't have to worry about these little details. Sometimes reducing the number of lines can help readability, sometimes it can hurt it (complex one liners, etc).

 

Your example is pretty trivial so I don't think it really matters much although I don't think you gain anything with option 1. I personally prefer option 2. Given how verbose Java can be, I can understand why it might look pretty ugly to one line it. In C#, which is still more verbose than many languages, you get to use var keyword to help shorten things but Java has no such luck.

// Java
HashMap<String, String> map = new HashMap<String, String>();

// C#
var map = new Dictionary<string, string>();

It's still not too bad when you're looking at it in an editor with syntax highlighting though so I still probably wouldn't break it up across two lines.

 

If you have a large/complex method where things are declared all over the place, it might be useful to have the declarations grouped together somewhere but chances are you have bigger issues if that's the case.

 

Stick to a style you like and be consistent. When working with a team it's useful to use a style guide as mentioned above.

Link to comment
Share on other sites

Link to post
Share on other sites

Just swinging by to throw a bit of a curveball. When using the scanner object it's generally not a great idea to use nextInt. You should really use nextLine and convert that string to what you need :D.

 

That said, I'd write it in the style of #2 myself, but with the modification I mentioned. So I'd decrease lines in one place just to add another one or two somewhere else!

Link to comment
Share on other sites

Link to post
Share on other sites

5 hours ago, madknight3 said:

 


// Java
HashMap<String, String> map = new HashMap<String, String>();

// C#
var map = new Dictionary<string, string>();

 

C++ <3  (assuming using namespace std)

map<string, string> map;

 

And as far as pointers go , which use the "new" keyword , we can use auto specifier :

auto map = new map<string,string>;

 

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

35 minutes ago, Nineshadow said:

 


auto map = new map<string,string>;

 

new is so 1998. 

auto m = make_unique<map<string,string>>(); 

C++ really forces the second style for most stuff since

map<string, string> map;

isn't just declaring something. 

 

Though I'm a fan of Python's way of declaring variables

 

 

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×