How would you do your If and else statements

Pages: 1234
No, the doing-it-all-on-one-line bit. What does this do?

int* x, y, &z;

Is this easier to read?
1
2
3
int *x
,   y
,   &z;
If you dislike both styles, the preferred method is one-by-one:
1
2
3
int *x;
int y;
int &z;
Though when I see individual declarations instead of comma-separated declarations, it changes the connotation of the code from "these are related" to "these are unrelated".
Last edited on
closed account (EwCjE3v7)
No I like the comma way

but the way you did int* x and than later did int *x :D
Trying to make it harder

The first way is more clear to me as the second its kinda saying that this is a statement on its own.
With arrays they have braces {} so you can work it out
YellowPyrmid wrote:
but the way you did int* x and than later did int *x :D
Trying to make it harder
Yes, that was the whole point ;p
closed account (EwCjE3v7)
:D okay, I thought you were trying to show me how seperate lines with commas make it easier to read
@LB
I have never understood why so many say it isn't readable. Your first line is perfectly readable to me. x is a pointer to int, y is an int, z is a reference (granted it will return an error for being uninitialized). That has always confused me, some of the examples people give for being unreadable, to me, usually isn't all that hard to read.
When you are only look at it as a single line in a forum post it is still quite readable yes, but when you have been scanning and reading thousands of lines of code to find a bug or just to find out what something does for the last few hours it can easily be missed.
Last edited on
closed account (N36fSL3A)
LB wrote:
Yes, that was the whole point ;p
punpunpun
Nah, not really now that I think about it.

BHX Specter wrote:
I have never understood why so many say it isn't readable.
It's readable, but it's hard on the eyes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

int main(){
	std::string str;
	std::cin >> str;
	if(str == "hi")
		std::cout << "hi";
	else if(str == "hello"){
	std::cout << "hel";
	std::cout << "lo";
	}else if(str == "salut"){
	std::cout << "sal";
	std::cout << "lut";
	}else if(str == "bonjour")
		std::cout << "bonjour";
	return 0;
}
This is definately readable, but hard on the eyes.

Snipped from the "vector" header from VS2012:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// vector standard header
#pragma once
#ifndef _VECTOR_
#define _VECTOR_
#ifndef RC_INVOKED
#include <xmemory>
#include <stdexcept>

 #pragma pack(push,_CRT_PACKING)
 #pragma warning(push,3)
 #pragma push_macro("new")
 #undef new

 #pragma warning(disable: 4127)
 #pragma warning(disable: 4244)

_STD_BEGIN
 #define _VECTOR_ORPHAN_RANGE	(_ITERATOR_DEBUG_LEVEL == 2)

		// TEMPLATE CLASS _Vector_const_iterator
template<class _Myvec>
	class _Vector_const_iterator
		: public _Iterator012<random_access_iterator_tag,
			typename _Myvec::value_type,
			typename _Myvec::difference_type,
			typename _Myvec::const_pointer,
			typename _Myvec::const_reference,
			_Iterator_base>
	{	// iterator for nonmutable vector
public:
	typedef _Vector_const_iterator<_Myvec> _Myiter;
	typedef random_access_iterator_tag iterator_category;

	typedef typename _Myvec::value_type value_type;
	typedef typename _Myvec::difference_type difference_type;
	typedef typename _Myvec::const_pointer pointer;
	typedef typename _Myvec::const_reference reference;
	typedef typename _Myvec::pointer _Tptr;

	_Vector_const_iterator()
		: _Ptr()
		{	// construct with null pointer
		}

	_Vector_const_iterator(_Tptr _Parg, const _Container_base *_Pvector)
		: _Ptr(_Parg)
		{	// construct with pointer _Parg
		this->_Adopt(_Pvector);
		}

	typedef pointer _Unchecked_type;

	_Myiter& _Rechecked(_Unchecked_type _Right)
		{	// reset from unchecked iterator
		this->_Ptr = (_Tptr)_Right;
		return (*this);
		}

	_Unchecked_type _Unchecked() const
		{	// make an unchecked iterator
		return (_Unchecked_type(this->_Ptr));
		}
Readable, but hard on the eyes.
Last edited on
closed account (EwCjE3v7)
Yea you can read code but sometimes it takes a few seconds to process everything up
@Fredbill
What do you mean? That vector iterator code is perfectly readable - it took me a while to work out what you find hard about it (I assume its the brace placement?).
It looks ugly with 8 spaces per tab.
closed account (N36fSL3A)
I think it looks ugly regardless.
It's probably because the identifiers are all underscores followed by a capital letter - the standard reserves those for the implementation to use so as not to interfere with user code.
These last few posts make a point, it's more shareable if you use spaces instead of tabs:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main(){
  std::string str;
  std::cin >> str;
  if(str == "hi")
    std::cout << "hi";
  else if(str == "hello"){
    std::cout << "hel";
    std::cout << "lo";
  }else if(str == "salut"){
    std::cout << "sal";
    std::cout << "lut";
  }else if(str == "bonjour")
    std::cout << "bonjour";
  return 0;
}


It also makes me feel like if your if/else statements are hard to read, maybe consider a more "tell don't ask" method of coding.
closed account (N36fSL3A)
I don't code like that, it was just an example of an unreadable source file.
Last edited on
"Ident with tabs, align with spaces"

That way everyone could use tab width you like and it will not break aligment.
Example:
1
2
3
4
5
6
if (condition1 &&
    condition2) {
	action1;
	action2(long_argument,
	        even_longer_argument);
}
Copy it to any editor you like and change tab size. It will line up nicely.
I have always advocated "Indent with tabs, align with spaces" as MiiNiPaa suggests, since the tabs can always be converted to spaces by picky programmers. The reverse conversion is more difficult, however, as a simple find and replace will grab the secondary indentation incorrectly.
closed account (N36fSL3A)
Yea, I always do that. I didn't even really think about other programmers, but I just like doing it.
"Ident with tabs, align with spaces"


Hmm, I like that.
Fredbill wrote:
Readable, but hard on the eyes.

Yeah I can see why some would consider that hard on the eyes.

@NT3
Yeah it is readable, but I'm like you that isn't hard on my eyes.

Everyone has different coding styles, but I don't find it hard on my eyes personally. As for tab/spacing, I have to agree as my first IDE was Rhide (sometimes wish it was still maintained so I could still use it for nostalgia reasons) that didn't do anything for you except highlighting the keywords.
closed account (9jNRX9L8)
Personally I don't think anyone on here could explain there whole style because its just so many rules to adapt to. This is my style:

1
2
3
4
5
6
7
8
9
10
11
12
// Variables
int number = 1;
string playerName = "Bob";

// Main method
int main()
{
    if (number = 1)
    {
         cout << "With the number " + number + " " + playerName + " won the ticket to Adventure Island." << endl;
    }
}


Lol? This <> source code format is going out of the text box.
Last edited on
Pages: 1234